| 1 |
from pathlib import Path |
| 2 |
|
| 3 |
import jinja2 |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.template import TemplateDoesNotExist, TemplateSyntaxError |
| 7 |
from django.utils.functional import cached_property |
| 8 |
from django.utils.module_loading import import_string |
| 9 |
|
| 10 |
from .base import BaseEngine |
| 11 |
from .utils import csrf_input_lazy, csrf_token_lazy |
| 12 |
|
| 13 |
|
| 14 |
class Jinja2(BaseEngine): |
| 15 |
app_dirname = "jinja2" |
| 16 |
|
| 17 |
def __init__(self, params): |
| 18 |
params = params.copy() |
| 19 |
options = params.pop("OPTIONS").copy() |
| 20 |
super().__init__(params) |
| 21 |
|
| 22 |
self.context_processors = options.pop("context_processors", []) |
| 23 |
|
| 24 |
environment = options.pop("environment", "jinja2.Environment") |
| 25 |
environment_cls = import_string(environment) |
| 26 |
|
| 27 |
if "loader" not in options: |
| 28 |
options["loader"] = jinja2.FileSystemLoader(self.template_dirs) |
| 29 |
options.setdefault("autoescape", True) |
| 30 |
options.setdefault("auto_reload", settings.DEBUG) |
| 31 |
options.setdefault( |
| 32 |
"undefined", jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined |
| 33 |
) |
| 34 |
|
| 35 |
self.env = environment_cls(**options) |
| 36 |
|
| 37 |
def from_string(self, template_code): |
| 38 |
return Template(self.env.from_string(template_code), self) |
| 39 |
|
| 40 |
def get_template(self, template_name): |
| 41 |
try: |
| 42 |
return Template(self.env.get_template(template_name), self) |
| 43 |
except jinja2.TemplateNotFound as exc: |
| 44 |
raise TemplateDoesNotExist(exc.name, backend=self) from exc |
| 45 |
except jinja2.TemplateSyntaxError as exc: |
| 46 |
new = TemplateSyntaxError(exc.args) |
| 47 |
new.template_debug = get_exception_info(exc) |
| 48 |
raise new from exc |
| 49 |
|
| 50 |
@cached_property |
| 51 |
def template_context_processors(self): |
| 52 |
return [import_string(path) for path in self.context_processors] |
| 53 |
|
| 54 |
|
| 55 |
class Template: |
| 56 |
def __init__(self, template, backend): |
| 57 |
self.template = template |
| 58 |
self.backend = backend |
| 59 |
self.origin = Origin( |
| 60 |
name=template.filename, |
| 61 |
template_name=template.name, |
| 62 |
) |
| 63 |
|
| 64 |
def render(self, context=None, request=None): |
| 65 |
if context is None: |
| 66 |
context = {} |
| 67 |
if request is not None: |
| 68 |
context["request"] = request |
| 69 |
context["csrf_input"] = csrf_input_lazy(request) |
| 70 |
context["csrf_token"] = csrf_token_lazy(request) |
| 71 |
for context_processor in self.backend.template_context_processors: |
| 72 |
context.update(context_processor(request)) |
| 73 |
try: |
| 74 |
return self.template.render(context) |
| 75 |
except jinja2.TemplateSyntaxError as exc: |
| 76 |
new = TemplateSyntaxError(exc.args) |
| 77 |
new.template_debug = get_exception_info(exc) |
| 78 |
raise new from exc |
| 79 |
|
| 80 |
|
| 81 |
class Origin: |
| 82 |
""" |
| 83 |
A container to hold debug information as described in the template API |
| 84 |
documentation. |
| 85 |
""" |
| 86 |
|
| 87 |
def __init__(self, name, template_name): |
| 88 |
self.name = name |
| 89 |
self.template_name = template_name |
| 90 |
|
| 91 |
|
| 92 |
def get_exception_info(exception): |
| 93 |
""" |
| 94 |
Format exception information for display on the debug page using the |
| 95 |
structure described in the template API documentation. |
| 96 |
""" |
| 97 |
context_lines = 10 |
| 98 |
lineno = exception.lineno |
| 99 |
source = exception.source |
| 100 |
if source is None: |
| 101 |
exception_file = Path(exception.filename) |
| 102 |
if exception_file.exists(): |
| 103 |
source = exception_file.read_text() |
| 104 |
if source is not None: |
| 105 |
lines = list(enumerate(source.strip().split("\n"), start=1)) |
| 106 |
during = lines[lineno - 1][1] |
| 107 |
total = len(lines) |
| 108 |
top = max(0, lineno - context_lines - 1) |
| 109 |
bottom = min(total, lineno + context_lines) |
| 110 |
else: |
| 111 |
during = "" |
| 112 |
lines = [] |
| 113 |
total = top = bottom = 0 |
| 114 |
return { |
| 115 |
"name": exception.filename, |
| 116 |
"message": exception.message, |
| 117 |
"source_lines": lines[top:bottom], |
| 118 |
"line": lineno, |
| 119 |
"before": "", |
| 120 |
"during": during, |
| 121 |
"after": "", |
| 122 |
"total": total, |
| 123 |
"top": top, |
| 124 |
"bottom": bottom, |
| 125 |
} |