148 lines
4.5 KiB
Python
148 lines
4.5 KiB
Python
from flask import jsonify, request
|
|
from flask.sansio.blueprints import Blueprint
|
|
|
|
from api import etag_response, require_auth
|
|
from api.template import template_bp
|
|
from db import get_db
|
|
from db.models.PathTemplate import PathTemplate
|
|
|
|
path_template_bp = Blueprint('path_template', __name__, url_prefix='/api/template/path')
|
|
@path_template_bp.route('/<int:template_id>', methods=['GET'])
|
|
@etag_response
|
|
def get_path_template(template_id):
|
|
"""
|
|
Get a specific path template by ID.
|
|
|
|
This endpoint retrieves a path template by its ID.
|
|
|
|
Request:
|
|
- template_id (int): The ID of the template to retrieve
|
|
|
|
Returns:
|
|
A JSON object containing the path template.
|
|
|
|
Response Codes:
|
|
- 200: Success
|
|
- 204: No content (template not found)
|
|
"""
|
|
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
|
|
|
|
@path_template_bp.route('/', methods=['GET'])
|
|
@etag_response
|
|
def get_path_templates():
|
|
"""
|
|
List all path templates.
|
|
|
|
This endpoint retrieves a list of all path templates in the system.
|
|
|
|
Returns:
|
|
A JSON array containing all path templates.
|
|
|
|
Response Codes:
|
|
- 200: Success
|
|
"""
|
|
with get_db() as session:
|
|
templates = session.query(PathTemplate).all()
|
|
return jsonify([template.to_dict() for template in templates]), 200
|
|
|
|
|
|
@path_template_bp.route('/', methods=['POST'])
|
|
@require_auth(roles=['admin'])
|
|
def create_path_template():
|
|
"""
|
|
Create a new path template.
|
|
|
|
This endpoint creates a new path template with the provided data.
|
|
It requires authentication with the 'admin' role.
|
|
|
|
Request:
|
|
- title (str): The title of the template (required)
|
|
- structure (str): The structure definition for 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")
|
|
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
|
|
|
|
|
|
@path_template_bp.route('/<int:template_id>', methods=['PUT', 'PATCH'])
|
|
@require_auth(roles=['admin'])
|
|
def update_path_template(template_id):
|
|
"""
|
|
Update a path template.
|
|
|
|
This endpoint updates an existing path 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
|
|
- structure (str, optional): The new structure definition 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(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
|
|
|
|
|
|
@path_template_bp.route('/<int:template_id>', methods=['DELETE'])
|
|
@require_auth(roles=['admin'])
|
|
def delete_path_template(template_id):
|
|
"""
|
|
Delete a path template.
|
|
|
|
This endpoint deletes an existing path 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(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
|