93 lines
3.6 KiB
Python
93 lines
3.6 KiB
Python
import json
|
|
from flask import jsonify, request
|
|
from api import etag_response, require_auth
|
|
from api.template import template_bp
|
|
from db import get_db
|
|
from db.models.MarkdownTemplate import MarkdownTemplate
|
|
|
|
cached_templates = {}
|
|
def inflate_template(template):
|
|
template.parameters = json.loads(template.parameters)
|
|
for parameter in template.parameters:
|
|
if parameter.get('type', {}).get('base_type') == 'template':
|
|
sub_template_id = parameter.get('type', {}).get('definition', {}).get('template', {}).get('id', 0)
|
|
if sub_template_id in cached_templates.keys():
|
|
parameter['type']['definition']['template'] = cached_templates[sub_template_id]
|
|
else:
|
|
with get_db() as session:
|
|
sub_template = session.query(MarkdownTemplate).get(sub_template_id)
|
|
parameter['type']['definition']['template'] = inflate_template(sub_template)
|
|
cached_templates[template.id] = template
|
|
return template
|
|
|
|
|
|
@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(inflate_template(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([inflate_template(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")
|
|
layout = data.get("layout")
|
|
template = MarkdownTemplate(title=title, parameters=parameters, layout=layout)
|
|
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()
|
|
if template_id in cached_templates.keys():
|
|
cached_templates[template_id] = inflate_template(template.to_dict())
|
|
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()
|
|
if template_id in cached_templates.keys():
|
|
cached_templates.pop(template_id)
|
|
return jsonify({'message': 'deleted'}), 200
|
|
|