From bf62c4b70e1541579da69d5c94ac8ab410149c49 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 27 Mar 2015 14:05:44 -0400 Subject: [PATCH] Extract a pure-XBlock version of MakoModuleDescriptor --- common/lib/xmodule/xmodule/mako_module.py | 35 +++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/common/lib/xmodule/xmodule/mako_module.py b/common/lib/xmodule/xmodule/mako_module.py index 7718cb341e..da9da9a31d 100644 --- a/common/lib/xmodule/xmodule/mako_module.py +++ b/common/lib/xmodule/xmodule/mako_module.py @@ -1,4 +1,9 @@ -from .x_module import XModuleDescriptor, DescriptorSystem +""" +Code to handle mako templating for XModules and XBlocks. +""" +from xblock.fragment import Fragment + +from .x_module import XModuleDescriptor, DescriptorSystem, shim_xmodule_js class MakoDescriptorSystem(DescriptorSystem): @@ -8,20 +13,19 @@ class MakoDescriptorSystem(DescriptorSystem): self.render_template = render_template -class MakoModuleDescriptor(XModuleDescriptor): +class MakoTemplateBlockBase(object): """ - Module descriptor intended as a mixin that uses a mako template + XBlock intended as a mixin that uses a mako template to specify the module html. Expects the descriptor to have the `mako_template` attribute set with the name of the template to render, and it will pass the descriptor as the `module` parameter to that template - - MakoModuleDescriptor.__init__ takes the same arguments as xmodule.x_module:XModuleDescriptor.__init__ """ + # pylint: disable=no-member def __init__(self, *args, **kwargs): - super(MakoModuleDescriptor, self).__init__(*args, **kwargs) + super(MakoTemplateBlockBase, self).__init__(*args, **kwargs) if getattr(self.runtime, 'render_template', None) is None: raise TypeError( '{runtime} must have a render_template function' @@ -39,6 +43,21 @@ class MakoModuleDescriptor(XModuleDescriptor): 'editable_metadata_fields': self.editable_metadata_fields } + def studio_view(self, context): # pylint: disable=unused-argument + """ + View used in Studio. + """ + # pylint: disable=no-member + fragment = Fragment( + self.system.render_template(self.mako_template, self.get_context()) + ) + shim_xmodule_js(self, fragment) + return fragment + + +class MakoModuleDescriptor(MakoTemplateBlockBase, XModuleDescriptor): # pylint: disable=abstract-method + """ + Mixin to use for XModule descriptors. + """ def get_html(self): - return self.system.render_template( - self.mako_template, self.get_context()) + return self.studio_view(None).content