register and sign in pages
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "core"
|
||||
@@ -0,0 +1,32 @@
|
||||
"""A single response shape for every error the API returns.
|
||||
|
||||
{"detail": "...", "code": "...", "errors": {"field": ["..."]}}
|
||||
|
||||
`errors` is only present for validation failures. The SPA reads `code` to
|
||||
branch (e.g. `registration_required`) and `detail` to show a message.
|
||||
"""
|
||||
|
||||
from rest_framework import exceptions
|
||||
from rest_framework.views import exception_handler as drf_exception_handler
|
||||
|
||||
|
||||
def exception_handler(exc, context):
|
||||
response = drf_exception_handler(exc, context)
|
||||
if response is None:
|
||||
return None
|
||||
|
||||
code = getattr(exc, "default_code", "error")
|
||||
data = response.data
|
||||
|
||||
if isinstance(exc, exceptions.ValidationError):
|
||||
detail = "The submitted data was invalid."
|
||||
if isinstance(data, dict):
|
||||
non_field = data.get("detail") or data.get("non_field_errors")
|
||||
if non_field:
|
||||
detail = non_field[0] if isinstance(non_field, list) else str(non_field)
|
||||
response.data = {"detail": str(detail), "code": code, "errors": data}
|
||||
return response
|
||||
|
||||
detail = data.get("detail") if isinstance(data, dict) else data
|
||||
response.data = {"detail": str(detail), "code": code}
|
||||
return response
|
||||
@@ -0,0 +1,6 @@
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
|
||||
class DefaultPagination(PageNumberPagination):
|
||||
page_size_query_param = "page_size"
|
||||
max_page_size = 100
|
||||
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import healthcheck
|
||||
|
||||
urlpatterns = [
|
||||
path("health/", healthcheck, name="healthcheck"),
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
from django.db import connection
|
||||
from rest_framework.decorators import api_view, authentication_classes, permission_classes
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
@authentication_classes([])
|
||||
@permission_classes([])
|
||||
def healthcheck(request):
|
||||
"""Liveness + database reachability, for compose/nginx health probes."""
|
||||
try:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("SELECT 1")
|
||||
database = "up"
|
||||
except Exception: # pragma: no cover - reported, not raised
|
||||
database = "down"
|
||||
return Response({"status": "ok", "database": database})
|
||||
Reference in New Issue
Block a user