Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove middleware during app start/reload if not needed #44

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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)