diff --git a/courseware/content_parser.py b/courseware/content_parser.py index 61e65caf71..7fb7a8a298 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -5,13 +5,12 @@ import re from datetime import timedelta from lxml import etree -from mako.template import Template -from mako.lookup import TemplateLookup try: # This lets us do __name__ == ='__main__' from django.conf import settings from student.models import UserProfile from student.models import UserTestGroup + from mitxmako.shortcuts import render_to_response, render_to_string except: settings = None @@ -142,13 +141,9 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None): #to its children later. return -template_lookup = TemplateLookup(directories = [settings.DATA_DIR], - module_directory = settings.MAKO_MODULE_DIR) - def course_file(user): # TODO: Cache. filename = UserProfile.objects.get(user=user).courseware - data_template = template_lookup.get_template(filename) # TODO: Rewrite in Django groups = [u.name for u in UserTestGroup.objects.raw("select * from auth_user, student_usertestgroup, student_usertestgroup_users where auth_user.id = student_usertestgroup_users.user_id and student_usertestgroup_users.usertestgroup_id = student_usertestgroup.id and auth_user.id = %s", [user.id])] @@ -156,7 +151,7 @@ def course_file(user): options = {'dev_content':settings.DEV_CONTENT, 'groups' : groups} - tree = etree.XML(data_template.render(**options)) + tree = etree.XML(render_to_string(filename, options, namespace = 'course')) id_tag(tree) propogate_downward_tag(tree, "due") propogate_downward_tag(tree, "graded") diff --git a/courseware/modules/html_module.py b/courseware/modules/html_module.py index 92e29df938..4194f73e74 100644 --- a/courseware/modules/html_module.py +++ b/courseware/modules/html_module.py @@ -24,7 +24,7 @@ class Module(XModule): textlist=[i for i in textlist if type(i)==str] return "".join(textlist) try: - filename=settings.DATA_DIR+"html/"+self.filename+".xml" + filename=settings.DATA_DIR+"html/"+self.filename return open(filename).read() except: # For backwards compatibility. TODO: Remove return render_to_string(self.filename, {'id': self.item_id}) diff --git a/courseware/modules/template_module.py b/courseware/modules/template_module.py index 11778348e3..d9dbb613f0 100644 --- a/courseware/modules/template_module.py +++ b/courseware/modules/template_module.py @@ -26,4 +26,4 @@ class Module(XModule): filename = xmltree.tag params = dict(xmltree.items()) # print params - self.html = render_to_string('custom_tags/'+filename, params) + self.html = render_to_string(filename, params, namespace = 'custom_tags') diff --git a/mitxmako/middleware.py b/mitxmako/middleware.py index 481d1db672..857ae29541 100644 --- a/mitxmako/middleware.py +++ b/mitxmako/middleware.py @@ -17,33 +17,31 @@ import tempfile from django.template import RequestContext requestcontext = None -lookup = 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 - global template_dirs, output_encoding, module_directory, encoding_errors - directories = getattr(settings, 'MAKO_TEMPLATE_DIRS', settings.TEMPLATE_DIRS) - + template_locations = settings.MAKO_TEMPLATES module_directory = getattr(settings, 'MAKO_MODULE_DIR', None) + if module_directory is None: module_directory = tempfile.mkdtemp() - output_encoding = getattr(settings, 'MAKO_OUTPUT_ENCODING', 'utf-8') - encoding_errors = getattr(settings, 'MAKO_ENCODING_ERRORS', 'replace') - - global lookup - lookup = TemplateLookup(directories=directories, + for location in template_locations: + lookup[location] = TemplateLookup(directories=template_locations[location], module_directory=module_directory, - output_encoding=output_encoding, - encoding_errors=encoding_errors, + output_encoding='utf-8', + input_encoding='utf-8', + encoding_errors='replace', ) + import mitxmako mitxmako.lookup = lookup def process_request (self, request): global requestcontext requestcontext = RequestContext(request) -# print requestcontext + diff --git a/mitxmako/shortcuts.py b/mitxmako/shortcuts.py index 261cee5b41..862f8b96bb 100644 --- a/mitxmako/shortcuts.py +++ b/mitxmako/shortcuts.py @@ -20,7 +20,7 @@ from django.conf import settings from mitxmako.middleware import requestcontext -def render_to_string(template_name, dictionary, context_instance=None): +def render_to_string(template_name, dictionary, context_instance=None, namespace='main'): context_instance = context_instance or Context(dictionary) # add dictionary to context_instance context_instance.update(dictionary or {}) @@ -31,12 +31,12 @@ def render_to_string(template_name, dictionary, context_instance=None): for d in context_instance: context_dictionary.update(d) # fetch and render template - template = middleware.lookup.get_template(template_name) + template = middleware.lookup[namespace].get_template(template_name) return template.render(**context_dictionary) -def render_to_response(template_name, dictionary, context_instance=None, **kwargs): +def render_to_response(template_name, dictionary, context_instance=None, namespace='main', **kwargs): """ Returns a HttpResponse whose content is filled with the result of calling lookup.get_template(args[0]).render with the passed arguments. """ - return HttpResponse(render_to_string(template_name, dictionary, context_instance), **kwargs) + return HttpResponse(render_to_string(template_name, dictionary, context_instance, namespace), **kwargs) diff --git a/settings_new_askbot.py b/settings_new_askbot.py index 561abaef3a..496e1ad79c 100644 --- a/settings_new_askbot.py +++ b/settings_new_askbot.py @@ -148,12 +148,11 @@ MAXLOG = 500 LOG_DIR = "/tmp/" MAKO_MODULE_DIR = None +MAKO_TEMPLATES = {} + # Make sure we execute correctly regardless of where we're called from execfile(os.path.join(BASE_DIR, "settings.py")) -if MAKO_MODULE_DIR == None: - MAKO_MODULE_DIR = tempfile.mkdtemp('mako') - # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error. @@ -388,5 +387,17 @@ ASKBOT_CSS_DEVEL = True BROKER_TRANSPORT = "djkombu.transport.DatabaseTransport" CELERY_ALWAYS_EAGER = True +ot = MAKO_TEMPLATES +MAKO_TEMPLATES['course'] = [DATA_DIR] +MAKO_TEMPLATES['sections'] = [DATA_DIR+'/sections'] +MAKO_TEMPLATES['custom_tags'] = [DATA_DIR+'/custom_tags'] +MAKO_TEMPLATES['main'] = [BASE_DIR+'/templates/'] + + +MAKO_TEMPLATES.update(ot) + +if MAKO_MODULE_DIR == None: + MAKO_MODULE_DIR = tempfile.mkdtemp('mako') + djcelery.setup_loader()