forked from mlch911/studentTrainPlan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
387 lines (335 loc) · 13.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from flask import Flask, render_template, request, flash, jsonify, redirect, url_for, session
from utils import query, map_student_course, recommed_module
import json
import time
import os
# 创建flask对象
app = Flask(__name__)
app.config['SECRET_KEY'] = 'gsolvit'
# Flask中的route()装饰器用于将URL绑定到函数
# URL '/ index' 规则绑定到index()函数。 用户访问http://localhost:5000 / index URL,index()函数的输出将在浏览器中呈现。
@app.route('/index', methods=['GET', 'POST'])
def index():
# 通过render_template()函数呈现HTML文件
# print("执行了index()")
return render_template('index.html')
@app.route('/manager', methods=['GET', 'POST'])
def manager():
sql = "select * from STUDENT"
result = query.query(sql)
return render_template('manager.html', result=result)
# 管理员添加用户
# url_for()函数对于动态构建特定函数的URL非常有用。使用url_for()将应用程序重定向到 manager()函数
@app.route('/managerAdd', methods=['GET', 'POST'])
def managerAdd():
stu_id = session.get('stu_id')
#print(stu_id)
if stu_id == 'admin':
if request.method == 'GET':
#print('1111')
return render_template('managerAdd.html')
else:
#print('222')
# 服务器通过POST方法接收数据,从表单form数据获取参数值
name = request.form.get('name')
sex = request.form.get('sex')
stu_no = request.form.get('stu_no')
college = request.form.get('college')
major = request.form.get('major')
ad_year = request.form.get('ad_year')
password = request.form.get('password')
sql="INSERT INTO STUDENT VALUES ('%s','%s','%s','%s','%s','%s','%s','%s')" % (name,sex,stu_no,college,major,ad_year,password,stu_no)
#print(sql)
query.update(sql)
# 将用户重定向到具有指定状态代码的另一个目标位置
return redirect(url_for('manager'))
else:
return u'页面不存在'
# 管理员删除用户
@app.route('/managerDelete', methods=['GET', 'POST'])
def managerDelete():
stu_id = session.get('stu_id')
#print(stu_id)
if stu_id == 'admin':
if request.method == 'GET':
#print('1111')
return render_template('managerDelete.html')
else:
#print('222')
stu_no = request.form.get('stu_no')
sql="DELETE FROM STUDENT WHERE STU_NO='%s'" % stu_no
#print(sql)
query.update(sql)
return redirect(url_for('manager'))
else:
return u'页面不存在'
# 管理员编辑用户
@app.route('/managerEdit', methods=['GET', 'POST'])
def managerEdit():
stu_id = session.get('stu_id')
if stu_id == 'admin':
if request.method == 'GET':
return render_template('managerEdit.html')
else:
stu_no = request.form.get('stu_no')
name = request.form.get('name')
sex = request.form.get('sex')
college = request.form.get('college')
major = request.form.get('major')
ad_year = request.form.get('ad_year')
password = request.form.get('password')
sql="select * from STUDENT WHERE STU_NO='%s'" % stu_no
result=query.query(sql)
if name=='':
name=result[0][0]
if sex=='':
sex=result[0][1]
if college=='':
college=result[0][3]
if major=='':
major=result[0][4]
if ad_year=='':
ad_year=result[0][5]
sql="UPDATE STUDENT SET NAME='%s',SEX='%s',COLLEGE='%s',MAJOR='%s',AD_YEAR='%s',PASSWORD='%s',ID='%s' WHERE STU_NO='%s'" % (name, sex, college, major, ad_year, password, stu_no, stu_no)
#print(sql)
query.update(sql)
return redirect(url_for('manager'))
else:
return u'页面不存在'
# 课程评论
@app.route('/course_discussion', methods=['GET', 'POST'])
def course_discussion():
if request.method == 'GET':
return render_template('course_discussion.html')
else:
topic = request.form.get('topic')
comments = request.form.get('comments')
#commenter = request.form.get('commenter')
# print(len(topic))
# print('course_discussion')
# print(topic, commenter, comments)
stu_id = session.get('stu_id')
sql = "select NAME from STUDENT where STU_NO = '%s'" % stu_id
stu_name = query.query(sql)
stu_name = stu_name[0][0]
now = time.time()
now = time.strftime('%Y-%m-%d', time.localtime(now))
now = str(now)
news_id = stu_name + now
sql = "INSERT INTO NEWS(TOPIC, COMMENTS, COMMENTER, NEWS_ID, IS_FIRST) VALUES ('%s', '%s', '%s', '%s', '0')" % (topic, comments, stu_name, news_id)
print(sql)
query.update(sql)
return redirect(url_for('news_center'))
# 登录
@app.route('/', methods=['GET', 'POST'])
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method=='GET':
return render_template('login.html')
else:
stu_id = request.form.get('stu_id')
password = request.form.get('password')
sql = "select * from STUDENT where STU_NO = '%s'" % stu_id
result = query.query(sql)
print(result)
if len(result) != 0:
#print(result[0][6], password)
if result[0][6] == password:
session['stu_id'] = result[0][2]
session.permanent=True
if stu_id=='admin':
return redirect(url_for('manager'))
else:
return redirect(url_for('index'))
else:
return u'账号或密码错误'
else:
return u'不存在这个用户'
# 注册
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method=='GET':
return render_template('register.html')
else:
stu_id = request.form.get('stu_id')
user = request.form.get('user')
password = request.form.get('password')
password1 = request.form.get('password1')
print(stu_id, user, password, password1)
if(password1 != password):
return u'两次输入密码不同,请检查'
else:
sql = "select * from STUDENT where STU_NO = '%s'" % stu_id
#print(sql)
result = query.query(sql)
#print(result)
if len(result) == 0:
return u'没有这个用户了'
else:
if result[0][6] == user:
sql = "UPDATE student SET PASSWORD='%s' WHERE STU_NO='%s'" % (password, stu_id)
query.update(sql)
return redirect(url_for('login'))
else:
return u'密码错误'
# 课程论坛
@app.route('/news_center', methods=['GET', 'POST'])
def news_center():
sql = "select * from NEWS WHERE IS_FIRST='0'"
result = query.query(sql)
print(result)
return render_template('news_center.html', result=result)
# 回复评论
@app.route('/detail/<question>', methods=['GET', 'POST'])
def detail(question):
print(question)
#question=str(question)
if request.method=='GET':
sql="SELECT TOPIC, COMMENTS, COMMENTER, CREATE_TIME FROM NEWS WHERE NEWS_ID='%s' AND IS_FIRST='0'" % question
title=query.query(sql)
#print(title)
title=title[0]
sql="SELECT * FROM NEWS WHERE IS_FIRST='%s'" % question
result=query.query(sql)
return render_template('detail.html', title=title, result=result)
else:
comments = request.form.get('comments')
stu_id = session.get('stu_id')
sql = "select NAME from STUDENT where STU_NO = '%s'" % stu_id
stu_name = query.query(sql)
stu_name = stu_name[0][0]
now = time.time()
now = time.strftime('%Y-%m-%d', time.localtime(now))
now = str(now)
news_id = stu_name + now
sql = "INSERT INTO NEWS(TOPIC, COMMENTS, COMMENTER, NEWS_ID, IS_FIRST) VALUES ('回复', '%s', '%s', '%s', '%s')" % (comments, stu_name, news_id,question)
print(sql)
query.update(sql)
sql = "SELECT TOPIC, COMMENTS, COMMENTER, CREATE_TIME FROM NEWS WHERE NEWS_ID='%s' AND IS_FIRST='0'" % question
title = query.query(sql)
# print(title)
title = title[0]
sql = "SELECT * FROM NEWS WHERE IS_FIRST='%s'" % question
result = query.query(sql)
return render_template('detail.html', title=title, result=result)
# 课程推荐
@app.route('/recommed', methods=['GET', 'POST'])
def recommed():
return render_template('recommed.html')
# 获取推荐数据
@app.route("/getRecommedData", methods=['GET','POST'])
def getRecommedData():
stu_no = session.get('stu_id')
# 从学生表、课程表获取数据,并返回:【姓名-学号】、【课程名】、【学号--标号】
id2Student, id2Course, stuNo2MatId = map_student_course.get_map_student()
# 获取学生--课程评分二维表,评分矩阵
scoreMatrix = map_student_course.get_matrix(id2Student)
"""
函数,recommedCourse:使用SVD进行课程推荐:
返回:(课程1ID, 课程1评分)
"""
topNCourse, topNStudent = recommed_module.recommedCoursePerson(scoreMatrix, stuNo2MatId[stu_no], N=20)
"""
将得到的Course与Person装换为前端图标需要的json格式:
{
"source": [
[2.3, "计算机视觉"],
[1.1, "自然语言处理"],
[2.4, "高等数学"],
[3.1, "线性代数"],
[4.7, "计算机网络"],
[5.1, "离散数学"]
]
}
"""
id2Student = {i:id2Student[i][0] for i in id2Student.keys()}
print(id2Student)
print(id2Course)
courseJson = recommed_module.toBarJson(topNCourse, id2Course)
personJson = recommed_module.toBarJson(topNStudent, id2Student)
courseJson = recommed_module.regularData(courseJson, 1, 5) # 将列表的值归一化到a、b之间
personJson = recommed_module.regularData(personJson, 0, 1)
coursePersonJson = {}
coursePersonJson['course'] = courseJson
coursePersonJson['person'] = personJson
return jsonify(coursePersonJson)
# 个人中心
@app.route('/personal_information', methods=['GET', 'POST'])
def personal_information():
"""
功能(个人中心界面): 根据"stu_id"从数据库中得到学生基本信息,用于个人中心信息显示
:return:
"""
stu_no = session.get('stu_id')
print(stu_no + ' is stu_no')
sql = "SELECT * FROM student WHERE STU_NO = '%s'" % stu_no
result = query.query(sql)
return render_template('personal_information.html', result=result)
# 课程进度
@app.route('/train_plan', methods=['GET', 'POST'])
def train_plan():
return render_template('train_plan.html')
@app.route('/get_info', methods=['GET', 'POST'])
def get_info():
"""
功能: 初始进入课程进度界面,根据stu_id从数据库中得到数据 并将其转换为计划树所需json格式数据
:return: planTree:(json) 计划树所需数据
"""
stu_id = session.get('stu_id')
planTree = query.getPlanTreeJson(stu_id)
print(planTree)
return jsonify(planTree)
# 提交选课
@app.route('/submit_train_plan', methods=['GET', 'POST'])
def submit_train_place():
"""
功能1:数据库学生选课信息的更新
功能2: 实现计划树以及进度条的提交更新。
:return:
"""
"""功能1:"""
# 使用 request.get_json(force=True) 忽略mimetype
twoData = request.get_json(force=True)
train_plan = twoData['tree']
scores = twoData['scores']
#train_plan['name'] = "数据转换成功"
print('反馈回来的数据是:')
print(train_plan)
data = train_plan['children']
array_finish = [0]*120
#print(array_finish)
for data_children in data:
data_children = data_children['children']
#print(data_children)
for data_children_child_1 in data_children:
#print('data_children_child', data_children_child)
data_children_child_1 = data_children_child_1['children']
for data_children_child in data_children_child_1:
name = data_children_child['children'][0]['name']
color = data_children_child['children'][0]['itemStyle']['borderColor']
#print(name, color)
sql = "select CO_100 from education_plan WHERE CO_NAME='%s'" % name
co_100 = query.query(sql)
co_100 = co_100[0][0]
if color == 'red':
array_finish[int(co_100)] = 0
else:
array_finish[int(co_100)] = 1
finish_co = ''
for i in range(1, 119):
if array_finish[i] == 1:
finish_co += '1'
else:
finish_co += '0'
print(finish_co)
#print(array_finish)
stu_id = session.get('stu_id')
query.updateDatabase(stu_id, train_plan)
query.updateScore(stu_id, scores)
"""功能2:"""
train_plan_str = json.dumps(train_plan)
train_plan_str = train_plan_str.replace("yellow", "green")
train_plan = json.loads(train_plan_str)
return jsonify(train_plan)
if __name__ == '__main__':
app.run("0.0.0.0", debug=True)