server/src/metaeducation/middleware.py
author durandn
Thu, 10 Mar 2016 13:32:18 +0100
changeset 15 8004d8fc9b38
parent 4 8bc8b208441d
child 27 c3c6f2f85680
permissions -rw-r--r--
Corrected settings to support deployment in a subdirectory + protected front_list and front_delete so only staff users can access it + added version display in front_list

from django.shortcuts import redirect
from django.conf import settings
from urllib.parse import urlencode
from re import compile

EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'OAUTH_EXEMPT_URLS'):
    EXEMPT_URLS += [compile(expr) for expr in settings.OAUTH_EXEMPT_URLS]

class MtdcLoginRequiredWithContextMiddleware:
    """
    Middleware intended to emulate login_required decorator so we can forward the context query arg
    """
    def process_request(self, request):
        if not request.user.is_authenticated():
            path = request.path_info.lstrip('/')
            if not any(m.match(path) for m in EXEMPT_URLS):
                if request.GET.get("context", ""):
                    context = request.GET["context"]
                    response = redirect(settings.LOGIN_URL)
                    print(path)
                    response["LOCATION"] += "?"+urlencode({"context": context, "next": settings.URL_SUBDIRECTORY+"/"+path})
                    return response