fix: refactor MakoService to allow specifying namespace per template (#33061)

This commit is contained in:
Braden MacDonald
2023-08-21 10:05:38 -07:00
committed by GitHub
parent bb28c27f96
commit d60cdc2305
25 changed files with 113 additions and 55 deletions

View File

@@ -172,7 +172,7 @@ class AnnotatableBlock(
'content_html': self._render_content()
}
return self.runtime.service(self, 'mako').render_template('annotatable.html', context)
return self.runtime.service(self, 'mako').render_lms_template('annotatable.html', context)
def student_view(self, context): # lint-amnesty, pylint: disable=unused-argument
"""
@@ -191,7 +191,7 @@ class AnnotatableBlock(
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_sass_to_fragment(fragment, 'AnnotatableBlockEditor.scss')
add_webpack_js_to_fragment(fragment, 'AnnotatableBlockEditor')

View File

@@ -356,7 +356,7 @@ class ProblemBlock(
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_sass_to_fragment(fragment, 'ProblemBlockEditor.scss')
add_webpack_js_to_fragment(fragment, 'ProblemBlockEditor')
@@ -898,7 +898,7 @@ class ProblemBlock(
"""
curr_score, total_possible = self.get_display_progress()
return self.runtime.service(self, 'mako').render_template('problem_ajax.html', {
return self.runtime.service(self, 'mako').render_lms_template('problem_ajax.html', {
'element_id': self.location.html_id(),
'id': str(self.location),
'ajax_url': self.ajax_url,
@@ -1247,7 +1247,7 @@ class ProblemBlock(
'submit_disabled_cta': submit_disabled_ctas[0] if submit_disabled_ctas else None,
}
html = self.runtime.service(self, 'mako').render_template('problem.html', context)
html = self.runtime.service(self, 'mako').render_lms_template('problem.html', context)
if encapsulate:
html = HTML('<div id="problem_{id}" class="problem" data-url="{ajax_url}">{html}</div>').format(
@@ -1548,7 +1548,7 @@ class ProblemBlock(
return {
'answers': new_answers,
'correct_status_html': self.runtime.service(self, 'mako').render_template(
'correct_status_html': self.runtime.service(self, 'mako').render_lms_template(
'status_span.html',
{'status': Status('correct', self.runtime.service(self, "i18n").gettext)}
)

View File

@@ -223,7 +223,7 @@ class ConditionalBlock(
def get_html(self):
required_html_ids = [block.location.html_id() for block in self.get_required_blocks]
return self.runtime.service(self, 'mako').render_template('conditional_ajax.html', {
return self.runtime.service(self, 'mako').render_lms_template('conditional_ajax.html', {
'element_id': self.location.html_id(),
'ajax_url': self.ajax_url,
'depends': ';'.join(required_html_ids)
@@ -249,7 +249,7 @@ class ConditionalBlock(
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_webpack_js_to_fragment(fragment, 'ConditionalBlockEditor')
shim_xmodule_js(fragment, self.studio_js_module_name)
@@ -262,7 +262,7 @@ class ConditionalBlock(
if not self.is_condition_satisfied():
context = {'module': self,
'message': self.conditional_message}
html = self.runtime.service(self, 'mako').render_template('conditional_block.html', context)
html = self.runtime.service(self, 'mako').render_lms_template('conditional_block.html', context)
return json.dumps({'fragments': [{'content': html}], 'message': bool(self.conditional_message)})
fragments = [child.render(STUDENT_VIEW).to_dict() for child in self.get_children()]

View File

@@ -204,7 +204,7 @@ class DiscussionXBlock(XBlock, StudioEditableXBlockMixin, XmlMixin): # lint-amn
'login_msg': login_msg,
}
fragment.add_content(
self.runtime.service(self, 'mako').render_template('discussion/_discussion_inline.html', context)
self.runtime.service(self, 'mako').render_lms_template('discussion/_discussion_inline.html', context)
)
fragment.initialize_js('DiscussionInlineBlock')
@@ -216,7 +216,8 @@ class DiscussionXBlock(XBlock, StudioEditableXBlockMixin, XmlMixin): # lint-amn
Renders author view for Studio.
"""
fragment = Fragment()
fragment.add_content(self.runtime.service(self, 'mako').render_template(
# For historic reasons, this template is in the LMS templates folder:
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template(
'discussion/_discussion_inline_studio.html',
{
'discussion_id': self.discussion_id,

View File

@@ -60,7 +60,7 @@ class ErrorBlock(
"""
Return a fragment that contains the html for the student view.
"""
fragment = Fragment(self.runtime.service(self, 'mako').render_template('module-error.html', {
fragment = Fragment(self.runtime.service(self, 'mako').render_lms_template('module-error.html', {
'staff_access': True,
'data': self.contents,
'error': self.error_msg,

View File

@@ -134,7 +134,7 @@ class HtmlBlockMixin( # lint-amnesty, pylint: disable=abstract-method
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_sass_to_fragment(fragment, 'HtmlBlockEditor.scss')
add_webpack_js_to_fragment(fragment, 'HtmlBlockEditor')
@@ -463,7 +463,7 @@ class CourseInfoBlock(CourseInfoFields, HtmlBlockMixin): # lint-amnesty, pylint
'visible_updates': course_updates[:3],
'hidden_updates': course_updates[3:],
}
return self.runtime.service(self, 'mako').render_template(
return self.runtime.service(self, 'mako').render_lms_template(
f"{self.TEMPLATE_DIR}/course_updates.html",
context,
)

View File

@@ -391,7 +391,7 @@ class LibraryContentBlock(
'content': rendered_child.content,
})
fragment.add_content(self.runtime.service(self, 'mako').render_template('vert_module.html', {
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template('vert_module.html', {
'items': contents,
'xblock_context': context,
'show_bookmark_button': False,
@@ -421,7 +421,7 @@ class LibraryContentBlock(
if max_count < 0:
max_count = len(self.children)
fragment.add_content(self.runtime.service(self, 'mako').render_template(
fragment.add_content(self.runtime.service(self, 'mako').render_cms_template(
"library-block-author-preview-header.html", {
'max_count': max_count,
'display_name': self.display_name or self.url_name,
@@ -442,7 +442,7 @@ class LibraryContentBlock(
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_webpack_js_to_fragment(fragment, 'LibraryContentBlockEditor')
shim_xmodule_js(fragment, self.studio_js_module_name)

View File

@@ -104,7 +104,8 @@ class LibraryRoot(XBlock):
})
fragment.add_content(
self.runtime.service(self, 'mako').render_template("studio_render_paged_children_view.html", {
# For historic reasons, this template is in the LMS folder, and some external code may depend on that.
self.runtime.service(self, 'mako').render_lms_template("studio_render_paged_children_view.html", {
'items': contents,
'xblock_context': context,
'can_add': can_add,

View File

@@ -381,7 +381,7 @@ class LTIBlock(
# Add our specific template information (the raw data body)
context.update({'data': self.data})
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, context)
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, context)
)
add_sass_to_fragment(fragment, 'LTIBlockEditor.scss')
add_webpack_js_to_fragment(fragment, 'LTIBlockEditor')
@@ -498,7 +498,7 @@ class LTIBlock(
Return the student view.
"""
fragment = Fragment()
fragment.add_content(self.runtime.service(self, 'mako').render_template('lti.html', self.get_context()))
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template('lti.html', self.get_context()))
add_webpack_js_to_fragment(fragment, 'LTIBlockDisplay')
shim_xmodule_js(fragment, 'LTI')
return fragment
@@ -508,7 +508,7 @@ class LTIBlock(
"""
This is called to get context with new oauth params to iframe.
"""
template = self.runtime.service(self, 'mako').render_template('lti_form.html', self.get_context())
template = self.runtime.service(self, 'mako').render_lms_template('lti_form.html', self.get_context())
return Response(template, content_type='text/html')
def get_user_id(self):

View File

@@ -135,7 +135,7 @@ class PollBlock(
'ajax_url': self.ajax_url,
'configuration_json': self.dump_poll(),
}
fragment.add_content(self.runtime.service(self, 'mako').render_template('poll.html', params))
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template('poll.html', params))
add_sass_to_fragment(fragment, 'PollBlockDisplay.scss')
add_webpack_js_to_fragment(fragment, 'PollBlockDisplay')
shim_xmodule_js(fragment, 'Poll')

View File

@@ -516,7 +516,7 @@ class SequenceBlock(
if not self._can_user_view_content(course):
banner_text = self._hidden_content_banner_text(course)
hidden_content_html = self.runtime.service(self, 'mako').render_template(
hidden_content_html = self.runtime.service(self, 'mako').render_lms_template(
'hidden_content.html',
{
'self_paced': course.self_paced,
@@ -589,7 +589,7 @@ class SequenceBlock(
parent_block_id = self.get_parent().scope_ids.usage_id.block_id
params['chapter_completion_aggregator_url'] = '/'.join(
[settings.COMPLETION_AGGREGATOR_URL, str(self.scope_ids.usage_id.context_key), parent_block_id]) + '/'
fragment.add_content(self.runtime.service(self, 'mako').render_template("seq_block.html", params))
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template("seq_block.html", params))
self._capture_full_seq_item_metrics(children)
self._capture_current_unit_metrics(children)

View File

@@ -282,7 +282,7 @@ class SplitTestBlock( # lint-amnesty, pylint: disable=abstract-method
sorted_inactive_contents = sorted(inactive_contents, key=itemgetter('group_name'))
# Use the new template
fragment.add_content(self.runtime.service(self, 'mako').render_template('split_test_staff_view.html', {
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template('split_test_staff_view.html', {
'items': sorted_active_contents + sorted_inactive_contents,
}))
fragment.add_css('.split-test-child { display: none; }')
@@ -309,7 +309,7 @@ class SplitTestBlock( # lint-amnesty, pylint: disable=abstract-method
fragment, inactive_children, context
)
fragment.add_content(self.runtime.service(self, 'mako').render_template('split_test_author_view.html', {
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template('split_test_author_view.html', {
'split_test': self,
'is_root': is_root,
'is_configured': self.is_configured,
@@ -348,7 +348,7 @@ class SplitTestBlock( # lint-amnesty, pylint: disable=abstract-method
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_webpack_js_to_fragment(fragment, 'SplitTestBlockEditor')
shim_xmodule_js(fragment, self.studio_js_module_name)
@@ -367,7 +367,7 @@ class SplitTestBlock( # lint-amnesty, pylint: disable=abstract-method
return self._staff_view(context)
else:
child_fragment = self.child.render(STUDENT_VIEW, context)
fragment = Fragment(self.runtime.service(self, 'mako').render_template('split_test_student_view.html', {
fragment = Fragment(self.runtime.service(self, 'mako').render_lms_template('split_test_student_view.html', {
'child_content': child_fragment.content,
'child_id': self.child.scope_ids.usage_id,
}))

View File

@@ -33,10 +33,9 @@ class StudioEditableBlock(XBlockMixin):
'content': rendered_child.content
})
# 'lms.' namespace_prefix is required for rendering in studio
mako_service = self.runtime.service(self, 'mako')
mako_service.namespace_prefix = 'lms.'
fragment.add_content(mako_service.render_template("studio_render_children_view.html", { # pylint: disable=no-member
# For historic reasons, this template is in the LMS folder, and some code like xblock-utils expects that.
fragment.add_content(mako_service.render_lms_template("studio_render_children_view.html", { # pylint: disable=no-member
'items': contents,
'xblock_context': context,
'can_add': can_add,

View File

@@ -67,7 +67,7 @@ class CustomTagBlock(CustomTagTemplateBlock): # pylint: disable=abstract-method
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_sass_to_fragment(fragment, 'CustomTagBlockEditor.scss')
add_webpack_js_to_fragment(fragment, 'CustomTagBlockEditor')

View File

@@ -56,6 +56,18 @@ class StubMakoService:
"""
return self._render_template(*args, **kwargs)
def render_lms_template(self, *args, **kwargs):
"""
Invokes the configured render_template method.
"""
return self._render_template(*args, **kwargs)
def render_cms_template(self, *args, **kwargs):
"""
Invokes the configured render_template method.
"""
return self._render_template(*args, **kwargs)
class StubUserService(UserService):
"""

View File

@@ -162,7 +162,7 @@ class ConditionalBlockBasicTest(unittest.TestCase):
# we reverse it here
html = blocks['cond_block'].render(STUDENT_VIEW).content
mako_service = blocks['cond_block'].runtime.service(blocks['cond_block'], 'mako')
expected = mako_service.render_template('conditional_ajax.html', {
expected = mako_service.render_lms_template('conditional_ajax.html', {
'ajax_url': blocks['cond_block'].ajax_url,
'element_id': 'i4x-edX-conditional_test-conditional-SampleConditional',
'depends': 'i4x-edX-conditional_test-problem-SampleProblem',
@@ -243,7 +243,7 @@ class ConditionalBlockXmlTest(unittest.TestCase):
block = self.get_block_for_location(location)
html = block.render(STUDENT_VIEW).content
mako_service = block.runtime.service(block, 'mako')
html_expect = mako_service.render_template(
html_expect = mako_service.render_lms_template(
'conditional_ajax.html',
{
# Test ajax url is just usage-id / handler_name

View File

@@ -316,7 +316,7 @@ class CourseInfoBlockTestCase(unittest.TestCase):
template_name = f"{info_block.TEMPLATE_DIR}/course_updates.html"
info_block.get_html()
# Assertion to validate that render function is called with the expected context
info_block.runtime.service(info_block, 'mako').render_template.assert_called_once_with(
info_block.runtime.service(info_block, 'mako').render_lms_template.assert_called_once_with(
template_name,
expected_context
)

View File

@@ -161,7 +161,8 @@ class VerticalBlock(
child_context['username'], str(self.location)), # pylint: disable=no-member
})
fragment.add_content(self.runtime.service(self, 'mako').render_template('vert_module.html', fragment_context))
mako_service = self.runtime.service(self, 'mako')
fragment.add_content(mako_service.render_lms_template('vert_module.html', fragment_context))
add_webpack_js_to_fragment(fragment, 'VerticalStudentView')
fragment.initialize_js('VerticalStudentView')

View File

@@ -258,7 +258,7 @@ class VideoBlock(
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_sass_to_fragment(fragment, 'VideoBlockEditor.scss')
add_webpack_js_to_fragment(fragment, 'VideoBlockEditor')
@@ -498,7 +498,7 @@ class VideoBlock(
organization=organization
)
return self.runtime.service(self, 'mako').render_template('video.html', template_context)
return self.runtime.service(self, 'mako').render_lms_template('video.html', template_context)
def get_course_video_sharing_override(self):
"""

View File

@@ -253,7 +253,7 @@ class WordCloudBlock( # pylint: disable=abstract-method
Renders the output that a student will see.
"""
fragment = Fragment()
fragment.add_content(self.runtime.service(self, 'mako').render_template('word_cloud.html', {
fragment.add_content(self.runtime.service(self, 'mako').render_lms_template('word_cloud.html', {
'ajax_url': self.ajax_url,
'display_name': self.display_name,
'instructions': self.instructions,
@@ -279,7 +279,7 @@ class WordCloudBlock( # pylint: disable=abstract-method
Return the studio view.
"""
fragment = Fragment(
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
self.runtime.service(self, 'mako').render_cms_template(self.mako_template, self.get_context())
)
add_webpack_js_to_fragment(fragment, 'WordCloudBlockEditor')
shim_xmodule_js(fragment, self.studio_js_module_name)