From 8174e4969081c388e01ad4a7767cc07b5c0a70d1 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Tue, 14 Feb 2012 11:49:47 -0500 Subject: [PATCH 1/4] echo change in 1a68a3047124 to remove outdated call to modules.get_module_class --HG-- branch : pmitros-mod-template --- courseware/capa/calc.py | 6 ++++++ courseware/module_render.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/courseware/capa/calc.py b/courseware/capa/calc.py index 41ced03e58..dc8653cf21 100644 --- a/courseware/capa/calc.py +++ b/courseware/capa/calc.py @@ -1,4 +1,5 @@ import copy +import logging import math import operator @@ -27,10 +28,15 @@ default_variables = {'j':numpy.complex(0,1), } +log = logging.getLogger("mitx.courseware.capa") + def evaluator(variables, functions, string): ''' Evaluate an expression. Variables are passed as a dictionary from string to value. Unary functions are passed as a dictionary from string to function ''' + log.debug(u"Evaluating: {0} with vars: {1}, funcs: {2}" + .format(string, variables, functions)) + all_variables = copy.copy(default_variables) all_variables.update(variables) all_functions = copy.copy(default_functions) diff --git a/courseware/module_render.py b/courseware/module_render.py index 1f4e784b2e..bf62585d65 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -60,10 +60,10 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): ajax_url = '/modx/'+module+'/'+id+'/' - id_tag=courseware.modules.get_module_class(module.id_attribute) + # id_tag=courseware.modules.get_module_class(module) # Grab the XML corresponding to the request from course.xml - xml = content_parser.module_xml(content_parser.course_file(request.user), module, id_tag, id) + xml = content_parser.module_xml(content_parser.course_file(request.user), module, 'id', id) # Create the module instance=courseware.modules.get_module_class(module)(xml, From 0d9a24e4f4048580a7c1df2c750bd8ec11606f6e Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sun, 19 Feb 2012 20:12:34 -0500 Subject: [PATCH 2/4] Templates broken into namespaces --HG-- branch : pmitros-mod-template --- courseware/content_parser.py | 9 ++------- courseware/modules/html_module.py | 2 +- courseware/modules/template_module.py | 2 +- mitxmako/middleware.py | 22 ++++++++++------------ mitxmako/shortcuts.py | 8 ++++---- settings_new_askbot.py | 16 +++++++++++++--- 6 files changed, 31 insertions(+), 28 deletions(-) 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..ff548dea34 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('custom_tags/'+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..6efecdaf48 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,16 @@ ASKBOT_CSS_DEVEL = True BROKER_TRANSPORT = "djkombu.transport.DatabaseTransport" CELERY_ALWAYS_EAGER = True +ot = MAKO_TEMPLATES +MAKO_TEMPLATES['course'] = [DATA_DIR] +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() From 1befa20b1260236ea6f79a655e5261311d679be0 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Mon, 20 Feb 2012 07:08:22 -0500 Subject: [PATCH 3/4] Last commit tested and debugged --HG-- branch : pmitros-mod-template --- courseware/modules/template_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/courseware/modules/template_module.py b/courseware/modules/template_module.py index ff548dea34..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, namespace = 'custom_tags') + self.html = render_to_string(filename, params, namespace = 'custom_tags') From e57fc19045bb9488ca010a3f37d986bb076d852b Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Mon, 20 Feb 2012 07:16:22 -0500 Subject: [PATCH 4/4] Including namespace for sections --HG-- branch : pmitros-mod-template --- settings_new_askbot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/settings_new_askbot.py b/settings_new_askbot.py index 6efecdaf48..496e1ad79c 100644 --- a/settings_new_askbot.py +++ b/settings_new_askbot.py @@ -389,6 +389,7 @@ 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/']