From 93970f347347d930f49508a7d35de9dbca8bb408 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Tue, 24 May 2016 13:12:34 -0400 Subject: [PATCH] Allow edxmako to be passed an explicit request to create the RequestContext from --- cms/djangoapps/contentstore/views/error.py | 4 +-- common/djangoapps/edxmako/request_context.py | 14 ++++++---- common/djangoapps/edxmako/shortcuts.py | 29 +++++++++++++++++--- lms/djangoapps/static_template_view/views.py | 4 +-- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/cms/djangoapps/contentstore/views/error.py b/cms/djangoapps/contentstore/views/error.py index 2eea0bc2a7..6f58ea296d 100644 --- a/cms/djangoapps/contentstore/views/error.py +++ b/cms/djangoapps/contentstore/views/error.py @@ -39,9 +39,9 @@ def server_error(request): @jsonable_error(404, "Resource not found") def render_404(request): - return HttpResponseNotFound(render_to_string('404.html', {})) + return HttpResponseNotFound(render_to_string('404.html', {}, request=request)) @jsonable_error(500, "The Studio servers encountered an error") def render_500(request): - return HttpResponseServerError(render_to_string('500.html', {})) + return HttpResponseServerError(render_to_string('500.html', {}, request=request)) diff --git a/common/djangoapps/edxmako/request_context.py b/common/djangoapps/edxmako/request_context.py index 900836eede..b10b8753ae 100644 --- a/common/djangoapps/edxmako/request_context.py +++ b/common/djangoapps/edxmako/request_context.py @@ -38,23 +38,25 @@ def get_template_context_processors(): return tuple(import_string(path) for path in context_processors) -def get_template_request_context(): +def get_template_request_context(request=None): """ Returns the template processing context to use for the current request, or returns None if there is not a current request. """ + if request is None: + request = get_current_request() + + if request is None: + return None + request_cache_dict = request_cache.get_cache('edxmako') cache_key = "request_context" if cache_key in request_cache_dict: return request_cache_dict[cache_key] - request = get_current_request() - - if request is None: - return None - context = RequestContext(request) + context['is_secure'] = request.is_secure() context['site'] = safe_get_host(request) diff --git a/common/djangoapps/edxmako/shortcuts.py b/common/djangoapps/edxmako/shortcuts.py index 4d6d56deb0..94cb1cdd01 100644 --- a/common/djangoapps/edxmako/shortcuts.py +++ b/common/djangoapps/edxmako/shortcuts.py @@ -111,7 +111,28 @@ def microsite_footer_context_processor(request): ) -def render_to_string(template_name, dictionary, context=None, namespace='main'): +def render_to_string(template_name, dictionary, context=None, namespace='main', request=None): + """ + Render a Mako template to as a string. + + The following values are available to all templates: + settings: the django settings object + EDX_ROOT_URL: settings.EDX_ROOT_URL + marketing_link: The :func:`marketing_link` function + is_any_marketing_link_set: The :func:`is_any_marketing_link_set` function + is_marketing_link_set: The :func:`is_marketing_link_set` function + + Arguments: + template_name: The name of the template to render. Will be loaded + from the template paths specified in configuration. + dictionary: A dictionary of variables to insert into the template during + rendering. + context: A :class:`~django.template.Context` with values to make + available to the template. + namespace: The Mako namespace to find the named template in. + request: The request to use to construct the RequestContext for rendering + this template. If not supplied, the current request will be used. + """ # see if there is an override template defined in the microsite template_name = microsite.get_template_path(template_name) @@ -128,7 +149,7 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'): context_instance['is_marketing_link_set'] = is_marketing_link_set # In various testing contexts, there might not be a current request context. - request_context = get_template_request_context() + request_context = get_template_request_context(request) if request_context: for item in request_context: context_dictionary.update(item) @@ -148,11 +169,11 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'): return template.render_unicode(**context_dictionary) -def render_to_response(template_name, dictionary=None, context_instance=None, namespace='main', **kwargs): +def render_to_response(template_name, dictionary=None, context_instance=None, namespace='main', request=None, **kwargs): """ Returns a HttpResponse whose content is filled with the result of calling lookup.get_template(args[0]).render with the passed arguments. """ dictionary = dictionary or {} - return HttpResponse(render_to_string(template_name, dictionary, context_instance, namespace), **kwargs) + return HttpResponse(render_to_string(template_name, dictionary, context_instance, namespace, request), **kwargs) diff --git a/lms/djangoapps/static_template_view/views.py b/lms/djangoapps/static_template_view/views.py index 3873a64d93..e22eca1dc9 100644 --- a/lms/djangoapps/static_template_view/views.py +++ b/lms/djangoapps/static_template_view/views.py @@ -71,8 +71,8 @@ def render_press_release(request, slug): def render_404(request): - return HttpResponseNotFound(render_to_string('static_templates/404.html', {})) + return HttpResponseNotFound(render_to_string('static_templates/404.html', {}, request=request)) def render_500(request): - return HttpResponseServerError(render_to_string('static_templates/server-error.html', {})) + return HttpResponseServerError(render_to_string('static_templates/server-error.html', {}, request=request))