~canonical-isd-hackers/canonical-identity-provider/flakes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""
Request processors return dictionaries to be merged into a
template context. Each function takes the request object as its only parameter
and returns a dictionary to add to the context.

These are referenced from the setting TEMPLATE_CONTEXT_PROCESSORS and used by
RequestContext.
"""

from django.conf import settings


import identityprovider.signed as signed
from identityprovider.branding import current_brand


def readonly(request):
    return {'readonly': settings.READ_ONLY_MODE}


def branding(request):
    return {
            'template_dir': current_brand.template_dir,
            'brand': current_brand,
            }


supported = [(x, settings.LANGUAGE_NAMES[x])
             for x in settings.SUPPORTED_LANGUAGES]

def i18n(request):
    return {'supported_languages': supported}

def detect_embedded(request):
    """Determine if this request should be rendered using the embedded theme.

    For now, just check if this request is associated to a particular
    trust root.
    """
    embedded_trust_root = getattr(settings, 'EMBEDDED_TRUST_ROOT', 'invalid')
    trust_root = None
    token = getattr(request, 'token', None)
    if token is not None:
        try:
            raw_orequest = request.session.get(token, None)
            orequest = signed.loads(raw_orequest, settings.SECRET_KEY)
            trust_root = orequest.trust_root
        except:
            pass

    result = {'embedded': embedded_trust_root == trust_root}
    return result

def google_analytics_id(request):
    """Adds the google analytics id to the context if it's present."""
    return {
        'google_analytics_id': getattr(settings, 'GOOGLE_ANALYTICS_ID', None),
        }