更新:models/routes/services/templates/docs
This commit is contained in:
+30
-4
@@ -1,13 +1,13 @@
|
||||
from flask import Blueprint, request, jsonify, render_template
|
||||
from app.models import db, Goal
|
||||
from app.routes.auth import login_required_json, admin_required
|
||||
from flask import Blueprint, request, jsonify, render_template, session
|
||||
from app.models import db, Goal, StudentGoal, GoalRelation, StudentGoalEvaluation
|
||||
from app.routes.auth import login_required_json, admin_required, login_required
|
||||
from app.routes import main_bp
|
||||
|
||||
goals_bp = Blueprint("goals", __name__)
|
||||
|
||||
# 目标管理页面路由
|
||||
@main_bp.route("/goals")
|
||||
@admin_required
|
||||
@login_required
|
||||
def goals_page():
|
||||
"""目标管理页面"""
|
||||
return render_template("goals.html", active_nav="goals")
|
||||
@@ -57,7 +57,33 @@ def update_goal(goal_id):
|
||||
@goals_bp.route("/api/goals/<int:goal_id>", methods=["DELETE"])
|
||||
@login_required_json
|
||||
def delete_goal(goal_id):
|
||||
"""删除目标模板(仅管理员,需检查依赖)"""
|
||||
from app.models import User
|
||||
user = User.query.get(session.get("user_id"))
|
||||
if not user or user.role != "admin":
|
||||
return jsonify({"error": "权限不足,仅管理员可操作"}), 403
|
||||
|
||||
goal = Goal.query.get_or_404(goal_id)
|
||||
|
||||
# 检查依赖关系
|
||||
deps = []
|
||||
|
||||
# 1. 检查是否有学员分配了此目标
|
||||
student_goal_count = StudentGoal.query.filter_by(goal_id=goal_id).count()
|
||||
if student_goal_count > 0:
|
||||
deps.append(f"已被 {student_goal_count} 名学员分配")
|
||||
|
||||
# 2. 检查是否有目标关联(作为父目标或子目标)
|
||||
as_parent = GoalRelation.query.filter_by(parent_goal_id=goal_id).count()
|
||||
as_child = GoalRelation.query.filter_by(child_goal_id=goal_id).count()
|
||||
if as_parent > 0:
|
||||
deps.append(f"有 {as_parent} 个子目标关联")
|
||||
if as_child > 0:
|
||||
deps.append(f"作为子目标被 {as_child} 个目标引用")
|
||||
|
||||
if deps:
|
||||
return jsonify({"error": f"无法删除:{', '.join(deps)}"}), 400
|
||||
|
||||
db.session.delete(goal)
|
||||
db.session.commit()
|
||||
return jsonify({"message": "删除成功"})
|
||||
|
||||
Reference in New Issue
Block a user