Files
Evection/apps/accounts/middleware.py
T
ksmithandClaude Sonnet 5 2e4674a18f Make the updater deploy tagged releases, like ds-chat's upgrade.sh
scripts/upgrade.sh now fetches tags and does a detached-HEAD checkout of
the latest one (git tag --sort=-creatordate | head -n1), matching the
ds-chat project's deploy/upgrade.sh exactly rather than a plain git pull
on whatever branch happened to be checked out. Errors clearly if the repo
has no tags yet (true for Evection right now — nothing to deploy until a
release is tagged and pushed), and no-ops cleanly if already on the
latest tag. scripts/install.sh does the same tag pin on first install
(falling back to whatever's checked out if no tags exist yet), so the
very first deploy is consistent with every upgrade after it.

Added a GET /health/ endpoint (apps.core.views.health) that checks DB
connectivity and is exempt from the first-run setup redirect, for the
upgrade script's post-restart verification (curled over the gunicorn
unix socket, same as ds-chat's curl-based check) and for any external
uptime monitor.

While wiring the setup-wizard exemption for /health/, found that
SetupWizardMiddleware._a_user_exists's reset fixture was scoped only to
apps/accounts/tests.py, so a test anywhere else that creates a user and
makes a request could leak a stale "a user exists" into later test files
with no reset — confirmed this concretely by temporarily breaking the new
exemption and watching the "works before setup" test still pass purely
from accounts/tests.py leaving the module-order-adjacent apps/articles
tests to poison the shared class attribute. Moved the fixture to a new
top-level conftest.py so it's autouse across every test file, which also
makes the new health-check test meaningful regardless of run order.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 22:15:37 -06:00

49 lines
1.8 KiB
Python

from django.conf import settings
from django.shortcuts import redirect
from django.urls import reverse
from .models import User
_SETUP_EXEMPT_PREFIXES = (settings.STATIC_URL,)
class SetupWizardMiddleware:
"""Force every request to the first-run setup wizard until an account exists.
Once any User row exists, /setup/ becomes unreachable again (see the
setup view), mirroring FreshRSS's one-time install wizard. The "a user
exists" check only ever flips false -> true for the lifetime of an
instance, so it's cached per-process after the first positive result to
avoid a query on every request.
"""
_a_user_exists = False
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
path = request.path
if not path.startswith(_SETUP_EXEMPT_PREFIXES) and path not in _setup_exempt_paths():
if not SetupWizardMiddleware._a_user_exists:
SetupWizardMiddleware._a_user_exists = User.objects.exists()
setup_url = reverse("accounts:setup")
if path != setup_url and not SetupWizardMiddleware._a_user_exists:
return redirect(setup_url)
return self.get_response(request)
def _setup_exempt_paths():
"""Routes that must work even before the instance's first-run setup is
complete: the manifest/service worker/offline page (a browser can fetch
these on any visit, setup page included — it extends the same base
template) and the health check (ops tooling, e.g. scripts/upgrade.sh,
needs it reachable immediately after a fresh deploy, before anyone has
created an account)."""
return {
reverse("pwa:manifest"),
reverse("pwa:service_worker"),
reverse("pwa:offline"),
reverse("core:health"),
}