server/src/metaeducation/middleware.py
author durandn
Mon, 14 Mar 2016 15:08:25 +0100
changeset 35 e82a0ac6cc2c
parent 32 eb9e83610c99
child 63 6bfac7c633a0
permissions -rw-r--r--
used '%r' and removed '+str()' in logger.debug() calls
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     1
import logging
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     2
from re import compile
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     3
from urllib.parse import urlencode
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     4
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     5
from django.conf import settings
32
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     6
from django.shortcuts import redirect
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     7
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
     8
logger = logging.getLogger(__name__)
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     9
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    10
EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))]
15
8004d8fc9b38 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
durandn
parents: 4
diff changeset
    11
if hasattr(settings, 'OAUTH_EXEMPT_URLS'):
8004d8fc9b38 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
durandn
parents: 4
diff changeset
    12
    EXEMPT_URLS += [compile(expr) for expr in settings.OAUTH_EXEMPT_URLS]
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    13
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    14
class MtdcLoginRequiredWithContextMiddleware:
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    15
    """
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    16
    Middleware intended to emulate login_required decorator so we can forward the context query arg
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    17
    """
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    18
    def process_request(self, request):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    19
        if not request.user.is_authenticated():
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    20
            path = request.path_info.lstrip('/')
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    21
            if not any(m.match(path) for m in EXEMPT_URLS):
32
eb9e83610c99 added logging and logs config
durandn
parents: 27
diff changeset
    22
                logger.debug("LOGIN_REQUIRED: User is not logged and this request triggered Oauth redirects")
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    23
                if request.GET.get("context", ""):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    24
                    context = request.GET["context"]
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    25
                    response = redirect(settings.LOGIN_URL)
35
e82a0ac6cc2c used '%r' and removed '+str()' in logger.debug() calls
durandn
parents: 32
diff changeset
    26
                    logger.debug("LOGIN_REQUIRED: will redirect to %r", settings.LOGIN_URL)
e82a0ac6cc2c used '%r' and removed '+str()' in logger.debug() calls
durandn
parents: 32
diff changeset
    27
                    logger.debug("LOGIN_REQUIRED: query args will be %r", {"context": context, "next": settings.BASE_URL+path})
27
c3c6f2f85680 use BASE_URL in middleware + increment version
durandn
parents: 15
diff changeset
    28
                    response["LOCATION"] += "?"+urlencode({"context": context, "next": settings.BASE_URL+path})
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    29
                    return response