Files
edx-platform/common/djangoapps/edxmako/services.py
Jillian Vogel ad5ad72273 [BD-13] Deprecate ModuleSystem.render_template (fixed) (#29354)
* refactor: deprecates ModuleSystem.render_template

in favor of the added MakoSystem render_template method.

Related changes:
* Adds the MakoService to the StudioEditModuleRuntime,
  PreviewModuleSystem, LmsModuleSystem, and XBlockRuntime
* MakoService constructor takes a `namespace_prefix` string, so that the
  CMS PreviewModuleSystem can render to LMS templates, without needing
  the special render_from_lms helper method.
* ModuleSystem.render_template becomes a read-only property, so the
  constructor calls and test module systems are updated accordingly.
* Adds tests for the MakoService and module system shims.

(cherry picked from commit 457f959356)

* refactor: use MakoService.render_template to remove deprecation warnings

from block code.

(cherry picked from commit 8d62d337f5)

* refactor: use MakoService.render_template to remove deprecation warnings

from test code.

(cherry picked from commit 26b43465a4)

* test: Adds a test to verify the bug introduced by the previous changes

The AuthoringMixin is automatically added to all XBlocks (see
settings.XBLOCK_MIXINS), and AuthoringMixin.visibility_view expects the
"mako" service.

This test verifies the bug by testing the PureXBlock, which does not
require the "mako" service, and so fails when the visibility_view is
rendered.

* fix: AuthoringMixin needs mako service

which fixes the visibility_view for XBlocks which don't explicitly
require the mako service.

Also removes the unneeded class property _services_requested from
AuthoringMixin and StudioEditableBlock. This property is better provided
by the XBlockMixin class.
2021-11-29 14:42:52 -05:00

31 lines
942 B
Python

"""
Supports rendering an XBlock to HTML using mako templates.
"""
from xblock.reference.plugins import Service
from common.djangoapps.edxmako.shortcuts import render_to_string
class MakoService(Service):
"""
A service for rendering XBlocks to HTML using mako templates.
Args:
namespace_prefix(string): optional prefix to the mako namespace used to find the template file.
e.g to access LMS templates from within Studio code, pass namespace_prefix='lms.'
"""
def __init__(
self,
namespace_prefix='',
**kwargs
):
super().__init__(**kwargs)
self.namespace_prefix = namespace_prefix
def render_template(self, template_file, dictionary, namespace='main'):
"""
Takes (template_file, dictionary) and returns rendered HTML.
"""
return render_to_string(template_file, dictionary, namespace=self.namespace_prefix + namespace)