shortcuts.py 5.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   Copyright (c) 2008 Mikeal Rogers
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from django.template import Context
from django.http import HttpResponse
17
import logging
18

19
from microsite_configuration import microsite
20

21
from edxmako import lookup_template
22
from edxmako.middleware import get_template_request_context
Piotr Mitros committed
23
from django.conf import settings
24
from django.core.urlresolvers import reverse
25
log = logging.getLogger(__name__)
Piotr Mitros committed
26

27

28 29 30 31 32 33 34 35 36
def marketing_link(name):
    """Returns the correct URL for a link to the marketing site
    depending on if the marketing site is enabled

    Since the marketing site is enabled by a setting, we have two
    possible URLs for certain links. This function is to decides
    which URL should be provided.
    """

37 38 39
    # link_map maps URLs from the marketing site to the old equivalent on
    # the Django site
    link_map = settings.MKTG_URL_LINK_MAP
40
    enable_mktg_site = microsite.get_value(
41 42 43 44 45
        'ENABLE_MKTG_SITE',
        settings.FEATURES.get('ENABLE_MKTG_SITE', False)
    )

    if enable_mktg_site and name in settings.MKTG_URLS:
46 47 48
        # special case for when we only want the root marketing URL
        if name == 'ROOT':
            return settings.MKTG_URLS.get('ROOT')
49
        return settings.MKTG_URLS.get('ROOT') + settings.MKTG_URLS.get(name)
50
    # only link to the old pages when the marketing site isn't on
51
    elif not enable_mktg_site and name in link_map:
52 53 54
        # don't try to reverse disabled marketing links
        if link_map[name] is not None:
            return reverse(link_map[name])
55
    else:
56
        log.warning("Cannot find corresponding link for name: {name}".format(name=name))
57
        return '#'
58

59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
def marketing_link_context_processor(request):
    """
    A django context processor to give templates access to marketing URLs

    Returns a dict whose keys are the marketing link names usable with the
    marketing_link method (e.g. 'ROOT', 'CONTACT', etc.) prefixed with
    'MKTG_URL_' and whose values are the corresponding URLs as computed by the
    marketing_link method.
    """
    return dict(
        [
            ("MKTG_URL_" + k, marketing_link(k))
            for k in (
                settings.MKTG_URL_LINK_MAP.viewkeys() |
                settings.MKTG_URLS.viewkeys()
            )
        ]
    )

79

80 81 82 83 84 85 86 87 88 89 90
def open_source_footer_context_processor(request):
    """
    Checks the site name to determine whether to use the edX.org footer or the Open Source Footer.
    """
    return dict(
        [
            ("IS_EDX_DOMAIN", settings.FEATURES.get('IS_EDX_DOMAIN', False))
        ]
    )


91 92 93 94 95 96 97 98 99 100
def microsite_footer_context_processor(request):
    """
    Checks the site name to determine whether to use the edX.org footer or the Open Source Footer.
    """
    return dict(
        [
            ("IS_REQUEST_IN_MICROSITE", microsite.is_request_in_microsite())
        ]
    )

101

102
def render_to_string(template_name, dictionary, context=None, namespace='main'):
103 104

    # see if there is an override template defined in the microsite
105
    template_name = microsite.get_template_path(template_name)
106

107
    context_instance = Context(dictionary)
108 109 110 111
    # add dictionary to context_instance
    context_instance.update(dictionary or {})
    # collapse context_instance to a single dictionary for mako
    context_dictionary = {}
Piotr Mitros committed
112
    context_instance['settings'] = settings
113
    context_instance['EDX_ROOT_URL'] = settings.EDX_ROOT_URL
114
    context_instance['marketing_link'] = marketing_link
115 116

    # In various testing contexts, there might not be a current request context.
117 118 119 120 121 122
    request_context = get_template_request_context()
    if request_context:
        for item in request_context:
            context_dictionary.update(item)
    for item in context_instance:
        context_dictionary.update(item)
123
    if context:
124
        context_dictionary.update(context)
125 126 127 128 129 130 131

    # "Fix" CSRF token by evaluating the lazy object
    KEY_CSRF_TOKENS = ('csrf_token', 'csrf')
    for key in KEY_CSRF_TOKENS:
        if key in context_dictionary:
            context_dictionary[key] = unicode(context_dictionary[key])

132
    # fetch and render template
133
    template = lookup_template(namespace, template_name)
134
    return template.render_unicode(**context_dictionary)
135

136

137
def render_to_response(template_name, dictionary=None, context_instance=None, namespace='main', **kwargs):
138 139 140 141
    """
    Returns a HttpResponse whose content is filled with the result of calling
    lookup.get_template(args[0]).render with the passed arguments.
    """
142 143

    # see if there is an override template defined in the microsite
144
    template_name = microsite.get_template_path(template_name)
145

146
    dictionary = dictionary or {}
147
    return HttpResponse(render_to_string(template_name, dictionary, context_instance, namespace), **kwargs)