Django 1.11: Template now accepts backend in __init__.

This makes our `zproject.jinja2.backend.Template` compatible with
Jinja2. After this change we don't need to override __init__ function
in Template class.

The only reason we now need to create our own Template class is that
we need to send template_rendered signals.

We need our own Jinja2 class because we need to maintain backward
compatibility with Django 1.10 and we need inject `debug` parameter.
This commit is contained in:
Umair Khan
2017-05-17 14:45:49 +05:00
committed by Tim Abbott
parent d8598e18e0
commit c25e9ad193

View File

@@ -29,16 +29,19 @@ class Jinja2(django_jinja2.Jinja2):
# type: (Dict[str, Any], *Any, **Any) -> None # type: (Dict[str, Any], *Any, **Any) -> None
# We need to remove `context_processors` from `OPTIONS` because # We need to remove `context_processors` from `OPTIONS` because
# `Environment` doesn't expect it # `Environment` doesn't expect it
self.context_processors = params['OPTIONS'].pop('context_processors', []) context_processors = params['OPTIONS'].pop('context_processors', [])
self.debug = params['OPTIONS'].pop('debug', False) debug = params['OPTIONS'].pop('debug', False)
super(Jinja2, self).__init__(params, *args, **kwargs) super(Jinja2, self).__init__(params, *args, **kwargs)
# We need to create these two properties after calling the __init__
# of base class so that they are not overridden.
self.context_processors = context_processors
self.debug = debug
def get_template(self, template_name): def get_template(self, template_name):
# type: (str) -> Template # type: (str) -> Template
try: try:
return Template(self.env.get_template(template_name), return Template(self.env.get_template(template_name), self)
self.context_processors,
self.debug)
except jinja2.TemplateNotFound as exc: except jinja2.TemplateNotFound as exc:
six.reraise(TemplateDoesNotExist, TemplateDoesNotExist(exc.args), six.reraise(TemplateDoesNotExist, TemplateDoesNotExist(exc.args),
sys.exc_info()[2]) sys.exc_info()[2])
@@ -48,9 +51,7 @@ class Jinja2(django_jinja2.Jinja2):
def from_string(self, template_code): def from_string(self, template_code):
# type: (str) -> Template # type: (str) -> Template
return Template(self.env.from_string(template_code), return Template(self.env.from_string(template_code), self)
self.context_processors,
self.debug)
class Template(django_jinja2.Template): class Template(django_jinja2.Template):
@@ -60,12 +61,15 @@ class Template(django_jinja2.Template):
processors to the context before passing it to the `render` processors to the context before passing it to the `render`
function. function.
""" """
def __init__(self, template, backend):
def __init__(self, template, context_processors, debug, *args, **kwargs): # type: (django_jinja2.Template, django_jinja2.BaseEngine) -> None
# type: (str, List[str], bool, *Any, **Any) -> None # TODO: Remove this function once we shift to Django 1.11 completely.
self.context_processors = context_processors # This is only intended for backward compatibility.
self.debug = debug self.template = template
super(Template, self).__init__(template, *args, **kwargs) self.backend = backend
self.origin = django_jinja2.Origin(
name=template.filename, template_name=template.name,
)
def render(self, context=None, request=None): def render(self, context=None, request=None):
# type: (Optional[Union[Dict[str, Any], Context]], Optional[HttpRequest]) -> Text # type: (Optional[Union[Dict[str, Any], Context]], Optional[HttpRequest]) -> Text
@@ -86,11 +90,11 @@ class Template(django_jinja2.Template):
context['csrf_input'] = csrf_input_lazy(request) context['csrf_input'] = csrf_input_lazy(request)
context['csrf_token'] = csrf_token_lazy(request) context['csrf_token'] = csrf_token_lazy(request)
for context_processor in self.context_processors: for context_processor in self.backend.context_processors:
cp = import_string(context_processor) cp = import_string(context_processor)
context.update(cp(request)) context.update(cp(request))
if self.debug: if self.backend.debug:
template_rendered.send(sender=self, template=self, template_rendered.send(sender=self, template=self,
context=context) context=context)