11"""
22Template block
33"""
4-
4+ import logging
55from string import Template
6- from xblock .core import XBlock
76
87from lxml import etree
98from web_fragments .fragment import Fragment
9+ from xblock .core import XBlock
10+
1011from xmodule .editing_block import EditingMixin
12+ from xmodule .modulestore .exceptions import ItemNotFoundError
1113from xmodule .raw_block import RawMixin
12- from xmodule .util .builtin_assets import add_webpack_js_to_fragment , add_css_to_fragment
13- from xmodule .x_module import (
14- ResourceTemplates ,
15- shim_xmodule_js ,
16- XModuleMixin ,
17- XModuleToXBlockMixin ,
18- )
14+ from xmodule .util .builtin_assets import add_css_to_fragment , add_webpack_js_to_fragment
15+ from xmodule .x_module import ResourceTemplates , XModuleMixin , XModuleToXBlockMixin , shim_xmodule_js
1916from xmodule .xml_block import XmlMixin
2017
21- from openedx . core . djangolib . markup import Text
18+ log = logging . getLogger ( __name__ )
2219
2320
2421class CustomTagTemplateBlock ( # pylint: disable=abstract-method
@@ -76,8 +73,10 @@ def studio_view(self, _context):
7673
7774 def render_template (self , system , xml_data ):
7875 '''Render the template, given the definition xml_data'''
76+ if not xml_data :
77+ return "Please set the template for this custom tag."
7978 xmltree = etree .fromstring (xml_data )
80- if 'impl' in xmltree .attrib :
79+ if 'impl' in xmltree .attrib and xmltree . attrib [ 'impl' ] :
8180 template_name = xmltree .attrib ['impl' ]
8281 else :
8382 # VS[compat] backwards compatibility with old nested customtag structure
@@ -86,16 +85,19 @@ def render_template(self, system, xml_data):
8685 template_name = child_impl .text
8786 else :
8887 # TODO (vshnayder): better exception type
89- raise Exception ("Could not find impl attribute in customtag {}"
90- .format (self .location ))
88+ return Template ("Could not find impl attribute in customtag {}" ).safe_substitute ({})
9189
9290 params = dict (list (xmltree .items ()))
9391
9492 # cdodge: look up the template as a module
9593 template_loc = self .location .replace (category = 'custom_tag_template' , name = template_name )
94+ try :
95+ template_block = system .get_block (template_loc )
96+ template_block_data = template_block .data
97+ except ItemNotFoundError as ex :
98+ template_block_data = f"Could not find template block for custom tag with Id { template_name } "
99+ log .info (template_block_data )
96100
97- template_block = system .get_block (template_loc )
98- template_block_data = template_block .data
99101 template = Template (template_block_data )
100102 return template .safe_substitute (params )
101103
@@ -120,28 +122,28 @@ def export_to_file(self):
120122
121123
122124class TranslateCustomTagBlock ( # pylint: disable=abstract-method
123- XModuleToXBlockMixin ,
124- XModuleMixin ,
125+ CustomTagBlock ,
125126):
126127 """
127128 Converts olx of the form `<$custom_tag attr="" attr=""/>` to CustomTagBlock
128129 of the form `<customtag attr="" attr="" impl="$custom_tag"/>`.
129130 """
130131 resources_dir = None
131132
132- @classmethod
133- def parse_xml (cls , node , runtime , _keys ):
133+ def render_template (self , system , xml_data ):
134+ xml_string = ""
135+ if xml_data :
136+ xmltree = etree .fromstring (xml_data )
137+ xmltree = self .replace_xml (xmltree )
138+ xml_string = etree .tostring (xmltree , pretty_print = True ).decode ("utf-8" )
139+ return super ().render_template (system , xml_string or xml_data )
140+
141+ def replace_xml (self , node ):
134142 """
135- Transforms the xml_data from <$custom_tag attr="" attr=""/> to
136- <customtag attr="" attr="" impl="$custom_tag"/>
143+ Replaces the xml_data from <$custom_tag attr="" attr=""/> to
144+ <customtag attr="" attr="" impl="$custom_tag"/>.
137145 """
138-
139- runtime .error_tracker (Text ('WARNING: the <{tag}> tag is deprecated. '
140- 'Instead, use <customtag impl="{tag}" attr1="..." attr2="..."/>. ' )
141- .format (tag = node .tag ))
142-
143146 tag = node .tag
144147 node .tag = 'customtag'
145148 node .attrib ['impl' ] = tag
146-
147- return runtime .process_xml (etree .tostring (node ))
149+ return node
0 commit comments