更新:models/routes/services/templates/docs

This commit is contained in:
hmo
2026-04-26 18:02:36 +08:00
parent f7a82ac48a
commit 6abdd49c04
31 changed files with 1480 additions and 676 deletions
+13 -74
View File
@@ -2,83 +2,15 @@
from flask import Blueprint, request, jsonify
from app.models import db, Template
from app.routes.auth import admin_required
from app.routes.auth import admin_required, login_required_json
templates_bp = Blueprint('templates', __name__, url_prefix='/templates')
# 默认模板
DEFAULT_TEMPLATES = {
"ai_prompt": {
"name": "AI提示词模板",
"type": "ai_prompt",
"description": "生成练习方案时发送给AI的提示词",
"sort_order": 0,
"content": """你是一位资深的钢琴教师。请根据学员的具体问题详情,生成一份个性化练习方案报告。
## 学员基本信息
- **姓名**: {student_name}
- **微信昵称**: {wechat_nickname}
- **每日可练习时间**: {practice_time}
## 学员被诊断的问题
{student_problems}
## 每个问题的详细信息和练习方法(请务必基于这些内容生成方案)
{problems}
## 任务要求
请根据上述学员的问题诊断和详细信息,生成一份针对性的练习方案报告:
1. 先简述该学员当前存在的主要问题
2. 给出一个每日练习安排建议(你可以根据问题特点灵活安排热身、技术练习、曲目练习等环节)
3. 针对每个问题给出具体的日常练习方法
4. 给出3-5条重点注意事项
请使用Markdown格式,语言专业、简洁、有鼓励性。""",
},
"report": {
"name": "报告导出模板",
"type": "report",
"description": "导出方案时使用的Markdown模板",
"sort_order": 0,
"content": """# 钢琴练习方案 - {student_name}
**练习时间**: {practice_time} (共{total_minutes}分钟)
**生成时间**: {generated_at}
---
## AI个性化报告
{ai_report}
## 问题诊断
{problem_tags}
## 每日练习计划
{schedule_table}
---
*坚持练习 · 必有进步*""",
}
}
def init_default_templates():
"""初始化默认模板(如果不存在)"""
for key, tmpl in DEFAULT_TEMPLATES.items():
existing = Template.query.filter_by(name=tmpl["name"]).first()
if not existing:
t = Template(
name=tmpl["name"],
type=tmpl["type"],
content=tmpl["content"],
description=tmpl["description"],
sort_order=tmpl.get("sort_order", 0)
)
db.session.add(t)
db.session.commit()
# 默认模板从数据库获取,按 sort_order 排序
pass # 不再使用代码中的硬编码模板
@templates_bp.route("/")
@@ -90,7 +22,7 @@ def templates_page():
@templates_bp.route("/templates", methods=["GET"])
@admin_required
@login_required_json
def get_templates():
"""获取所有模板(按sort_order排序,可按type筛选)"""
query = Template.query.order_by(Template.sort_order.asc())
@@ -151,6 +83,13 @@ def update_template(template_id):
def delete_template(template_id):
"""删除模板"""
tmpl = Template.query.get_or_404(template_id)
template_type = tmpl.type
# 检查该类型模板总数,删除后必须至少剩1个
count = Template.query.filter_by(type=template_type).count()
if count <= 1:
return jsonify({"error": f"无法删除:{template_type}类型至少需要保留1个模板"}), 400
db.session.delete(tmpl)
db.session.commit()
return jsonify({"message": "删除成功"})
@@ -159,9 +98,9 @@ def delete_template(template_id):
@templates_bp.route("/templates/<string:template_type>/render", methods=["POST"])
@admin_required
def render_template_preview(template_type):
"""渲染模板(用于预览)"""
"""渲染模板(用于预览)- 使用该类型中 sort_order 最小的模板"""
data = request.get_json()
tmpl = Template.query.filter_by(type=template_type).first()
tmpl = Template.query.filter_by(type=template_type).order_by(Template.sort_order.asc()).first()
if not tmpl:
return jsonify({"error": "模板不存在"}), 404