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.PathTemplate import PathTemplate @template_bp.route('/path/', 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/', 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/', 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