middleware.py 3.18 KB
Newer Older
1
"""
2
This file implements the Middleware support for the Open edX platform.
3 4 5 6 7 8
A microsite enables the following features:

1) Mapping of sub-domain name to a 'brand', e.g. foo-university.edx.org
2) Present a landing page with a listing of courses that are specific to the 'brand'
3) Ability to swap out some branding elements in the website
"""
9 10

from django.conf import settings
11
from microsite_configuration import microsite
12 13


14
class MicrositeMiddleware(object):
15 16 17 18 19 20 21 22 23 24
    """
    Middleware class which will bind configuration information regarding 'microsites' on a per request basis.
    The actual configuration information is taken from Django settings information
    """

    def process_request(self, request):
        """
        Middleware entry point on every request processing. This will associate a request's domain name
        with a 'University' and any corresponding microsite configuration information
        """
25
        microsite.clear()
26 27 28

        domain = request.META.get('HTTP_HOST', None)

29
        microsite.set_by_domain(domain)
30 31 32 33 34 35 36

        return None

    def process_response(self, request, response):
        """
        Middleware entry point for request completion.
        """
37
        microsite.clear()
38
        return response
39 40


41
class MicrositeSessionCookieDomainMiddleware(object):
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    """
    Special case middleware which should be at the very end of the MIDDLEWARE list (so that it runs first
    on the process_response chain). This middleware will define a wrapper function for the set_cookie() function
    on the HttpResponse object, if the request is running in a middleware.

    This wrapped set_cookie will change the SESSION_COOKIE_DOMAIN setting so that the cookie can be bound to a
    fully customized URL.
    """

    def process_response(self, request, response):
        """
        Standard Middleware entry point
        """

        # See if we are running in a Microsite *AND* we have a custom SESSION_COOKIE_DOMAIN defined
        # in configuration
        if microsite.has_override_value('SESSION_COOKIE_DOMAIN'):

            # define wrapper function for the standard set_cookie()
            def _set_cookie_wrapper(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False):

                # only override if we are setting the cookie name to be the one the Django Session Middleware uses
                # as defined in settings.SESSION_COOKIE_NAME
                if key == settings.SESSION_COOKIE_NAME:
                    domain = microsite.get_value('SESSION_COOKIE_DOMAIN', domain)

                # then call down into the normal Django set_cookie method
                return response.set_cookie_wrapped_func(
                    key,
                    value,
                    max_age=max_age,
                    expires=expires,
                    path=path,
                    domain=domain,
                    secure=secure,
                    httponly=httponly
                )

            # then point the HttpResponse.set_cookie to point to the wrapper and keep
            # the original around
            response.set_cookie_wrapped_func = response.set_cookie
            response.set_cookie = _set_cookie_wrapper

        return response