dm/python/app.py

243 lines
8.7 KiB
Python
Raw Normal View History

2024-06-19 13:13:34 +08:00
from teacher_func import *
from student_func import *
2024-06-19 19:23:01 +08:00
from flask import Flask, render_template, request, jsonify, send_from_directory, session
2024-06-19 13:13:34 +08:00
from flask_cors import CORS
2024-06-19 19:23:01 +08:00
app = Flask(__name__, static_folder="public")
2024-06-19 13:13:34 +08:00
CORS(app, resources={r"/*": {"origins": "*"}})
app.config['SECRET_KEY'] = '350625'
2024-06-20 14:55:36 +08:00
@app.route('/api/student', methods=['POST']) # 检测历史是否登录过登录则直接进入index反之则进去login界面
2024-06-19 13:13:34 +08:00
def student(): # 判断是否已经登录
ID_data = request.json
ID = ID_data['student_ID']
student_succeed = student_succeed_func(ID)
student_lesson = get_lesson_func(ID)
return jsonify({'student_succeed': student_succeed, 'student_lesson': student_lesson})
2024-06-20 14:55:36 +08:00
@app.route('/api/login', methods=['POST']) # 登录功能
2024-06-19 13:13:34 +08:00
def login():
login_data = request.json # 获取前端发送过来的数据
ID = login_data['ID'] # 解析数据
password = login_data['password']
if len(ID) == 8: # 根据长度判断,长度为十则是学生
try:
en_password, password = student_login_func(ID, password)
print(en_password, password)
# 使用数据库查询并将查询结果用login_password接受
if en_password == password: # 进行判断并返回
session['id'] = ID
print('True')
print(ID)
return jsonify({'result': '登录成功', 'user': 'user'})
else:
print('False')
return jsonify({'result': '密码错误'})
except IndexError:
return jsonify({'result': '账号不存在,请先注册'})
elif len(ID) == 6: # 长度为6则是老师
try:
if password == teacher_login_func(ID):
session['id'] = ID
return jsonify({'result': '登录成功', 'user': 'teacher'})
else:
return jsonify({'result': '密码错误'})
except IndexError:
return jsonify({'result': '账号不存在,请先注册'})
elif len(ID) == 9: # 若长度为9则是管理员账号根据数据库进行修改即可
try:
if password == administrator_login_func(ID):
session['id'] = ID
return jsonify({'result': '登录成功', 'user': 'Administrator'})
else:
return jsonify({'result': '密码错误'})
except IndexError:
return jsonify({'result': '账号不存在,请先注册'})
else:
return jsonify({'result': '账号不存在'})
# postman
2024-06-20 14:55:36 +08:00
@app.route('/api/logout', methods=['POST']) # 退出登录功能
2024-06-19 13:13:34 +08:00
def logout(): # 退出登录
ID = session['id']
print(ID)
session.pop('id', None) # 删除id
return jsonify({'result': '注销成功'})
2024-06-20 14:55:36 +08:00
@app.route('/api/register', methods=['POST']) # 注册功能实现
2024-06-19 13:13:34 +08:00
def register():
register_data = request.json # 获取前端发送过来的数据
name = register_data['name']
ID = register_data['ID']
password = register_data['password']
Class = register_data['Class']
gender = register_data['gender']
try:
register_func(name, ID, password, Class, gender)
except:
return jsonify({'result': '注册失败'})
return jsonify({'result': '注册成功'})
2024-06-20 14:55:36 +08:00
@app.route('/api/student/get_lesson', methods=['GET'])
2024-06-19 13:13:34 +08:00
def get_lesson(): # 获取课程
student_id = session.get('id')
student_lesson = get_lesson_func(student_id)
print(student_lesson)
return jsonify({"student_lesson": student_lesson})
# 学生获取试卷
2024-06-20 14:55:36 +08:00
@app.route('/api/student/get_test', methods=['POST'])
2024-06-19 13:13:34 +08:00
def get_test(): # 获取试卷以及历史试卷
data = request.json
ID = data['student_ID']
return jsonify({"data": fetch_test_func(ID)})
2024-06-20 14:55:36 +08:00
@app.route('/api/student/fetch_result', methods=['POST']) # 查找成绩
2024-06-19 13:13:34 +08:00
def fetch_result():
data = request.json
ID = data['student_ID']
return jsonify({'result': fetch_result_func(ID)})
2024-06-20 14:55:36 +08:00
@app.route('/api/student/get_end_student', methods=['POST'])
2024-06-19 13:13:34 +08:00
def get_end_student():
data = request.json
ID = data['student_ID']
return jsonify({'result': find_end_test(ID)})
2024-06-20 14:55:36 +08:00
@app.route('/api/student/score_entry', methods=['POST'])
2024-06-19 13:13:34 +08:00
def score_entry():
data = request.json
ID = data['student_ID']
score = data['score']
testID = data['examId']
testID = int(testID)
score_entry_func(score, testID, ID)
return jsonify({'result': '成功'})
2024-06-20 15:30:16 +08:00
@app.route('/api/student/FetchTrainTitle',methods=['POST'])
2024-06-20 14:54:17 +08:00
def FetchTrainTitle():
data = request.json
ID = data['student_ID']
title=FetchTrainTitleFunc(ID)
return jsonify({'title': title})
2024-06-21 14:40:55 +08:00
@app.route('/api/student/TrainData',methods=['POST'])
def TrainData():
2024-06-24 19:51:39 +08:00
data=request.json
examID=data['operateID']
2024-06-21 14:40:55 +08:00
result=FetchTrainTestFunc(examID)
return result
2024-06-24 19:51:39 +08:00
@app.route('/api/student/HistoryTrain',methods=['POST'])
def HistoryTrain():
ID = request.json['student_ID']
result=HistoryTrainFunc(ID)
return jsonify({'HistoryTrain': result})
2024-06-21 14:40:55 +08:00
2024-06-20 14:54:17 +08:00
2024-06-19 13:13:34 +08:00
# 以下为教师功能—————————————————————————————————————————————————————————————————————
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/return_question', methods=['POST'])
2024-06-19 13:13:34 +08:00
def return_question():
data = request.json
ID = data['teacher_ID']
choice_question = choice_question_func(ID) # 调取相应老师科目的题库
completion_question = completion_question_func(ID)
t_or_f_question = t_or_f_question_func(ID)
return jsonify({'选择': choice_question, '填空': completion_question, '判断': t_or_f_question})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/select_class', methods=['POST'])
2024-06-19 13:13:34 +08:00
def select_class(): # 查找每个老师管理的班级 以及在考试管理查找默认班级
teacher_id = request.json['teacher_ID']
# testID =request.json['testID']
class_dic = find_classboss_succeed_func(teacher_id)
default_class = list(class_dic.keys())[0]
# print(testID)
return jsonify({'Class': class_dic,'default': find_default_class_func(default_class, teacher_id)})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/change_class', methods=['POST'])
2024-06-19 13:13:34 +08:00
def change_class(): # 查找每个老师管理的班级 以及在考试管理查找默认班级
teacher_id = request.json['teacher_ID']
ClassID=request.json['selectedValue']
return jsonify({'default': find_default_class_func(ClassID,teacher_id)})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/detaileddata', methods=['POST'])
2024-06-19 13:13:34 +08:00
def detaileddata():
data=request.json
ClassID=data['selectedValue']
testID=data['testID']
return jsonify({'data':detailed_class(testID,ClassID)})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/', methods=['POST'])
2024-06-19 13:13:34 +08:00
def find_test():
return
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/find_student', methods=['POST'])
2024-06-19 13:13:34 +08:00
def find_student(): # 获取班级学生的具体信息
student_list = []
Data = request.json
ClassID = Data['key']
student_succeed = find_class_student(ClassID)
for i in student_succeed:
student_list.append(find_student_succeed_func(i))
return [student_succeed, student_list]
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/accept_test', methods=['POST']) # 在前端发布成功之后要存入数据库
2024-06-19 13:13:34 +08:00
def accept_test():
data = request.json
choice_list = data['ChoiceQuestionSet']
completion_list = data['CompletionQuestionSet']
judge_list = data['JudgeQuestionSet']
hour = data['HourValue']
min = data['MinValue']
stop_time = data['StopTime']
class_list = data['selectedItems']
ID = data['teacher_ID']
save_test(choice_list, completion_list, judge_list, hour, min, stop_time, class_list, ID)
print('添加成功')
return jsonify({'result': '发布成功'})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/fetch_train_question',methods=["POST"])
2024-06-19 13:13:34 +08:00
def fetch_train_question():
return train_question()
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/SendTrainTest',methods=["POST"])
2024-06-19 13:13:34 +08:00
def SendTrainTest():
data=request.json
TrainChoice=data['TrainChoice']
TrainCompletion=data['TrainCompletion']
TrainJudge=data['TrainJudge']
Hour=data['HourValue']
Min=data['MinValue']
StopTime=data['StopTime']
Class=data['selectedItems']
Train=data['Train']
teacher_id=data['teacher_ID']
SendTrainTestFunc(TrainChoice, TrainCompletion, TrainJudge, Hour, Min, StopTime,Class,Train,teacher_id)
return '发布成功'
2024-06-19 19:23:01 +08:00
@app.route('/')
@app.route('/<path:path>')
def catch_all(path = "index.html"):
return send_from_directory("public", path)
2024-06-19 13:13:34 +08:00
if __name__ == '__main__':
2024-06-19 19:23:01 +08:00
app.run(host='0.0.0.0', port=5000, debug=True)