Commit 3c07c2b1 by Calen Pennington Committed by Matthew Mongeau

Make mitxmako available to both the lms and the cms

parent 2f799360
...@@ -20,10 +20,10 @@ from django.conf import settings ...@@ -20,10 +20,10 @@ from django.conf import settings
requestcontext = None requestcontext = None
lookup = {} lookup = {}
class MakoMiddleware(object): class MakoMiddleware(object):
def __init__(self): def __init__(self):
"""Setup mako variables and lookup object""" """Setup mako variables and lookup object"""
from django.conf import settings
# Set all mako variables based on django settings # Set all mako variables based on django settings
template_locations = settings.MAKO_TEMPLATES template_locations = settings.MAKO_TEMPLATES
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None) module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
...@@ -32,17 +32,17 @@ class MakoMiddleware(object): ...@@ -32,17 +32,17 @@ class MakoMiddleware(object):
module_directory = tempfile.mkdtemp() module_directory = tempfile.mkdtemp()
for location in template_locations: for location in template_locations:
lookup[location] = TemplateLookup(directories=template_locations[location], lookup[location] = TemplateLookup(directories=template_locations[location],
module_directory=module_directory, module_directory=module_directory,
output_encoding='utf-8', output_encoding='utf-8',
input_encoding='utf-8', input_encoding='utf-8',
encoding_errors='replace', encoding_errors='replace',
) )
import mitxmako import mitxmako
mitxmako.lookup = lookup mitxmako.lookup = lookup
def process_request (self, request): def process_request(self, request):
global requestcontext global requestcontext
requestcontext = RequestContext(request) requestcontext = RequestContext(request)
requestcontext['is_secure'] = request.is_secure() requestcontext['is_secure'] = request.is_secure()
......
...@@ -15,10 +15,9 @@ ...@@ -15,10 +15,9 @@
from django.template import Context from django.template import Context
from django.http import HttpResponse from django.http import HttpResponse
import mitxmako.middleware as middleware from . import middleware
from django.conf import settings from django.conf import settings
import mitxmako.middleware
def render_to_string(template_name, dictionary, context=None, namespace='main'): def render_to_string(template_name, dictionary, context=None, namespace='main'):
context_instance = Context(dictionary) context_instance = Context(dictionary)
...@@ -28,15 +27,12 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'): ...@@ -28,15 +27,12 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'):
context_dictionary = {} context_dictionary = {}
context_instance['settings'] = settings context_instance['settings'] = settings
context_instance['MITX_ROOT_URL'] = settings.MITX_ROOT_URL context_instance['MITX_ROOT_URL'] = settings.MITX_ROOT_URL
for d in mitxmako.middleware.requestcontext: for d in middleware.requestcontext:
context_dictionary.update(d) context_dictionary.update(d)
for d in context_instance: for d in context_instance:
context_dictionary.update(d) context_dictionary.update(d)
if context: if context:
context_dictionary.update(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 # fetch and render template
template = middleware.lookup[namespace].get_template(template_name) template = middleware.lookup[namespace].get_template(template_name)
return template.render(**context_dictionary) return template.render(**context_dictionary)
......
...@@ -14,10 +14,11 @@ ...@@ -14,10 +14,11 @@
from mako.template import Template as MakoTemplate 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): class Template(MakoTemplate):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
...@@ -25,4 +26,4 @@ class Template(MakoTemplate): ...@@ -25,4 +26,4 @@ class Template(MakoTemplate):
if not kwargs.get('no_django', False): if not kwargs.get('no_django', False):
overrides = dict([(k, getattr(middleware, k, None),) for k in django_variables]) overrides = dict([(k, getattr(middleware, k, None),) for k in django_variables])
kwargs.update(overrides) kwargs.update(overrides)
super(Template, self).__init__(*args, **kwargs) super(Template, self).__init__(*args, **kwargs)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment