StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 13 · Middleware

Maintenance Mode Gate

mediumDjango MiddlewareAccess Control—No data yet

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.

More in Middleware

See the module
Request Timing MiddlewareNothing in the project records what each request asked for or how long it took, so operations has no timing or identity in the logs. Write the middleware that times the downstream handling and logs one structured entry per request while passing every response through untouched.—No data yetmediumException Capture OrderingUnhandled view errors answer with a raw 500 that carries no trace id, and a request has nothing tying its log lines together. Write a middleware that stamps every request with an id and turns an unhandled view exception into the defined JSON error response carrying it, then register it in the right place in the stack.—No data yetmediumObservable Request PipelineThe app needs one coherent request pipeline - identity, timing, maintenance gating, and error conversion working together in the right order - and currently has none of it. Write the layers that assign a request id, log one timed entry carrying it, gate general traffic with the defined 503 while staff pass, and convert an unhandled view exception into the defined error response, then register them so every concern sees what it must.0%0 of 1 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.