Skip to content

Ignore App urls from maintenancemode #25

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

Open
wants to merge 4 commits 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
Empty file added example/ignore_app/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions example/ignore_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
7 changes: 7 additions & 0 deletions example/ignore_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import unicode_literals

from django.apps import AppConfig


class IgnoreAppConfig(AppConfig):
name = 'ignore_app'
Empty file.
5 changes: 5 additions & 0 deletions example/ignore_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import unicode_literals

from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions example/ignore_app/templates/503.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h3>Temporary unavailable</h3>

<p>You requested: {{ request_path }}</p>
3 changes: 3 additions & 0 deletions example/ignore_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions example/ignore_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import url

from .views import home, page_x

urlpatterns = [
url(r'^ignore_app/?$', home),
url(r'^ignore_app/page_x/?$', page_x),
]
9 changes: 9 additions & 0 deletions example/ignore_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.http import HttpResponse


def home(request):
return HttpResponse('Excluded App from maintenance mode')


def page_x(request):
return HttpResponse('Excluded App Page from maintenance mode')
8 changes: 7 additions & 1 deletion example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

PROJECT_APPS = [
'maintenancemode',
'ignore_app'
]

INSTALLED_APPS = [
Expand Down Expand Up @@ -95,6 +96,11 @@
STATIC_URL = '/static/'

MAINTENANCE_MODE = True # or ``False`` and use ``maintenance`` command

MAINTENANCE_IGNORE_URLS = (
re.compile(r'^/ignored.*'),
re.compile(r'^/ignored/?$'),
)

MAINTENANCE_IGNORE_APPS = (
'ignore_app.urls',
)
2 changes: 2 additions & 0 deletions example/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf.urls import url
from django.conf.urls import include

from .views import index

urlpatterns = [
url(r'^$', index),
url(r'^ignored/$', index),
url(r'', include('ignore_app.urls')),
]
1 change: 1 addition & 0 deletions maintenancemode/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class MaintenanceSettings(AppConf):
IGNORE_URLS = ()
IGNORE_APPS = []
LOCKFILE_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'maintenance.lock')
MODE = False
Expand Down
12 changes: 12 additions & 0 deletions maintenancemode/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

IGNORE_URLS = tuple([re.compile(u) for u in settings.MAINTENANCE_IGNORE_URLS])

IGNORE_APPS = []

if settings.MAINTENANCE_IGNORE_APPS:
for each_app in settings.MAINTENANCE_IGNORE_APPS:
app_obj = urls.include(each_app)[0]
IGNORE_APPS.extend([p.regex for p in app_obj.urlpatterns])


class MaintenanceModeMiddleware(object):

Expand Down Expand Up @@ -44,6 +51,11 @@ def process_request(self, request):
if url.match(request.path_info):
return None

# Check if app paths is explicitly excluded from maintenance mode
for url in IGNORE_APPS:
if url.match(request.path_info.replace('/', '', 1)):
return None

# Otherwise show the user the 503 page
resolver = urlresolvers.get_resolver(None)

Expand Down