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..6db88a3 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_MODE_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) -