176 lines
5.0 KiB
Python
176 lines
5.0 KiB
Python
# 模板管理路由
|
|
|
|
from flask import Blueprint, request, jsonify
|
|
from app.models import db, Template
|
|
from app.routes.auth import admin_required
|
|
|
|
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()
|
|
|
|
|
|
@templates_bp.route("/")
|
|
@admin_required
|
|
def templates_page():
|
|
"""模板管理页面"""
|
|
from flask import render_template
|
|
return render_template("templates.html")
|
|
|
|
|
|
@templates_bp.route("/templates", methods=["GET"])
|
|
@admin_required
|
|
def get_templates():
|
|
"""获取所有模板(按sort_order排序,可按type筛选)"""
|
|
query = Template.query.order_by(Template.sort_order.asc())
|
|
template_type = request.args.get('type')
|
|
if template_type:
|
|
query = query.filter_by(type=template_type)
|
|
templates = query.all()
|
|
return jsonify([t.to_dict() for t in templates])
|
|
|
|
|
|
@templates_bp.route("/templates/<int:template_id>", methods=["GET"])
|
|
@admin_required
|
|
def get_template(template_id):
|
|
"""获取单个模板"""
|
|
tmpl = Template.query.get_or_404(template_id)
|
|
return jsonify(tmpl.to_dict())
|
|
|
|
|
|
@templates_bp.route("/templates", methods=["POST"])
|
|
@admin_required
|
|
def create_template():
|
|
"""创建模板"""
|
|
data = request.get_json()
|
|
tmpl = Template(
|
|
name=data.get("name"),
|
|
type=data.get("type"),
|
|
content=data.get("content"),
|
|
description=data.get("description", ""),
|
|
sort_order=data.get("sort_order", 0)
|
|
)
|
|
db.session.add(tmpl)
|
|
db.session.commit()
|
|
return jsonify(tmpl.to_dict()), 201
|
|
|
|
|
|
@templates_bp.route("/templates/<int:template_id>", methods=["PUT"])
|
|
@admin_required
|
|
def update_template(template_id):
|
|
"""更新模板"""
|
|
tmpl = Template.query.get_or_404(template_id)
|
|
data = request.get_json()
|
|
|
|
if "content" in data:
|
|
tmpl.content = data["content"]
|
|
if "description" in data:
|
|
tmpl.description = data["description"]
|
|
if "name" in data:
|
|
tmpl.name = data["name"]
|
|
if "sort_order" in data:
|
|
tmpl.sort_order = data["sort_order"]
|
|
|
|
db.session.commit()
|
|
return jsonify(tmpl.to_dict())
|
|
|
|
|
|
@templates_bp.route("/templates/<int:template_id>", methods=["DELETE"])
|
|
@admin_required
|
|
def delete_template(template_id):
|
|
"""删除模板"""
|
|
tmpl = Template.query.get_or_404(template_id)
|
|
db.session.delete(tmpl)
|
|
db.session.commit()
|
|
return jsonify({"message": "删除成功"})
|
|
|
|
|
|
@templates_bp.route("/templates/<string:template_type>/render", methods=["POST"])
|
|
@admin_required
|
|
def render_template_preview(template_type):
|
|
"""渲染模板(用于预览)"""
|
|
data = request.get_json()
|
|
tmpl = Template.query.filter_by(type=template_type).first()
|
|
|
|
if not tmpl:
|
|
return jsonify({"error": "模板不存在"}), 404
|
|
|
|
content = tmpl.content
|
|
# 替换变量
|
|
for key, value in data.items():
|
|
placeholder = "{" + key + "}"
|
|
content = content.replace(placeholder, str(value))
|
|
|
|
return jsonify({"rendered": content})
|