- Introduced application-wide constants for file uploads, image processing, pagination, and authentication in `constants.py`. - Implemented synchronous and asynchronous board ownership verification functions in `ownership.py`. - Created a base repository class with common CRUD operations in `repository.py`. - Added standard response utilities for error and success messages in `responses.py`. - Refactored image validation to utilize constants for file size and MIME types. - Enhanced frontend components with consistent styling and validation utilities for forms. - Established global styles for buttons, forms, loading indicators, and messages to ensure a cohesive UI experience.
76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
"""Standard response utilities."""
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import status
|
|
|
|
|
|
class ErrorResponse:
|
|
"""Standard error response formats."""
|
|
|
|
@staticmethod
|
|
def not_found(resource: str = "Resource") -> dict[str, Any]:
|
|
"""404 Not Found response."""
|
|
return {
|
|
"status_code": status.HTTP_404_NOT_FOUND,
|
|
"detail": f"{resource} not found",
|
|
}
|
|
|
|
@staticmethod
|
|
def forbidden(message: str = "Access denied") -> dict[str, Any]:
|
|
"""403 Forbidden response."""
|
|
return {
|
|
"status_code": status.HTTP_403_FORBIDDEN,
|
|
"detail": message,
|
|
}
|
|
|
|
@staticmethod
|
|
def unauthorized(message: str = "Authentication required") -> dict[str, Any]:
|
|
"""401 Unauthorized response."""
|
|
return {
|
|
"status_code": status.HTTP_401_UNAUTHORIZED,
|
|
"detail": message,
|
|
"headers": {"WWW-Authenticate": "Bearer"},
|
|
}
|
|
|
|
@staticmethod
|
|
def bad_request(message: str) -> dict[str, Any]:
|
|
"""400 Bad Request response."""
|
|
return {
|
|
"status_code": status.HTTP_400_BAD_REQUEST,
|
|
"detail": message,
|
|
}
|
|
|
|
@staticmethod
|
|
def conflict(message: str) -> dict[str, Any]:
|
|
"""409 Conflict response."""
|
|
return {
|
|
"status_code": status.HTTP_409_CONFLICT,
|
|
"detail": message,
|
|
}
|
|
|
|
|
|
class SuccessResponse:
|
|
"""Standard success response formats."""
|
|
|
|
@staticmethod
|
|
def created(data: dict[str, Any], message: str = "Created successfully") -> dict[str, Any]:
|
|
"""201 Created response."""
|
|
return {
|
|
"message": message,
|
|
"data": data,
|
|
}
|
|
|
|
@staticmethod
|
|
def ok(data: dict[str, Any] | None = None, message: str = "Success") -> dict[str, Any]:
|
|
"""200 OK response."""
|
|
response = {"message": message}
|
|
if data:
|
|
response["data"] = data
|
|
return response
|
|
|
|
@staticmethod
|
|
def no_content() -> None:
|
|
"""204 No Content response."""
|
|
return None
|