Updated claude big changes
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from flask_login import current_user
|
||||
from flask import abort, jsonify
|
||||
from flask import abort
|
||||
|
||||
from functools import wraps
|
||||
from app.common.responses import error_response
|
||||
|
||||
def login_required(f):
|
||||
@wraps(f)
|
||||
@@ -20,6 +21,6 @@ def admin_required(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
if not current_user.is_authenticated or not current_user.admin_role:
|
||||
return jsonify({"error": "Admin access required"}), 403
|
||||
return error_response("Admin access required", 403)
|
||||
return f(*args, **kwargs)
|
||||
return decorated_function
|
||||
|
||||
54
app/common/responses.py
Normal file
54
app/common/responses.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
Standardized API response utilities for consistent error and success responses.
|
||||
|
||||
Usage:
|
||||
from app.common.responses import error_response, success_response
|
||||
|
||||
# Error responses
|
||||
return error_response("User not found", 404)
|
||||
return error_response("Invalid credentials", 401)
|
||||
return error_response("Server error", 500, details=str(e))
|
||||
|
||||
# Success responses
|
||||
return success_response({"user": user_data})
|
||||
return success_response({"message": "Created"}, 201)
|
||||
"""
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
def error_response(message: str, status_code: int = 400, details: str = None):
|
||||
"""
|
||||
Create a standardized error response.
|
||||
|
||||
Args:
|
||||
message: Human-readable error message
|
||||
status_code: HTTP status code (default 400)
|
||||
details: Optional additional details (e.g., exception info)
|
||||
|
||||
Returns:
|
||||
Tuple of (JSON response, status code)
|
||||
"""
|
||||
response = {
|
||||
"ok": False,
|
||||
"error": message
|
||||
}
|
||||
if details:
|
||||
response["details"] = details
|
||||
return jsonify(response), status_code
|
||||
|
||||
|
||||
def success_response(data: dict = None, status_code: int = 200):
|
||||
"""
|
||||
Create a standardized success response.
|
||||
|
||||
Args:
|
||||
data: Response data dictionary
|
||||
status_code: HTTP status code (default 200)
|
||||
|
||||
Returns:
|
||||
Tuple of (JSON response, status code)
|
||||
"""
|
||||
response = {"ok": True}
|
||||
if data:
|
||||
response.update(data)
|
||||
return jsonify(response), status_code
|
||||
Reference in New Issue
Block a user