request_context.py 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9
#   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
10
#   distributed under the License is distributed on an "AS IS" BASIS,
11 12 13
#   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.
14 15 16 17 18 19 20
#
# This file has been modified by edX.org

"""
Methods for creating RequestContext for using with Mako templates.
"""

21

22
from crum import get_current_request
Piotr Mitros committed
23
from django.template import RequestContext
24

25
import request_cache
26
from util.request import safe_get_host
27 28


29
def get_template_request_context(request=None):
30 31 32 33
    """
    Returns the template processing context to use for the current request,
    or returns None if there is not a current request.
    """
34

35 36 37 38 39 40
    if request is None:
        request = get_current_request()

    if request is None:
        return None

41
    request_cache_dict = request_cache.get_cache('edxmako')
42
    cache_key = "request_context"
43 44 45
    if cache_key in request_cache_dict:
        return request_cache_dict[cache_key]

46
    context = RequestContext(request)
47

48 49
    context['is_secure'] = request.is_secure()
    context['site'] = safe_get_host(request)
50

51 52
    request_cache_dict[cache_key] = context

53
    return context