PLAT-1419 Make edxmako a proper template backend - #16710
Conversation
8781a8c to
e4ffab2
Compare
bmedx
left a comment
There was a problem hiding this comment.
Looks ok, just some nits and questions
| Collect and cache the active context processors. | ||
| """ | ||
| context_processors = _builtin_context_processors | ||
| context_processors += tuple(self.context_processors) |
There was a problem hiding this comment.
It's so weird and seemingly unnecessary that they made _builtin_context_processors a tuple, but the conf option a list.
There was a problem hiding this comment.
Interesting; the original TEMPLATE_CONTEXT_PROCESSORS setting was a tuple, but the new context_processors option under TEMPLATES is a list. Maybe that's just because everything else under TEMPLATES was already a list for ease of customization, so it made sense to have them consistent? I definitely think using a tuple internally is the right call, to avoid accidental modifications.
| @@ -17,7 +17,7 @@ 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 | |||
There was a problem hiding this comment.
Nit: This class looks like it's just "Loader" now
There was a problem hiding this comment.
Fixed the package move but not the rename; fixing.
| context_dictionary['django_context'] = context_instance | ||
| context_dictionary['marketing_link'] = marketing_link | ||
| context_dictionary['is_any_marketing_link_set'] = is_any_marketing_link_set | ||
| context_dictionary['is_marketing_link_set'] = is_marketing_link_set |
There was a problem hiding this comment.
I understand what the comment is saying here, but I'm not sure I get where/why we turn off context processors in tests. Digging through the code hasn't been enlightening. These seem weird here, just trying to understand.
There was a problem hiding this comment.
It's not that we deliberately disable them for tests, but that context processors take an HttpRequest as their only argument...and sometimes we don't have an actual web request and the associated machinery, because we're testing something smaller than a full view rendering. This can also be a problem in Celery tasks, etc.
| template_locations = settings.MAKO_TEMPLATES | ||
| for namespace, directories in template_locations.items(): | ||
| for backend in settings.TEMPLATES: | ||
| if 'Mako' not in backend['BACKEND']: |
There was a problem hiding this comment.
Nit: Maybe use edxmako here instead?
There was a problem hiding this comment.
Sure, that's a little more specific.
| raise TemplateDoesNotExist(template_name) | ||
|
|
||
| @cached_property | ||
| def template_context_processors(self): |
There was a problem hiding this comment.
What uses this property? I couldn't find any usages.
There was a problem hiding this comment.
It's used by Django's RequestContext class in bind_template: https://github.com/django/django/blob/stable/1.8.x/django/template/context.py#L237
| @@ -53,7 +53,8 @@ def load_template(self, template_name, template_dirs=None): | |||
| output_encoding='utf-8', | |||
There was a problem hiding this comment.
Since load_template has been deprecated in the base Loader class since Django 1.9, should we also add a deprecation warning (perhaps shimmed) at the beginning of this method?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed - ticket over spam.
| if len(args) and isinstance(args[0], MakoTemplate): | ||
| self.mako_template = args[0] | ||
| else: | ||
| kwargs['lookup'] = LOOKUP['main'] |
There was a problem hiding this comment.
Is LOOKUP['main'] guaranteed to exist here?
There was a problem hiding this comment.
In practice, yes. It doesn't feel like throwing an exception from __init__ or passing None into the MakoTemplate constructor would be any better at debugging future failures around this than just bubbling up the KeyError; do you have any better suggestions?
There was a problem hiding this comment.
Nope - I support just letting the KeyError bubble up if it ever happens. We'd see it in the logs.
| """ | ||
| Add to the given dictionary context variables which should always be | ||
| present, even when context processors aren't run during tests. Using | ||
| a context processor should almost always be preferred to adding more |
There was a problem hiding this comment.
Nit: Perhaps stronger language here? Such as: "Never add to these variables."?
There was a problem hiding this comment.
Not sure if that's appropriate; I think the most accurate guidance is "only add things here needed to render the base templates", so this already feels a little on the scare words side to me.
| """ | ||
| Render the specified template with the given dictionary of context data. | ||
| """ | ||
| return self.lookup.get_template(template_filename).render(**dictionary) |
| self.module.DICT_VALUE['test_key'] = lambda settings: settings.DERIVED_VALUE * 3 | ||
| derived(('DICT_VALUE', 'test_key')) | ||
| derived_collection_entry('DICT_VALUE', 'test_key') | ||
| self.module.DICT_VALUE['list_key'] = ['first', lambda settings: settings.DERIVED_VALUE] |
There was a problem hiding this comment.
I think I'll just change it to something like "not derived".
e4ffab2 to
fdc50c3
Compare
|
EdX Release Notice: This PR has been deployed to the staging environment in preparation for a release to production on Tuesday, December 05, 2017. |
|
EdX Release Notice: This PR has been deployed to the production environment. |
This refactoring wasn't strictly necessary to achieve Django 1.10 compatibility, but it makes it much easier to figure out which recent changes to the Django template system impact us. It also makes it easier to understand our template engine and gets us more in line with modern Django practice in this regard. Notable changes:
TEMPLATESsetting.render_to_responseshortcut andTemplate.renderinto a single implementation.django_contextvariable; removed it.templatetag_helpersmodule; removed them both.It should now be feasible to deprecate the rendering functions from
edxmako.shortcutsin favor of the standard Django rendering shortcuts, but we use these in dozens of modules. Leaving that as an exercise for another time (if it's even worth pursuing).