-
Notifications
You must be signed in to change notification settings - Fork 4.3k
PLAT-1419 Make edxmako a proper template backend #16710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """ | ||
| Django template system engine for Mako templates. | ||
| """ | ||
| from __future__ import absolute_import, unicode_literals | ||
|
|
||
| import logging | ||
|
|
||
| from django.template import TemplateDoesNotExist, TemplateSyntaxError | ||
| from django.template.backends.base import BaseEngine | ||
| from django.template.context import _builtin_context_processors | ||
| from django.utils.functional import cached_property | ||
| from django.utils.module_loading import import_string | ||
| from mako.exceptions import MakoException, TopLevelLookupException, text_error_template | ||
|
|
||
| from openedx.core.djangoapps.theming.helpers import get_template_path | ||
|
|
||
| from .paths import lookup_template | ||
| from .template import Template | ||
|
|
||
| LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Mako(BaseEngine): | ||
| """ | ||
| A Mako template engine to be added to the ``TEMPLATES`` Django setting. | ||
| """ | ||
| app_dirname = 'templates' | ||
|
|
||
| def __init__(self, params): | ||
| """ | ||
| Fetches template options, initializing BaseEngine properties, | ||
| and assigning our Mako default settings. | ||
| Note that OPTIONS contains backend-specific settings. | ||
| :param params: This is simply the template dict you | ||
| define in your settings file. | ||
| """ | ||
| params = params.copy() | ||
| options = params.pop('OPTIONS').copy() | ||
| super(Mako, self).__init__(params) | ||
| self.context_processors = options.pop('context_processors', []) | ||
| self.namespace = options.pop('namespace', 'main') | ||
|
|
||
| def from_string(self, template_code): | ||
| try: | ||
| return Template(template_code) | ||
| except MakoException: | ||
| message = text_error_template().render() | ||
| raise TemplateSyntaxError(message) | ||
|
|
||
| def get_template(self, template_name): | ||
| """ | ||
| Loads and returns a template for the given name. | ||
| """ | ||
| template_name = get_template_path(template_name) | ||
| try: | ||
| return Template(lookup_template(self.namespace, template_name), engine=self) | ||
| except TopLevelLookupException: | ||
| raise TemplateDoesNotExist(template_name) | ||
|
|
||
| @cached_property | ||
| def template_context_processors(self): | ||
| """ | ||
| Collect and cache the active context processors. | ||
| """ | ||
| context_processors = _builtin_context_processors | ||
| context_processors += tuple(self.context_processors) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's so weird and seemingly unnecessary that they made _builtin_context_processors a tuple, but the conf option a list.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting; the original |
||
| return tuple(import_string(path) for path in context_processors) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| from django.conf import settings | ||
| from django.core.exceptions import ImproperlyConfigured | ||
| from django.template import Engine | ||
| from django.template import Engine, engines | ||
| from django.template.base import TemplateDoesNotExist | ||
| from django.template.loaders.app_directories import Loader as AppDirectoriesLoader | ||
| from django.template.loaders.filesystem import Loader as FilesystemLoader | ||
|
|
@@ -16,8 +16,8 @@ | |
| class MakoLoader(object): | ||
| """ | ||
| This is a Django loader object which will load the template as a | ||
| Mako template if the first line is "## mako". It is based off BaseLoader | ||
| in django.template.loader. | ||
| Mako template if the first line is "## mako". It is based off Loader | ||
| in django.template.loaders.base. | ||
| We need this in order to be able to include mako templates inside main_django.html. | ||
| """ | ||
|
|
||
|
|
@@ -53,7 +53,8 @@ def load_template(self, template_name, template_dirs=None): | |
| output_encoding='utf-8', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need to refactor this to switch to the new methods before we upgrade to 2.0 or above, but we have the whole Python 3 upgrade to finish before that. I created PLAT-1820 to track this, but don't think we want to spam Splunk with lots of deprecation warnings about it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed - ticket over spam. |
||
| default_filters=['decode.utf8'], | ||
| encoding_errors='replace', | ||
| uri=template_name) | ||
| uri=template_name, | ||
| engine=engines['mako']) | ||
| return template, None | ||
| else: | ||
| # This is a regular template | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What uses this property? I couldn't find any usages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used by Django's
RequestContextclass inbind_template: https://github.com/django/django/blob/stable/1.8.x/django/template/context.py#L237There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course!