Make mitxmako available to both the lms and the cms
This commit is contained in:
committed by
Matthew Mongeau
parent
3606ddbfdb
commit
e845221f4a
@@ -20,10 +20,10 @@ from django.conf import settings
|
||||
requestcontext = None
|
||||
lookup = {}
|
||||
|
||||
|
||||
class MakoMiddleware(object):
|
||||
def __init__(self):
|
||||
"""Setup mako variables and lookup object"""
|
||||
from django.conf import settings
|
||||
# Set all mako variables based on django settings
|
||||
template_locations = settings.MAKO_TEMPLATES
|
||||
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
|
||||
@@ -32,17 +32,17 @@ class MakoMiddleware(object):
|
||||
module_directory = tempfile.mkdtemp()
|
||||
|
||||
for location in template_locations:
|
||||
lookup[location] = TemplateLookup(directories=template_locations[location],
|
||||
lookup[location] = TemplateLookup(directories=template_locations[location],
|
||||
module_directory=module_directory,
|
||||
output_encoding='utf-8',
|
||||
input_encoding='utf-8',
|
||||
output_encoding='utf-8',
|
||||
input_encoding='utf-8',
|
||||
encoding_errors='replace',
|
||||
)
|
||||
|
||||
import mitxmako
|
||||
mitxmako.lookup = lookup
|
||||
|
||||
def process_request (self, request):
|
||||
def process_request(self, request):
|
||||
global requestcontext
|
||||
requestcontext = RequestContext(request)
|
||||
requestcontext['is_secure'] = request.is_secure()
|
||||
@@ -15,10 +15,9 @@
|
||||
from django.template import Context
|
||||
from django.http import HttpResponse
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
from . import middleware
|
||||
from django.conf import settings
|
||||
|
||||
import mitxmako.middleware
|
||||
|
||||
def render_to_string(template_name, dictionary, context=None, namespace='main'):
|
||||
context_instance = Context(dictionary)
|
||||
@@ -28,15 +27,12 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'):
|
||||
context_dictionary = {}
|
||||
context_instance['settings'] = settings
|
||||
context_instance['MITX_ROOT_URL'] = settings.MITX_ROOT_URL
|
||||
for d in mitxmako.middleware.requestcontext:
|
||||
for d in middleware.requestcontext:
|
||||
context_dictionary.update(d)
|
||||
for d in context_instance:
|
||||
context_dictionary.update(d)
|
||||
if context:
|
||||
if context:
|
||||
context_dictionary.update(context)
|
||||
## HACK
|
||||
## We should remove this, and possible set COURSE_TITLE in the middleware from the session.
|
||||
if 'COURSE_TITLE' not in context_dictionary: context_dictionary['COURSE_TITLE'] = ''
|
||||
# fetch and render template
|
||||
template = middleware.lookup[namespace].get_template(template_name)
|
||||
return template.render(**context_dictionary)
|
||||
@@ -14,10 +14,11 @@
|
||||
|
||||
from mako.template import Template as MakoTemplate
|
||||
|
||||
import middleware
|
||||
from . import middleware
|
||||
|
||||
django_variables = ['lookup', 'template_dirs', 'output_encoding',
|
||||
'module_directory', 'encoding_errors']
|
||||
|
||||
django_variables = ['lookup', 'template_dirs', 'output_encoding',
|
||||
'module_directory', 'encoding_errors',]
|
||||
|
||||
class Template(MakoTemplate):
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -25,4 +26,4 @@ class Template(MakoTemplate):
|
||||
if not kwargs.get('no_django', False):
|
||||
overrides = dict([(k, getattr(middleware, k, None),) for k in django_variables])
|
||||
kwargs.update(overrides)
|
||||
super(Template, self).__init__(*args, **kwargs)
|
||||
super(Template, self).__init__(*args, **kwargs)
|
||||
Reference in New Issue
Block a user