Return to Snippet

Revision: 55326
at February 2, 2012 19:25 by lukasvinclav

Initial Code
from django.conf import settings
from django.shortcuts import redirect
from re import compile

EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))]

if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS]
class AccountsMiddleware():
    def process_request(self, request):
        if request.user.is_anonymous():
            path = request.path_info.lstrip('/')
            if not any(m.match(path) for m in EXEMPT_URLS):
                return redirect(settings.LOGIN_URL)

Initial URL

Initial Description
Django's middleware for global app login. With this middleware you don't have to decorate views by login_required decorator.

Initial Title
Django global login middleware

Initial Tags
python, django

Initial Language