server/src/metaeducation/middleware.py
author durandn
Wed, 02 Mar 2016 10:45:20 +0100
changeset 8 e56471e269eb
parent 4 8bc8b208441d
child 15 8004d8fc9b38
permissions -rw-r--r--
small correction on signals
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     1
from django.shortcuts import redirect
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     2
from django.conf import settings
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     3
from urllib.parse import urlencode
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     4
from re import compile
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     5
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     6
EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))]
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     7
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     8
    EXEMPT_URLS += [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS]
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     9
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    10
class MtdcLoginRequiredWithContextMiddleware:
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    11
    """
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    12
    Middleware intended to emulate login_required decorator so we can forward the context query arg
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    13
    """
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    14
    def process_request(self, request):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    15
        if not request.user.is_authenticated():
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    16
            path = request.path_info.lstrip('/')
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    17
            if not any(m.match(path) for m in EXEMPT_URLS):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    18
                if request.GET.get("context", ""):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    19
                    context = request.GET["context"]
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    20
                    response = redirect(settings.LOGIN_URL)
4
8bc8b208441d corrected middleware to allow proper redirects
durandn
parents: 1
diff changeset
    21
                    response["LOCATION"] += "?"+urlencode({"context": context, "next": "/"+path})
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    22
                    return response