from flask import jsonify, request import api from api import limiter, require_auth, etag_response from api.setting import setting_bp from db import get_db from db.models.PathSetting import PathSetting import logging logger = logging.getLogger(__name__) @setting_bp.route('/path/', methods=['GET']) @limiter.limit(api.get_rate_limit) @etag_response def get_path_setting(setting_id): """ Get a specific path setting by ID. This endpoint retrieves a path setting by its ID. Request: - setting_id (int): The ID of the path setting to retrieve Returns: A JSON object containing the path setting. Response Codes: - 200: Success - 204: No content (setting not found) """ with get_db() as session: setting = session.query(PathSetting).get(setting_id) if setting is None: return jsonify({}), 204 return jsonify(setting.to_dict()), 200 @setting_bp.route('/path/', methods=['POST']) @require_auth(roles=['admin']) def create_path_setting(): """ Create a new path setting. This endpoint creates a new path setting with the provided webhook and template settings. It requires authentication with the 'admin' role. Request: - webhook_setting_id (int, optional): The ID of the webhook setting to associate - template_setting_id (int, optional): The ID of the template setting to associate Returns: A JSON object containing the created path setting. Response Codes: - 200: Created successfully - 500: Server error """ data = request.json webhook_setting_id = data.get('webhook_setting_id') template_setting_id = data.get('template_setting_id') setting = PathSetting( webhook_setting_id=webhook_setting_id, template_setting_id=template_setting_id ) with get_db() as session: try: session.add(setting) session.commit() return jsonify(setting.to_dict()), 200 except Exception: logger.error(f"failed to create path setting") return jsonify({"error": "failed to create setting"}), 500 @setting_bp.route('/path/', methods=['PUT', 'PATCH']) @require_auth(roles=['admin']) def update_path_setting(setting_id): """ Update a path setting. This endpoint updates an existing path setting with the provided webhook and template settings. It requires authentication with the 'admin' role. Request: - setting_id (int): The ID of the path setting to update - webhook_setting_id (int, optional): The new webhook setting ID - template_setting_id (int, optional): The new template setting ID Returns: A JSON object containing the updated path setting. Response Codes: - 200: Updated successfully - 400: Bad request (setting not found) - 500: Server error """ data = request.json with get_db() as session: try: setting = session.query(PathSetting).get(setting_id) if setting is None: return jsonify({"error": "setting not exists"}), 400 webhook_setting_id = data.get('webhook_setting_id', setting.webhook_setting_id) template_setting_id = data.get('template_setting_id', setting.template_setting_id) setting.webhook_setting_id = webhook_setting_id setting.template_setting_id = template_setting_id session.commit() return jsonify(setting.to_dict()), 200 except Exception: return jsonify({"error": "failed to update path setting"}), 500 @setting_bp.route('/path/', methods=['DELETE']) @require_auth(roles=['admin']) def delete_path_setting(setting_id): """ Delete a path setting. This endpoint deletes an existing path setting. It requires authentication with the 'admin' role. Request: - setting_id (int): The ID of the path setting to delete Returns: A JSON object with a success message. Response Codes: - 200: Deleted successfully - 400: Bad request (setting not found) """ with get_db() as session: setting = session.query(PathSetting).get(setting_id) if setting is None: return jsonify({"error": "setting not exists"}), 400 session.delete(setting) return jsonify({"message": "deleted"}), 200