Skip to content

Commit 6c8428d

Browse files
committed
feat: added the ability to remove the middleware during setup/reload
chore: removed duplicate if statement
1 parent 36a4185 commit 6c8428d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

maintenancemode/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class MaintenanceSettings(AppConf):
99
IGNORE_URLS = ()
1010
LOCKFILE_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), "maintenance.lock")
1111
MODE = False
12+
ONLY_EVALUATE_DURING_RELOAD = False
1213

1314
class Meta:
1415
prefix = "maintenance"

maintenancemode/middleware.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
from django import VERSION as django_version
33
from django.conf import urls
4+
from django.core.exceptions import MiddlewareNotUsed
45
from django.urls import get_resolver
56
from django.urls import resolvers
67
from django.utils.deprecation import MiddlewareMixin
@@ -15,13 +16,20 @@
1516
DJANGO_VERSION_MAJOR = django_version[0]
1617
DJANGO_VERSION_MINOR = django_version[1]
1718

19+
1820
class MaintenanceModeMiddleware(MiddlewareMixin):
21+
def __init__(self, get_response):
22+
if settings.MAINTENANCE_ONLY_EVALUATE_DURING_RELOAD and not maintenance.status():
23+
raise MiddlewareNotUsed()
24+
25+
super().__init__(get_response=get_response)
26+
1927
def process_request(self, request):
2028
# Allow access if middleware is not activated
2129
allow_staff = getattr(settings, "MAINTENANCE_ALLOW_STAFF", True)
2230
allow_superuser = getattr(settings, "MAINTENANCE_ALLOW_SUPERUSER", True)
2331

24-
if not (settings.MAINTENANCE_MODE or maintenance.status()):
32+
if not maintenance.status():
2533
return None
2634

2735
INTERNAL_IPS = maintenance.IPList(settings.INTERNAL_IPS)
@@ -66,4 +74,3 @@ def process_request(self, request):
6674
callback = resolve('503')
6775

6876
return callback(request)
69-

0 commit comments

Comments
 (0)