Files
HangmanLab.Backend/api/template.py

137 lines
4.8 KiB
Python

from flask import Blueprint, jsonify, request
from db import get_db
from api import require_auth, etag_response
from db.models.MarkdownTemplate import MarkdownTemplate
from db.models.PathTemplate import PathTemplate
template_bp = Blueprint('template', __name__, url_prefix='/api/template')
@template_bp.route('/path/<int:template_id>', methods=['GET'])
@etag_response
def get_path_template(template_id):
with get_db() as session:
template = session.query(PathTemplate).get(template_id)
if template is None:
return jsonify({}), 204
return jsonify(template.to_dict()), 200
@template_bp.route('/path/', methods=['GET'])
@etag_response
def get_path_templates():
with get_db() as session:
templates = session.query(PathTemplate).all()
return jsonify([template.to_dict() for template in templates]), 200
@template_bp.route('/path/', methods=['POST'])
@require_auth(roles=['admin'])
def create_path_template():
data = request.json
if "title" not in data:
return jsonify({"error": "title is missing"}), 400
title = data.get("title")
structure = data.get("structure")
template = PathTemplate(title=title, structure=structure)
try:
with get_db() as session:
session.add(template)
session.commit()
return template.to_dict(), 200
except Exception as e:
return jsonify({"error": "failed to create path template"}), 400
@template_bp.route('/path/<int:template_id>', methods=['PUT', 'PATCH'])
@require_auth(roles=['admin'])
def update_path_template(template_id):
data = request.json
with get_db() as session:
template = session.query(PathTemplate).get(template_id)
if template is None:
return jsonify({'error': 'path template not found'}), 400
title = data.get("title", template.title)
structure = data.get("structure", template.structure)
template.title = title
template.structure = structure
session.commit()
return jsonify(template.to_dict()), 200
@template_bp.route('/path/<int:template_id>', methods=['DELETE'])
@require_auth(roles=['admin'])
def delete_path_template(template_id):
with get_db() as session:
template = session.query(PathTemplate).get(template_id)
if template is None:
return jsonify({'error': 'path template not found'}), 400
session.delete(template)
session.commit()
return jsonify({'message': 'deleted'}), 200
@template_bp.route('/markdown/<int:template_id>', methods=['GET'])
@etag_response
def get_markdown_template(template_id):
with get_db() as session:
template = session.query(MarkdownTemplate).get(template_id)
if template is None:
return jsonify({}), 204
return jsonify(template.to_dict()), 200
@template_bp.route('/markdown/', methods=['GET'])
@etag_response
def get_markdown_templates():
with get_db() as session:
templates = session.query(MarkdownTemplate).all()
return jsonify([template.to_dict() for template in templates]), 200
@template_bp.route('/markdown/', methods=['POST'])
@require_auth(roles=['admin'])
def create_markdown_template():
data = request.json
if "title" not in data:
return jsonify({"error": "title is missing"}), 400
title = data.get("title")
parameters = data.get("parameters")
define = data.get("define")
template = MarkdownTemplate(title=title, parameters=parameters, define=define)
try:
with get_db() as session:
session.add(template)
session.commit()
return template.to_dict(), 200
except Exception as e:
return jsonify({"error": "failed to create markdown template"}), 400
@template_bp.route('/markdown/<int:template_id>', methods=['PUT', 'PATCH'])
@require_auth(roles=['admin'])
def update_markdown_template(template_id):
data = request.json
with get_db() as session:
template = session.query(MarkdownTemplate).get(template_id)
if template is None:
return jsonify({'error': 'markdown template not found'}), 400
title = data.get("title", template.title)
parameters = data.get("parameters", template.parameters)
define = data.get("define", template.define)
template.title = title
template.parameters = parameters
template.define = define
session.commit()
return jsonify(template.to_dict()), 200
@template_bp.route('/markdown/<int:template_id>', methods=['DELETE'])
@require_auth(roles=['admin'])
def delete_markdown_template(template_id):
with get_db() as session:
template = session.query(MarkdownTemplate).get(template_id)
if template is None:
return jsonify({'error': 'markdown template not found'}), 400
session.delete(template)
session.commit()
return jsonify({'message': 'deleted'}), 200