From a32d2c8cea87a6b6532eed035eb698fb156e329f Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Fri, 15 Nov 2024 09:41:48 +0100 Subject: [PATCH 1/2] feat(core): introduce a MaintenanceMiddleware Add a MaintenanceMiddleware that allows to set APIS in maintenance mode. Maintenance mode allows superusers to access the site, but all other requests get a simple maintenance page as a response. This can be enabled by adding `apis_core.core.middleware.MaintenanceMiddleware` to `settings.MIDDLEWARE`. To active the maintenance mode simply create a `apis_maintenance` file. To deactivate maintenance mode, remove that file. Closes: #1296 --- apis_core/core/middleware.py | 24 +++++++++++++++++++++++ apis_core/core/static/css/maintenance.css | 20 +++++++++++++++++++ apis_core/core/templates/maintenance.html | 20 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 apis_core/core/middleware.py create mode 100644 apis_core/core/static/css/maintenance.css create mode 100644 apis_core/core/templates/maintenance.html diff --git a/apis_core/core/middleware.py b/apis_core/core/middleware.py new file mode 100644 index 000000000..c4ad034d9 --- /dev/null +++ b/apis_core/core/middleware.py @@ -0,0 +1,24 @@ +import logging +from pathlib import Path + +from django.conf import settings +from django.shortcuts import render + +logger = logging.getLogger(__name__) + + +class MaintenanceMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + maintenance_file = getattr( + settings, "APIS_MAINTENANCE_FILE", "apis_maintenance" + ) + if Path(maintenance_file).exists(): + logger.warning("Site is running in maintenance mode") + if hasattr(request, "user"): + if request.user.is_superuser: + return self.get_response(request) + return render(request, "maintenance.html") + return self.get_response(request) diff --git a/apis_core/core/static/css/maintenance.css b/apis_core/core/static/css/maintenance.css new file mode 100644 index 000000000..ac9b25d7b --- /dev/null +++ b/apis_core/core/static/css/maintenance.css @@ -0,0 +1,20 @@ +body { + text-align: center; + padding: 150px; +} + +h1 { + font-size: 50px; +} + +body { + font: 20px Helvetica, sans-serif; + color: #333; +} + +article { + display: block; + text-align: left; + width: 650px; + margin: 0 auto; +} diff --git a/apis_core/core/templates/maintenance.html b/apis_core/core/templates/maintenance.html new file mode 100644 index 000000000..0c555a1f5 --- /dev/null +++ b/apis_core/core/templates/maintenance.html @@ -0,0 +1,20 @@ + +{% load static %} + + + + + Site Maintenance + + +
+

We’ll be back soon!

+
+

+ Sorry for the inconvenience but we’re performing some maintenance at the moment. We’ll be back online shortly! +

+

— The Team

+
+
+ + From 771151921fded72e692c413138ed9cb6738eefa7 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Fri, 15 Nov 2024 09:58:13 +0100 Subject: [PATCH 2/2] docs: add documentation regarding MaintenanceMiddleware --- docs/source/configuration.rst | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index 0f008dfad..14808f54c 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -133,4 +133,21 @@ APIS_LIST_VIEW_OBJECT_FILTER Allows to define a function that receives the view - including e.g. the `request` object - and a queryset and can do custom filtering on that queryset. This can be used to set the listviews to public using the -`APIS_LIST_VIEWS_ALLOWED` setting, but still only list specific entities. \ No newline at end of file +`APIS_LIST_VIEWS_ALLOWED` setting, but still only list specific entities. + + +Maintenance Middleware +^^^^^^^^^^^^^^^^^^^^^^ + +APIS ships a maintenance middlware that you can use and activate to enable a maintenance mode in your project. +Maintenance mode means that only superuser accounts can access the webinterfaces, all other requests are being +answered with a simple maintenance mode page (the ``maintenance.html`` template). +To use the middleware, add + +.. code-block:: python + + "apis_core.core.middleware.MaintenanceMiddleware" + +to your ``settings.MIDDLEWARE`` list. To activate the maintenance mode once the middlware is enabled, simply +create a file ``apis_maintenance`` in the directory the main Django process runs in. +The path of the maintenance file can be changed in the settings: ``APIS_MAINTENANCE_FILE = "path of the file"``