Commit bf62c4b7 by Calen Pennington

Extract a pure-XBlock version of MakoModuleDescriptor

parent b50876fc
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): class MakoDescriptorSystem(DescriptorSystem):
...@@ -8,20 +13,19 @@ class MakoDescriptorSystem(DescriptorSystem): ...@@ -8,20 +13,19 @@ class MakoDescriptorSystem(DescriptorSystem):
self.render_template = render_template 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. to specify the module html.
Expects the descriptor to have the `mako_template` attribute set Expects the descriptor to have the `mako_template` attribute set
with the name of the template to render, and it will pass with the name of the template to render, and it will pass
the descriptor as the `module` parameter to that template 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): 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: if getattr(self.runtime, 'render_template', None) is None:
raise TypeError( raise TypeError(
'{runtime} must have a render_template function' '{runtime} must have a render_template function'
...@@ -39,6 +43,21 @@ class MakoModuleDescriptor(XModuleDescriptor): ...@@ -39,6 +43,21 @@ class MakoModuleDescriptor(XModuleDescriptor):
'editable_metadata_fields': self.editable_metadata_fields '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): def get_html(self):
return self.system.render_template( return self.studio_view(None).content
self.mako_template, self.get_context())
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