Files
monica_site/site/public/middleware.py
T
westfarn 1f7d78de64
Deploy Beta / unit-tests (push) Successful in 9s
Deploy Beta / docker (push) Successful in 17s
Deploy Beta / deploy-beta (push) Successful in 2m31s
Add Django site, Docker packaging, and beta/prod Gitea deploys.
Unignore site/ (was blocked by mkdocs /site rule), add compose/Docker/uv tooling, and split deploys so push to main goes to beta while prod stays manual.
2026-08-08 07:32:55 -05:00

47 lines
1.4 KiB
Python

from django.conf import settings
from django.shortcuts import redirect
class UnderConstructionMiddleware:
"""
When SITE_UNDER_CONSTRUCTION is true, redirect public traffic to the holding page.
Exempt: health, static/media, admin, auth login/logout, SMS webhook, and the
under-construction page itself (so notify-me POSTs work).
"""
EXEMPT_PREFIXES = (
"/healthz",
"/static/",
"/media/",
"/admin/",
"/accounts/login",
"/accounts/logout",
"/under-construction",
"/portal/messaging/webhooks/",
"/unsubscribe/",
"/api/address-suggest",
)
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if settings.SITE_UNDER_CONSTRUCTION and not self._is_exempt(request):
return redirect("public:under_construction")
return self.get_response(request)
def _is_exempt(self, request) -> bool:
path = request.path
if any(path.startswith(prefix) for prefix in self.EXEMPT_PREFIXES):
return True
user = getattr(request, "user", None)
if (
user is not None
and getattr(user, "is_authenticated", False)
and getattr(user, "is_staff", False)
and path.startswith("/portal/")
):
return True
return False