83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
# 问题记录路由
|
|
|
|
from flask import request, jsonify
|
|
from app.routes import main_bp
|
|
from app.models import db, Student, StudentProblem
|
|
from app.routes.auth import login_required_json
|
|
|
|
|
|
@main_bp.route("/api/students/<int:student_id>/problems", methods=["GET"])
|
|
@login_required_json
|
|
def get_student_problems(student_id):
|
|
"""获取学员的问题列表"""
|
|
student = Student.query.get_or_404(student_id)
|
|
problems = student.problems.all()
|
|
return jsonify([p.to_dict() for p in problems])
|
|
|
|
|
|
@main_bp.route("/api/students/<int:student_id>/problems", methods=["POST"])
|
|
@login_required_json
|
|
def add_student_problem(student_id):
|
|
"""为学员添加问题(同步模式:先删再加)"""
|
|
student = Student.query.get_or_404(student_id)
|
|
data = request.get_json()
|
|
|
|
problems = data.get("problems", [])
|
|
submitted_ids = set()
|
|
|
|
# 删除旧的问题(如果勾选被取消)
|
|
existing = StudentProblem.query.filter_by(student_id=student_id).all()
|
|
for e in existing:
|
|
if e.problem_id not in [p.get("problem_id") for p in problems]:
|
|
db.session.delete(e)
|
|
|
|
# 添加或更新问题
|
|
for p in problems:
|
|
problem_id = p.get("problem_id")
|
|
submitted_ids.add(problem_id)
|
|
|
|
# 检查是否已存在
|
|
existing = StudentProblem.query.filter_by(
|
|
student_id=student_id, problem_id=problem_id
|
|
).first()
|
|
|
|
if existing:
|
|
# 更新现有记录
|
|
existing.severity = p.get("severity")
|
|
existing.level = p.get("level")
|
|
else:
|
|
# 添加新记录
|
|
problem = StudentProblem(
|
|
student_id=student_id,
|
|
problem_id=problem_id,
|
|
problem_name=p.get("problem_name"),
|
|
severity=p.get("severity"),
|
|
level=p.get("level"),
|
|
)
|
|
db.session.add(problem)
|
|
|
|
db.session.commit()
|
|
return jsonify({"message": "问题记录成功"})
|
|
|
|
|
|
@main_bp.route("/api/students/<int:student_id>/problems", methods=["DELETE"])
|
|
@login_required_json
|
|
def clear_student_problems(student_id):
|
|
"""清除学员的问题记录"""
|
|
StudentProblem.query.filter_by(student_id=student_id).delete()
|
|
db.session.commit()
|
|
return jsonify({"message": "清除成功"})
|
|
|
|
|
|
@main_bp.route(
|
|
"/api/students/<int:student_id>/problems/<problem_id>", methods=["DELETE"]
|
|
)
|
|
@login_required_json
|
|
def delete_single_problem(student_id, problem_id):
|
|
"""删除学员的单个问题"""
|
|
StudentProblem.query.filter_by(
|
|
student_id=student_id, problem_id=problem_id
|
|
).delete()
|
|
db.session.commit()
|
|
return jsonify({"message": "删除成功"})
|