Problem statement
In this Django project, implement the maintenance gate that keeps visitors off the site during a release while the people doing the release keep working. MaintenanceModeMiddleware in app/middleware.py is registered and written as a pass-through, so right now the flag can be turned on and every page still answers as if nothing were happening. Teach it to answer the request itself.
Requirements
Implement:
- the body of __call__ on MaintenanceModeMiddleware in app/middleware.py
The following files are already implemented and provided: app/views.py, app/urls.py and project/settings.py. MIDDLEWARE already lists MaintenanceModeMiddleware directly after django.contrib.auth.middleware.AuthenticationMiddleware, so request.user is attached by the time this middleware runs; project/settings.py is read-only, so the class cannot be moved or re-registered. The routes are given: / (home) prints a banner, /accounts/login/ (login) is Django's LoginView, /dashboard/ (dashboard) answers Dashboard ready., /status/ (status) answers Status: ok., /status/deep/ (deep-status) answers Status: deep check ok. and /statusboard/ (statusboard) answers Statusboard ready.. Every view appends its own name to the module-level list VIEW_CALLS in app/views.py before it answers, and the tests read that list, so whether a view ran is checked directly rather than guessed from a status code. Four settings are given, shipped as MAINTENANCE_MODE = False, MAINTENANCE_MESSAGE = "Site is down for maintenance. Please try again shortly.", MAINTENANCE_RETRY_AFTER = 120 and MAINTENANCE_ALLOWED_PATHS = ["/status/", "/accounts/login/"].
1. The gate
- While MAINTENANCE_MODE is on, a request that is not exempt comes back 503 with a body that is exactly MAINTENANCE_MESSAGE: ``text Site is down for maintenance. Please try again shortly. ``
- No view runs for such a request. The decision is made before the rest of the chain is called, not after: a gate that answers once the view has already done the work still shows the caller a 503, but the work happened.
2. The retry header
- That 503 carries a Retry-After header whose value is MAINTENANCE_RETRY_AFTER: ``text Retry-After: 120 ``
- Only the maintenance response carries it. A request that passes through comes back with the header absent.
3. The staff exemption
- A request from a user whose is_staff is true passes through and gets the real page.
- Being signed in is not the exemption: a signed-in user who is not staff is gated exactly like an anonymous visitor.
4. The allowlist
- A request whose path starts with any entry of MAINTENANCE_ALLOWED_PATHS passes through, whoever is asking.
- The entries are matched as prefixes of the full path, not as whole paths: /status/ is listed and passes, /status/deep/ starts with /status/ and passes too, and /statusboard/ does not start with /status/ so it is gated like anything else.
- /accounts/login/ is listed as shipped, so staff can still sign in to become staff.
5. The flag off
- While MAINTENANCE_MODE is off the middleware changes nothing: every visitor, staff or not, on any path, gets the view's own response, with no Retry-After header on it.
6. All four values are read, not copied
- Each of the four is read from django.conf.settings while the request is handled. One test runs the same gate again with MAINTENANCE_MESSAGE, MAINTENANCE_RETRY_AFTER and MAINTENANCE_ALLOWED_PATHS overridden, and the body, the header value and which paths pass through all follow the new values.
Notes
- A message, a retry value or a path prefix written into the middleware as a literal reports the shipped value after the setting has changed.
- request.user.is_staff is a plain boolean and is False for an anonymous visitor, so anonymity never needs a test of its own.
- A response you build yourself starts with no headers, so a header appears only where you put it.
- Only app/middleware.py changes.
- Do not touch sessions, messages or the cache, and do not redirect: the gate answers the request itself.
