From 3b4fb616488a1eab7b3d135d26bf4b0d1ffcdc0d Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 2 Jul 2012 14:10:29 -0400 Subject: [PATCH] Push dependency on mitxmako up out of mako_module --- common/lib/xmodule/xmodule/mako_module.py | 16 +++++++++++++--- common/lib/xmodule/xmodule/modulestore/mongo.py | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/common/lib/xmodule/xmodule/mako_module.py b/common/lib/xmodule/xmodule/mako_module.py index 2260dddd92..9a90afb896 100644 --- a/common/lib/xmodule/xmodule/mako_module.py +++ b/common/lib/xmodule/xmodule/mako_module.py @@ -1,5 +1,10 @@ -from x_module import XModuleDescriptor -from mitxmako.shortcuts import render_to_string +from x_module import XModuleDescriptor, DescriptorSystem + + +class MakoDescriptorSystem(DescriptorSystem): + def __init__(self, render_template, *args, **kwargs): + self.render_template = render_template + super(MakoDescriptorSystem, self).__init__(*args, **kwargs) class MakoModuleDescriptor(XModuleDescriptor): @@ -12,6 +17,11 @@ class MakoModuleDescriptor(XModuleDescriptor): the descriptor as the `module` parameter to that template """ + def __init__(self, system, definition=None, **kwargs): + if getattr(system, 'render_template', None) is None: + raise TypeError('{system} must have a render_template function in order to use a MakoDescriptor'.format(system=system)) + super(MakoModuleDescriptor, self).__init__(system, definition, **kwargs) + def get_context(self): """ Return the context to render the mako template with @@ -19,4 +29,4 @@ class MakoModuleDescriptor(XModuleDescriptor): return {'module': self} def get_html(self): - return render_to_string(self.mako_template, self.get_context()) + return self.system.render_template(self.mako_template, self.get_context()) diff --git a/common/lib/xmodule/xmodule/modulestore/mongo.py b/common/lib/xmodule/xmodule/modulestore/mongo.py index f305f8aa97..cc731c929c 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo.py @@ -1,6 +1,8 @@ import pymongo from importlib import import_module -from xmodule.x_module import XModuleDescriptor, DescriptorSystem +from xmodule.x_module import XModuleDescriptor +from xmodule.mako_module import MakoDescriptorSystem +from mitxmako.shortcuts import render_to_string from . import ModuleStore, Location from .exceptions import ItemNotFoundError, InsufficientSpecificationError @@ -54,7 +56,7 @@ class MongoModuleStore(ModuleStore): # TODO (cpennington): Pass a proper resources_fs to the system return XModuleDescriptor.load_from_json( - item, DescriptorSystem(self.get_item, None), self.default_class) + item, MakoDescriptorSystem(load_item=self.get_item, resources_fs=None, render_template=render_to_string), self.default_class) def create_item(self, location): """