Allow edxmako to be passed an explicit request to create the RequestContext from
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user