2014-11-29 07:05:56 +08:00
|
|
|
# Since this package contains a "django" module, this is required on Python 2.
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-11-13 04:51:35 +08:00
|
|
|
import sys
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
import jinja2
|
|
|
|
|
2014-11-13 04:51:35 +08:00
|
|
|
from django.conf import settings
|
2015-01-11 04:35:09 +08:00
|
|
|
from django.template import TemplateDoesNotExist, TemplateSyntaxError
|
2014-11-13 04:51:35 +08:00
|
|
|
from django.utils import six
|
2016-06-27 21:56:21 +08:00
|
|
|
from django.utils.functional import cached_property
|
2014-11-13 04:51:35 +08:00
|
|
|
from django.utils.module_loading import import_string
|
|
|
|
|
|
|
|
from .base import BaseEngine
|
|
|
|
from .utils import csrf_input_lazy, csrf_token_lazy
|
|
|
|
|
|
|
|
|
|
|
|
class Jinja2(BaseEngine):
|
|
|
|
|
|
|
|
app_dirname = 'jinja2'
|
|
|
|
|
|
|
|
def __init__(self, params):
|
|
|
|
params = params.copy()
|
|
|
|
options = params.pop('OPTIONS').copy()
|
|
|
|
super(Jinja2, self).__init__(params)
|
|
|
|
|
2016-06-27 21:56:21 +08:00
|
|
|
self.context_processors = options.pop('context_processors', [])
|
|
|
|
|
2014-11-13 04:51:35 +08:00
|
|
|
environment = options.pop('environment', 'jinja2.Environment')
|
|
|
|
environment_cls = import_string(environment)
|
|
|
|
|
2015-12-27 04:11:53 +08:00
|
|
|
if 'loader' not in options:
|
|
|
|
options['loader'] = jinja2.FileSystemLoader(self.template_dirs)
|
2014-11-13 04:51:35 +08:00
|
|
|
options.setdefault('autoescape', True)
|
|
|
|
options.setdefault('auto_reload', settings.DEBUG)
|
|
|
|
options.setdefault('undefined',
|
2015-01-11 04:35:09 +08:00
|
|
|
jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined)
|
2014-11-13 04:51:35 +08:00
|
|
|
|
|
|
|
self.env = environment_cls(**options)
|
|
|
|
|
|
|
|
def from_string(self, template_code):
|
2016-06-27 21:56:21 +08:00
|
|
|
return Template(self.env.from_string(template_code), self)
|
2014-11-13 04:51:35 +08:00
|
|
|
|
|
|
|
def get_template(self, template_name):
|
|
|
|
try:
|
2016-06-27 21:56:21 +08:00
|
|
|
return Template(self.env.get_template(template_name), self)
|
2015-01-11 04:35:09 +08:00
|
|
|
except jinja2.TemplateNotFound as exc:
|
2015-04-25 03:33:03 +08:00
|
|
|
six.reraise(
|
|
|
|
TemplateDoesNotExist,
|
|
|
|
TemplateDoesNotExist(exc.name, backend=self),
|
|
|
|
sys.exc_info()[2],
|
|
|
|
)
|
2015-01-11 04:35:09 +08:00
|
|
|
except jinja2.TemplateSyntaxError as exc:
|
2015-04-25 03:33:03 +08:00
|
|
|
new = TemplateSyntaxError(exc.args)
|
|
|
|
new.template_debug = get_exception_info(exc)
|
|
|
|
six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
|
2014-11-13 04:51:35 +08:00
|
|
|
|
2016-06-27 21:56:21 +08:00
|
|
|
@cached_property
|
|
|
|
def template_context_processors(self):
|
|
|
|
return [import_string(path) for path in self.context_processors]
|
|
|
|
|
2014-11-13 04:51:35 +08:00
|
|
|
|
|
|
|
class Template(object):
|
|
|
|
|
2016-06-27 21:56:21 +08:00
|
|
|
def __init__(self, template, backend):
|
2014-11-13 04:51:35 +08:00
|
|
|
self.template = template
|
2016-06-27 21:56:21 +08:00
|
|
|
self.backend = backend
|
2015-04-25 03:33:03 +08:00
|
|
|
self.origin = Origin(
|
|
|
|
name=template.filename, template_name=template.name,
|
|
|
|
)
|
2014-11-13 04:51:35 +08:00
|
|
|
|
|
|
|
def render(self, context=None, request=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
if request is not None:
|
|
|
|
context['request'] = request
|
|
|
|
context['csrf_input'] = csrf_input_lazy(request)
|
|
|
|
context['csrf_token'] = csrf_token_lazy(request)
|
2016-06-27 21:56:21 +08:00
|
|
|
for context_processor in self.backend.template_context_processors:
|
|
|
|
context.update(context_processor(request))
|
2015-03-31 12:37:17 +08:00
|
|
|
return self.template.render(context)
|
2015-04-25 03:33:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Origin(object):
|
|
|
|
"""
|
|
|
|
A container to hold debug information as described in the template API
|
|
|
|
documentation.
|
|
|
|
"""
|
|
|
|
def __init__(self, name, template_name):
|
|
|
|
self.name = name
|
|
|
|
self.template_name = template_name
|
|
|
|
|
|
|
|
|
|
|
|
def get_exception_info(exception):
|
|
|
|
"""
|
|
|
|
Formats exception information for display on the debug page using the
|
|
|
|
structure described in the template API documentation.
|
|
|
|
"""
|
|
|
|
context_lines = 10
|
|
|
|
lineno = exception.lineno
|
|
|
|
lines = list(enumerate(exception.source.strip().split("\n"), start=1))
|
|
|
|
during = lines[lineno - 1][1]
|
|
|
|
total = len(lines)
|
|
|
|
top = max(0, lineno - context_lines - 1)
|
|
|
|
bottom = min(total, lineno + context_lines)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': exception.filename,
|
|
|
|
'message': exception.message,
|
|
|
|
'source_lines': lines[top:bottom],
|
|
|
|
'line': lineno,
|
|
|
|
'before': '',
|
|
|
|
'during': during,
|
|
|
|
'after': '',
|
|
|
|
'total': total,
|
|
|
|
'top': top,
|
|
|
|
'bottom': bottom,
|
|
|
|
}
|