from flask import jsonify, request, Blueprint import api from api import etag_response, limiter, require_auth from db import get_db from db.models.MarkdownTemplateSetting import MarkdownTemplateSetting markdown_template_setting_bp = Blueprint('markdown_template_setting', __name__, url_prefix='/api/setting/markdown/template') @markdown_template_setting_bp.route('/', methods=['GET']) @etag_response def list_template_settings(): """ List all markdown template settings. This endpoint retrieves a list of all markdown template settings in the system. Returns: A JSON array containing all template settings. Response Codes: - 200: Success """ with get_db() as session: settings = session.query(MarkdownTemplateSetting).all() return jsonify([s.to_dict() for s in settings]), 200 @markdown_template_setting_bp.route('/', methods=['GET']) @etag_response @limiter.limit(api.get_rate_limit) def get_template_setting(setting_id): """ Get a specific markdown template setting by ID. This endpoint retrieves a markdown template setting by its ID. Request: - setting_id (int): The ID of the template setting to retrieve Returns: A JSON object containing the template setting. Response Codes: - 200: Success - 204: No content (setting not found) """ with get_db() as session: setting = session.query(MarkdownTemplateSetting).get(setting_id) if not setting: return jsonify({}), 204 return jsonify(setting.to_dict()), 200 @markdown_template_setting_bp.route('/', methods=['POST']) @require_auth(roles=['admin']) def create_template_setting(): """ Create a new markdown template setting. This endpoint creates a new markdown template setting with the provided template ID. It requires authentication with the 'admin' role. Request: - template_id (int): The ID of the template to associate with this setting Returns: A JSON object containing the created template setting. Response Codes: - 201: Created successfully """ data = request.json template_id = data.get('template_id') new_setting = MarkdownTemplateSetting(template_id=template_id) with get_db() as session: session.add(new_setting) session.commit() return jsonify(new_setting.to_dict()), 201 @markdown_template_setting_bp.route('/', methods=['PUT', 'PATCH']) @require_auth(roles=['admin']) def update_template_setting(setting_id): """ Update a markdown template setting. This endpoint updates an existing markdown template setting with the provided template ID. It requires authentication with the 'admin' role. - PUT: Replaces the entire template setting - PATCH: Updates only the specified fields Request: - setting_id (int): The ID of the template setting to update - template_id (int): The new template ID Returns: A JSON object containing the updated template setting. Response Codes: - 200: Updated successfully - 404: Template setting not found """ with get_db() as session: setting = session.get(MarkdownTemplateSetting, setting_id) if setting is None: return jsonify({"error": "template setting not found"}), 404 data = request.json if request.method == 'PUT': setting.template_id = data.get('template_id') elif request.method == 'PATCH': if 'template_id' in data: setting.template_id = data.get('template_id') session.commit() return jsonify(setting.to_dict()), 200 @markdown_template_setting_bp.route('/', methods=['DELETE']) @require_auth(roles=['admin']) def delete_template_setting(setting_id): """ Delete a markdown template setting. This endpoint deletes an existing markdown template setting. It requires authentication with the 'admin' role. Request: - setting_id (int): The ID of the template setting to delete Returns: A JSON object containing the deleted template setting. Response Codes: - 200: Deleted successfully """ with get_db() as session: setting = session.get(MarkdownTemplateSetting, setting_id) st = setting.to_dict() session.delete(setting) session.commit() return jsonify(st), 200