173 lines
5.9 KiB
Python
173 lines
5.9 KiB
Python
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):
|
|
for parameter in template.get('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):
|
|
"""
|
|
Get a specific markdown template by ID.
|
|
|
|
This endpoint retrieves a markdown template by its ID.
|
|
The template is inflated to include any nested templates.
|
|
|
|
Request:
|
|
- template_id (int): The ID of the template to retrieve
|
|
|
|
Returns:
|
|
A JSON object containing the markdown template.
|
|
|
|
Response Codes:
|
|
- 200: Success
|
|
- 204: No content (template not found)
|
|
"""
|
|
with get_db() as session:
|
|
template = session.query(MarkdownTemplate).get(template_id)
|
|
if template is None:
|
|
return jsonify({}), 204
|
|
print(inflate_template(template.to_dict()))
|
|
return jsonify(inflate_template(template.to_dict())), 200
|
|
|
|
@template_bp.route('/markdown/', methods=['GET'])
|
|
@etag_response
|
|
def get_markdown_templates():
|
|
"""
|
|
List all markdown templates.
|
|
|
|
This endpoint retrieves a list of all markdown templates in the system.
|
|
Each template is inflated to include any nested templates.
|
|
|
|
Returns:
|
|
A JSON array containing all markdown templates.
|
|
|
|
Response Codes:
|
|
- 200: Success
|
|
"""
|
|
with get_db() as session:
|
|
templates = session.query(MarkdownTemplate).all()
|
|
print(templates)
|
|
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():
|
|
"""
|
|
Create a new markdown template.
|
|
|
|
This endpoint creates a new markdown template with the provided data.
|
|
It requires authentication with the 'admin' role.
|
|
|
|
Request:
|
|
- title (str): The title of the template (required)
|
|
- parameters (object, optional): The parameters for the template
|
|
- layout (str, optional): The layout content of the template
|
|
|
|
Returns:
|
|
A JSON object containing the created template.
|
|
|
|
Response Codes:
|
|
- 200: Created successfully
|
|
- 400: Bad request (missing title or other error)
|
|
"""
|
|
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 jsonify(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):
|
|
"""
|
|
Update a markdown template.
|
|
|
|
This endpoint updates an existing markdown template with the provided data.
|
|
It requires authentication with the 'admin' role.
|
|
|
|
Request:
|
|
- template_id (int): The ID of the template to update
|
|
- title (str, optional): The new title for the template
|
|
- parameters (object, optional): The new parameters for the template
|
|
- layout (str, optional): The new layout content for the template
|
|
|
|
Returns:
|
|
A JSON object containing the updated template.
|
|
|
|
Response Codes:
|
|
- 200: Updated successfully
|
|
- 400: Bad request (template not found)
|
|
"""
|
|
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)
|
|
layout = data.get("layout", template.layout)
|
|
template.title = title
|
|
template.parameters = parameters
|
|
template.layout = layout
|
|
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):
|
|
"""
|
|
Delete a markdown template.
|
|
|
|
This endpoint deletes an existing markdown template.
|
|
It requires authentication with the 'admin' role.
|
|
|
|
Request:
|
|
- template_id (int): The ID of the template to delete
|
|
|
|
Returns:
|
|
A JSON object with a success message.
|
|
|
|
Response Codes:
|
|
- 200: Deleted successfully
|
|
- 400: Bad request (template not found)
|
|
"""
|
|
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
|