Commit 0d9a24e4 by Piotr Mitros

Templates broken into namespaces

--HG--
branch : pmitros-mod-template
parent 5fe68e3a
......@@ -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")
......
......@@ -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})
......
......@@ -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')
......@@ -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
......@@ -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)
......@@ -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()
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