Skip to content

Commit

Permalink
feat: added the ability to remove the middleware during setup/reload
Browse files Browse the repository at this point in the history
chore: removed duplicate if statement
  • Loading branch information
aqeelat committed Sep 23, 2022
1 parent 36a4185 commit f853a05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions maintenancemode/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 9 additions & 2 deletions maintenancemode/middleware.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -66,4 +74,3 @@ def process_request(self, request):
callback = resolve('503')

return callback(request)

0 comments on commit f853a05

Please sign in to comment.