Maintainance Middleware

181 views
Skip to first unread message

Christian M Hoeppner

unread,
May 15, 2007, 12:37:36 PM5/15/07
to Django users
Hi there!

How would one handle the following tasks in a middleware?

If a settings called "MAINTENANCE" is true, check if the user is logged in. If
he is, let him view the requested page normally. If not, redirect to
maintenance page.

If setting is false, show page normally despite user logged in or not.

It's a way to show unauthorised users an "under construction" page while the
maintenance settings is set to true.

Can anyone give me some advice, or perhaps point me to someone who already
solved this task?

Thanks,
Chris Hoeppner
www.pixware.org

Jeremy Dunck

unread,
May 15, 2007, 1:02:59 PM5/15/07
to django...@googlegroups.com
On 5/15/07, Christian M Hoeppner <ch...@pixware.org> wrote:
>
> Hi there!
>
> How would one handle the following tasks in a middleware?

Put this somewhere in your app:
======
class MaintenanceMiddleware(object):
def process_request(self, request):
from django.conf import settings
from django.http import HttpResponseRedirect

if settings.MAINTENANCE and not request.user.is_authenticated()
return HttpResponseRedirect("/maintenance/")
return None

=======

Then add that to your MIDDLEWARE_CLASSES list; make sure it's after
AuthenticationMiddleware so that request.user exists.

Request middleware short-circuits the request cycle if it returns a
HttpResponse instance.

More:
http://www.djangoproject.com/documentation/middleware/#process-request

lize...@gmail.com

unread,
May 15, 2007, 1:03:58 PM5/15/07
to django...@googlegroups.com
Something like this (i didn't check if this code works):

from django.conf import settings
from django.http import HttpResponseRedirect

class MaintenanceMiddleware(object):
def process_request(self, request):

if settings.MAINTENANCE:
if not request.user.is_authenticated():


return HttpResponseRedirect('/maintenance/')
return None

Put this middleware after AuthenticationMiddleware in the settings.py

Christian M Hoeppner

unread,
May 15, 2007, 1:46:15 PM5/15/07
to django...@googlegroups.com
> Something like this (i didn't check if this code works):
>
> from django.conf import settings
> from django.http import HttpResponseRedirect
>
> class MaintenanceMiddleware(object):
> def process_request(self, request):
> if settings.MAINTENANCE:
> if not request.user.is_authenticated():
> return HttpResponseRedirect('/maintenance/')
> return None
>
> Put this middleware after AuthenticationMiddleware in the settings.py

Of course, this would work, but it also prevents users not logged in from
logging in. Is there a way to check if the user is accessing the login view,
in order to let him access his account and the site being under maintenance?

Thanks,
Chris Hoeppner
www.pixware.org

Jeremy Dunck

unread,
May 15, 2007, 3:17:28 PM5/15/07
to django...@googlegroups.com
On 5/15/07, Christian M Hoeppner <ch...@pixware.org> wrote:
...

> Of course, this would work, but it also prevents users not logged in from
> logging in. Is there a way to check if the user is accessing the login view,
> in order to let him access his account and the site being under maintenance?

You're more likely to get working code if all the requirements are stated. :)

Set these settings:
LOGIN_REDIRECT_URL
LOGIN_URL
LOGOUT_URL

Then the middleware would be:

class MaintenanceMiddleware(object):
def process_request(self, request):
from django.conf import settings
from django.http import HttpResponseRedirect


is_login = request.path in (
settings.LOGIN_REDIRECT_URL,
settings.LOGIN_URL,
settings.LOGOUT_URL)
if ((not is_login) and
settings.MAINTENANCE and
(not request.user.is_authenticated())):


return HttpResponseRedirect("/maintenance/")
return None

More:
http://www.djangoproject.com/documentation/request_response/#attributes
http://www.djangoproject.com/documentation/settings/#login-redirect-url

Note that those settings are new in the Django dev version (after
0.96). Of course you could add them to your own settings file, same
as MAINTENANCE.

Christian M Hoeppner

unread,
May 16, 2007, 5:57:46 AM5/16/07
to django...@googlegroups.com
> You're more likely to get working code if all the requirements are stated.
> :)

Yeah, you're right, Jeremy. Thank you a lot for your help.

I wonder... There isn't a way in a middleware to access some name path from
the urlconf, is there? That way I wouldn't have to set those settings, by
letting the admin-path bypass the maintenance middleware.

Jeremy Dunck

unread,
May 16, 2007, 9:26:00 AM5/16/07
to django...@googlegroups.com
On 5/16/07, Christian M Hoeppner <ch...@pixware.org> wrote:
...
> I wonder... There isn't a way in a middleware to access some name path from
> the urlconf, is there? That way I wouldn't have to set those settings, by
> letting the admin-path bypass the maintenance middleware.

Not as far as I can see now, but it does seem like a good idea.

New ticket:
http://code.djangoproject.com/ticket/4311

Christian M Hoeppner

unread,
May 16, 2007, 11:58:23 AM5/16/07
to django...@googlegroups.com
Just for the record:

** settings.py in $project_path:

# Maintenance Mode Switch
MAINTENANCE = True

# Login paths
LOGIN_REDIRECT_URL = '/admin/'
LOGIN_URL = '/admin/'
LOGOUT_URL = '/admin/logout/'

# This will be shown to unauthorised users when the site is in maintenance
mode
MAINTENANCE_PATH = '/maintenance/'

** middleware.py in $project_path:

class MaintenanceMiddleware(object):
def process_request(self, request):
from django.conf import settings
from django.http import HttpResponseRedirect


is_login = request.path in (
settings.LOGIN_REDIRECT_URL,
settings.LOGIN_URL,

settings.LOGOUT_URL,
settings.MAINTENANCE_PATH,


)
if ((not is_login) and settings.MAINTENANCE and (not
request.user.is_authenticated())):

return HttpResponseRedirect(settings.MAINTENANCE_PATH)
return None

I have added the MAINTENANCE_PATH setting, and included it in the path check.
If this one is not checked to be false, we get an endless redirect loop.

I'll be posting this into my new blog (as soon as I get it up) and if you like
it and find it usefull, you might also tell me to post it at Django Snippets.

Thank you everyone!

Chris Hoeppner
www.pixware.org

Reply all
Reply to author
Forward
0 new messages