From e624a8f4ac4b2bb045f6938f54a90bd948773288 Mon Sep 17 00:00:00 2001 From: Usman Khalid <2200617@gmail.com> Date: Thu, 4 Mar 2021 20:06:39 +0500 Subject: [PATCH] Convert CustomTagModule to CustomTagBlock. --- common/lib/xmodule/setup.py | 2 +- common/lib/xmodule/xmodule/static_content.py | 2 + common/lib/xmodule/xmodule/template_module.py | 69 ++++++++++++++++--- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/common/lib/xmodule/setup.py b/common/lib/xmodule/setup.py index 2b22814c1c..22271d305b 100644 --- a/common/lib/xmodule/setup.py +++ b/common/lib/xmodule/setup.py @@ -4,7 +4,6 @@ from setuptools import find_packages, setup XMODULES = [ "book = xmodule.backcompat_module:TranslateCustomTagDescriptor", - "customtag = xmodule.template_module:CustomTagDescriptor", "discuss = xmodule.backcompat_module:TranslateCustomTagDescriptor", "image = xmodule.backcompat_module:TranslateCustomTagDescriptor", "section = xmodule.backcompat_module:SemanticSectionDescriptor", @@ -20,6 +19,7 @@ XBLOCKS = [ "conditional = xmodule.conditional_module:ConditionalBlock", "course = xmodule.course_module:CourseBlock", "course_info = xmodule.html_module:CourseInfoBlock", + "customtag = xmodule.template_module:CustomTagBlock", "error = xmodule.error_module:ErrorBlock", "hidden = xmodule.hidden_module:HiddenDescriptor", "html = xmodule.html_module:HtmlBlock", diff --git a/common/lib/xmodule/xmodule/static_content.py b/common/lib/xmodule/xmodule/static_content.py index 6f5e37b758..58e03bd05a 100755 --- a/common/lib/xmodule/xmodule/static_content.py +++ b/common/lib/xmodule/xmodule/static_content.py @@ -29,6 +29,7 @@ from xmodule.lti_module import LTIBlock from xmodule.poll_module import PollBlock from xmodule.seq_module import SequenceBlock from xmodule.split_test_module import SplitTestBlock +from xmodule.template_module import CustomTagBlock from xmodule.word_cloud_module import WordCloudBlock from xmodule.x_module import XModuleDescriptor, HTMLSnippet @@ -75,6 +76,7 @@ XBLOCK_CLASSES = [ AnnotatableBlock, ConditionalBlock, CourseInfoBlock, + CustomTagBlock, HtmlBlock, LibraryContentBlock, LTIBlock, diff --git a/common/lib/xmodule/xmodule/template_module.py b/common/lib/xmodule/xmodule/template_module.py index d53856a2a9..a94cf457fa 100644 --- a/common/lib/xmodule/xmodule/template_module.py +++ b/common/lib/xmodule/xmodule/template_module.py @@ -5,11 +5,32 @@ Template module from string import Template from lxml import etree -from xmodule.raw_module import RawDescriptor -from xmodule.x_module import XModule # lint-amnesty, pylint: disable=unused-import +from pkg_resources import resource_string +from web_fragments.fragment import Fragment +from xmodule.editing_module import EditingMixin +from xmodule.raw_module import RawMixin +from xmodule.util.xmodule_django import add_webpack_to_fragment +from xmodule.x_module import ( + HTMLSnippet, + ResourceTemplates, + shim_xmodule_js, + XModuleMixin, + XModuleDescriptorToXBlockMixin, + XModuleToXBlockMixin, +) +from xmodule.xml_module import XmlMixin -class CustomTagModule(XModule): +class CustomTagBlock( + RawMixin, + XmlMixin, + EditingMixin, + XModuleDescriptorToXBlockMixin, + XModuleToXBlockMixin, + HTMLSnippet, + ResourceTemplates, + XModuleMixin, +): # pylint: disable=abstract-method """ This module supports tags of the form @@ -31,17 +52,35 @@ class CustomTagModule(XModule): Renders to:: More information given in the text """ - - def get_html(self): - return self.descriptor.rendered_html - - -class CustomTagDescriptor(RawDescriptor): - """ Descriptor for custom tags. Loads the template when created.""" - module_class = CustomTagModule resources_dir = None template_dir_name = 'customtag' + preview_view_js = { + 'js': [], + 'xmodule_js': resource_string(__name__, 'js/src/xmodule.js'), + } + preview_view_css = { + 'scss': [], + } + studio_view_js = { + 'js': [resource_string(__name__, 'js/src/raw/edit/xml.js')], + 'xmodule_js': resource_string(__name__, 'js/src/xmodule.js'), + } + studio_view_css = { + 'scss': [resource_string(__name__, 'css/codemirror/codemirror.scss')], + } + + def studio_view(self, _context): + """ + Return the studio view. + """ + fragment = Fragment( + self.system.render_template(self.mako_template, self.get_context()) + ) + add_webpack_to_fragment(fragment, 'CustomTagBlockStudio') + shim_xmodule_js(fragment, 'XMLEditingDescriptor') + return fragment + def render_template(self, system, xml_data): '''Render the template, given the definition xml_data''' xmltree = etree.fromstring(xml_data) @@ -71,6 +110,14 @@ class CustomTagDescriptor(RawDescriptor): def rendered_html(self): return self.render_template(self.system, self.data) + def student_view(self, _context): + """ + Renders the student view. + """ + fragment = Fragment() + fragment.add_content(self.rendered_html) + return fragment + def export_to_file(self): """ Custom tags are special: since they're already pointers, we don't want