From 6c8428d248eb10b6505b910179f97a63ae2b0566 Mon Sep 17 00:00:00 2001 From: Abdullah Alaqeel Date: Fri, 23 Sep 2022 15:12:58 +0300 Subject: [PATCH] feat: added the ability to remove the middleware during setup/reload chore: removed duplicate if statement --- maintenancemode/conf.py | 1 + maintenancemode/middleware.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/maintenancemode/conf.py b/maintenancemode/conf.py index 49fc81e..035e2fe 100644 --- a/maintenancemode/conf.py +++ b/maintenancemode/conf.py @@ -9,6 +9,7 @@ class MaintenanceSettings(AppConf): IGNORE_URLS = () LOCKFILE_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "maintenance.lock") MODE = False + ONLY_EVALUATE_DURING_RELOAD = False class Meta: prefix = "maintenance" diff --git a/maintenancemode/middleware.py b/maintenancemode/middleware.py index 2f01b7c..7977f52 100644 --- a/maintenancemode/middleware.py +++ b/maintenancemode/middleware.py @@ -1,6 +1,7 @@ import re from django import VERSION as django_version from django.conf import urls +from django.core.exceptions import MiddlewareNotUsed from django.urls import get_resolver from django.urls import resolvers from django.utils.deprecation import MiddlewareMixin @@ -15,13 +16,20 @@ DJANGO_VERSION_MAJOR = django_version[0] DJANGO_VERSION_MINOR = django_version[1] + class MaintenanceModeMiddleware(MiddlewareMixin): + def __init__(self, get_response): + if settings.MAINTENANCE_ONLY_EVALUATE_DURING_RELOAD and not maintenance.status(): + raise MiddlewareNotUsed() + + super().__init__(get_response=get_response) + def process_request(self, request): # Allow access if middleware is not activated allow_staff = getattr(settings, "MAINTENANCE_ALLOW_STAFF", True) allow_superuser = getattr(settings, "MAINTENANCE_ALLOW_SUPERUSER", True) - if not (settings.MAINTENANCE_MODE or maintenance.status()): + if not maintenance.status(): return None INTERNAL_IPS = maintenance.IPList(settings.INTERNAL_IPS) @@ -66,4 +74,3 @@ def process_request(self, request): callback = resolve('503') return callback(request) -