Commit c16ac4fb by Piotr Mitros

Merge with local template paths

parents ffba3c59 e57fc190
...@@ -5,13 +5,12 @@ import re ...@@ -5,13 +5,12 @@ import re
from datetime import timedelta from datetime import timedelta
from lxml import etree from lxml import etree
from mako.template import Template
from mako.lookup import TemplateLookup
try: # This lets us do __name__ == ='__main__' try: # This lets us do __name__ == ='__main__'
from django.conf import settings from django.conf import settings
from student.models import UserProfile from student.models import UserProfile
from student.models import UserTestGroup from student.models import UserTestGroup
from mitxmako.shortcuts import render_to_response, render_to_string
except: except:
settings = None settings = None
...@@ -142,13 +141,9 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None): ...@@ -142,13 +141,9 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None):
#to its children later. #to its children later.
return return
template_lookup = TemplateLookup(directories = [settings.DATA_DIR],
module_directory = settings.MAKO_MODULE_DIR)
def course_file(user): def course_file(user):
# TODO: Cache. # TODO: Cache.
filename = UserProfile.objects.get(user=user).courseware filename = UserProfile.objects.get(user=user).courseware
data_template = template_lookup.get_template(filename)
# TODO: Rewrite in Django # 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])] 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): ...@@ -156,7 +151,7 @@ def course_file(user):
options = {'dev_content':settings.DEV_CONTENT, options = {'dev_content':settings.DEV_CONTENT,
'groups' : groups} 'groups' : groups}
tree = etree.XML(data_template.render(**options)) tree = etree.XML(render_to_string(filename, options, namespace = 'course'))
id_tag(tree) id_tag(tree)
propogate_downward_tag(tree, "due") propogate_downward_tag(tree, "due")
propogate_downward_tag(tree, "graded") propogate_downward_tag(tree, "graded")
......
...@@ -24,7 +24,7 @@ class Module(XModule): ...@@ -24,7 +24,7 @@ class Module(XModule):
textlist=[i for i in textlist if type(i)==str] textlist=[i for i in textlist if type(i)==str]
return "".join(textlist) return "".join(textlist)
try: try:
filename=settings.DATA_DIR+"html/"+self.filename+".xml" filename=settings.DATA_DIR+"html/"+self.filename
return open(filename).read() return open(filename).read()
except: # For backwards compatibility. TODO: Remove except: # For backwards compatibility. TODO: Remove
return render_to_string(self.filename, {'id': self.item_id}) return render_to_string(self.filename, {'id': self.item_id})
......
...@@ -26,4 +26,4 @@ class Module(XModule): ...@@ -26,4 +26,4 @@ class Module(XModule):
filename = xmltree.tag filename = xmltree.tag
params = dict(xmltree.items()) params = dict(xmltree.items())
# print params # print params
self.html = render_to_string('custom_tags/'+filename, params) self.html = render_to_string(filename, params, namespace = 'custom_tags')
...@@ -17,33 +17,31 @@ import tempfile ...@@ -17,33 +17,31 @@ import tempfile
from django.template import RequestContext from django.template import RequestContext
requestcontext = None requestcontext = None
lookup = None 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 from django.conf import settings
# Set all mako variables based on django settings # Set all mako variables based on django settings
global template_dirs, output_encoding, module_directory, encoding_errors template_locations = settings.MAKO_TEMPLATES
directories = getattr(settings, 'MAKO_TEMPLATE_DIRS', settings.TEMPLATE_DIRS)
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None) module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
if module_directory is None: if module_directory is None:
module_directory = tempfile.mkdtemp() module_directory = tempfile.mkdtemp()
output_encoding = getattr(settings, 'MAKO_OUTPUT_ENCODING', 'utf-8') for location in template_locations:
encoding_errors = getattr(settings, 'MAKO_ENCODING_ERRORS', 'replace') lookup[location] = TemplateLookup(directories=template_locations[location],
global lookup
lookup = TemplateLookup(directories=directories,
module_directory=module_directory, module_directory=module_directory,
output_encoding=output_encoding, output_encoding='utf-8',
encoding_errors=encoding_errors, input_encoding='utf-8',
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)
# print requestcontext
...@@ -20,7 +20,7 @@ from django.conf import settings ...@@ -20,7 +20,7 @@ from django.conf import settings
from mitxmako.middleware import requestcontext 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) context_instance = context_instance or Context(dictionary)
# add dictionary to context_instance # add dictionary to context_instance
context_instance.update(dictionary or {}) context_instance.update(dictionary or {})
...@@ -31,12 +31,12 @@ def render_to_string(template_name, dictionary, context_instance=None): ...@@ -31,12 +31,12 @@ def render_to_string(template_name, dictionary, context_instance=None):
for d in context_instance: for d in context_instance:
context_dictionary.update(d) context_dictionary.update(d)
# fetch and render template # fetch and render template
template = middleware.lookup.get_template(template_name) template = middleware.lookup[namespace].get_template(template_name)
return template.render(**context_dictionary) 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 Returns a HttpResponse whose content is filled with the result of calling
lookup.get_template(args[0]).render with the passed arguments. 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 ...@@ -148,12 +148,11 @@ MAXLOG = 500
LOG_DIR = "/tmp/" LOG_DIR = "/tmp/"
MAKO_MODULE_DIR = None MAKO_MODULE_DIR = None
MAKO_TEMPLATES = {}
# Make sure we execute correctly regardless of where we're called from # Make sure we execute correctly regardless of where we're called from
execfile(os.path.join(BASE_DIR, "settings.py")) 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 # A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to # performed by this configuration is to send an email to
# the site admins on every HTTP 500 error. # the site admins on every HTTP 500 error.
...@@ -388,5 +387,17 @@ ASKBOT_CSS_DEVEL = True ...@@ -388,5 +387,17 @@ ASKBOT_CSS_DEVEL = True
BROKER_TRANSPORT = "djkombu.transport.DatabaseTransport" BROKER_TRANSPORT = "djkombu.transport.DatabaseTransport"
CELERY_ALWAYS_EAGER = True 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() 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