测试gitnore
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from django.core.exceptions import ImproperlyConfigured, SuspiciousFileOperation
|
||||
from django.core.exceptions import (
|
||||
ImproperlyConfigured, SuspiciousFileOperation,
|
||||
)
|
||||
from django.template.utils import get_app_template_dirs
|
||||
from django.utils._os import safe_join
|
||||
from django.utils.functional import cached_property
|
||||
@@ -16,20 +18,18 @@ class BaseEngine:
|
||||
`params` is a dict of configuration settings.
|
||||
"""
|
||||
params = params.copy()
|
||||
self.name = params.pop("NAME")
|
||||
self.dirs = list(params.pop("DIRS"))
|
||||
self.app_dirs = params.pop("APP_DIRS")
|
||||
self.name = params.pop('NAME')
|
||||
self.dirs = list(params.pop('DIRS'))
|
||||
self.app_dirs = params.pop('APP_DIRS')
|
||||
if params:
|
||||
raise ImproperlyConfigured(
|
||||
"Unknown parameters: {}".format(", ".join(params))
|
||||
)
|
||||
"Unknown parameters: {}".format(", ".join(params)))
|
||||
|
||||
@property
|
||||
def app_dirname(self):
|
||||
raise ImproperlyConfigured(
|
||||
"{} doesn't support loading templates from installed "
|
||||
"applications.".format(self.__class__.__name__)
|
||||
)
|
||||
"applications.".format(self.__class__.__name__))
|
||||
|
||||
def from_string(self, template_code):
|
||||
"""
|
||||
@@ -38,8 +38,8 @@ class BaseEngine:
|
||||
This method is optional.
|
||||
"""
|
||||
raise NotImplementedError(
|
||||
"subclasses of BaseEngine should provide a from_string() method"
|
||||
)
|
||||
"subclasses of BaseEngine should provide "
|
||||
"a from_string() method")
|
||||
|
||||
def get_template(self, template_name):
|
||||
"""
|
||||
@@ -48,8 +48,8 @@ class BaseEngine:
|
||||
Raise TemplateDoesNotExist if no such template exists.
|
||||
"""
|
||||
raise NotImplementedError(
|
||||
"subclasses of BaseEngine must provide a get_template() method"
|
||||
)
|
||||
"subclasses of BaseEngine must provide "
|
||||
"a get_template() method")
|
||||
|
||||
# Utility methods: they are provided to minimize code duplication and
|
||||
# security issues in third-party backends.
|
||||
|
||||
@@ -13,16 +13,16 @@ from .base import BaseEngine
|
||||
|
||||
class DjangoTemplates(BaseEngine):
|
||||
|
||||
app_dirname = "templates"
|
||||
app_dirname = 'templates'
|
||||
|
||||
def __init__(self, params):
|
||||
params = params.copy()
|
||||
options = params.pop("OPTIONS").copy()
|
||||
options.setdefault("autoescape", True)
|
||||
options.setdefault("debug", settings.DEBUG)
|
||||
options.setdefault("file_charset", "utf-8")
|
||||
libraries = options.get("libraries", {})
|
||||
options["libraries"] = self.get_templatetag_libraries(libraries)
|
||||
options = params.pop('OPTIONS').copy()
|
||||
options.setdefault('autoescape', True)
|
||||
options.setdefault('debug', settings.DEBUG)
|
||||
options.setdefault('file_charset', 'utf-8')
|
||||
libraries = options.get('libraries', {})
|
||||
options['libraries'] = self.get_templatetag_libraries(libraries)
|
||||
super().__init__(params)
|
||||
self.engine = Engine(self.dirs, self.app_dirs, **options)
|
||||
|
||||
@@ -46,6 +46,7 @@ class DjangoTemplates(BaseEngine):
|
||||
|
||||
|
||||
class Template:
|
||||
|
||||
def __init__(self, template, backend):
|
||||
self.template = template
|
||||
self.backend = backend
|
||||
@@ -55,9 +56,7 @@ class Template:
|
||||
return self.template.origin
|
||||
|
||||
def render(self, context=None, request=None):
|
||||
context = make_context(
|
||||
context, request, autoescape=self.backend.engine.autoescape
|
||||
)
|
||||
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
|
||||
try:
|
||||
return self.template.render(context)
|
||||
except TemplateDoesNotExist as exc:
|
||||
@@ -72,7 +71,7 @@ def copy_exception(exc, backend=None):
|
||||
"""
|
||||
backend = backend or exc.backend
|
||||
new = exc.__class__(*exc.args, tried=exc.tried, backend=backend, chain=exc.chain)
|
||||
if hasattr(exc, "template_debug"):
|
||||
if hasattr(exc, 'template_debug'):
|
||||
new.template_debug = exc.template_debug
|
||||
return new
|
||||
|
||||
@@ -93,10 +92,10 @@ def get_installed_libraries():
|
||||
django.templatetags.i18n is stored as i18n.
|
||||
"""
|
||||
libraries = {}
|
||||
candidates = ["django.templatetags"]
|
||||
candidates = ['django.templatetags']
|
||||
candidates.extend(
|
||||
"%s.templatetags" % app_config.name for app_config in apps.get_app_configs()
|
||||
)
|
||||
'%s.templatetags' % app_config.name
|
||||
for app_config in apps.get_app_configs())
|
||||
|
||||
for candidate in candidates:
|
||||
try:
|
||||
@@ -105,9 +104,9 @@ def get_installed_libraries():
|
||||
# No templatetags package defined. This is safe to ignore.
|
||||
continue
|
||||
|
||||
if hasattr(pkg, "__path__"):
|
||||
if hasattr(pkg, '__path__'):
|
||||
for name in get_package_libraries(pkg):
|
||||
libraries[name[len(candidate) + 1 :]] = name
|
||||
libraries[name[len(candidate) + 1:]] = name
|
||||
|
||||
return libraries
|
||||
|
||||
@@ -117,7 +116,7 @@ def get_package_libraries(pkg):
|
||||
Recursively yield template tag libraries defined in submodules of a
|
||||
package.
|
||||
"""
|
||||
for entry in walk_packages(pkg.__path__, pkg.__name__ + "."):
|
||||
for entry in walk_packages(pkg.__path__, pkg.__name__ + '.'):
|
||||
try:
|
||||
module = import_module(entry[1])
|
||||
except ImportError as e:
|
||||
@@ -126,5 +125,5 @@ def get_package_libraries(pkg):
|
||||
"trying to load '%s': %s" % (entry[1], e)
|
||||
) from e
|
||||
|
||||
if hasattr(module, "register"):
|
||||
if hasattr(module, 'register'):
|
||||
yield entry[1]
|
||||
|
||||
@@ -10,13 +10,14 @@ from .utils import csrf_input_lazy, csrf_token_lazy
|
||||
|
||||
class TemplateStrings(BaseEngine):
|
||||
|
||||
app_dirname = "template_strings"
|
||||
app_dirname = 'template_strings'
|
||||
|
||||
def __init__(self, params):
|
||||
params = params.copy()
|
||||
options = params.pop("OPTIONS").copy()
|
||||
options = params.pop('OPTIONS').copy()
|
||||
if options:
|
||||
raise ImproperlyConfigured("Unknown options: {}".format(", ".join(options)))
|
||||
raise ImproperlyConfigured(
|
||||
"Unknown options: {}".format(", ".join(options)))
|
||||
super().__init__(params)
|
||||
|
||||
def from_string(self, template_code):
|
||||
@@ -26,27 +27,26 @@ class TemplateStrings(BaseEngine):
|
||||
tried = []
|
||||
for template_file in self.iter_template_filenames(template_name):
|
||||
try:
|
||||
with open(template_file, encoding="utf-8") as fp:
|
||||
with open(template_file, encoding='utf-8') as fp:
|
||||
template_code = fp.read()
|
||||
except FileNotFoundError:
|
||||
tried.append(
|
||||
(
|
||||
Origin(template_file, template_name, self),
|
||||
"Source does not exist",
|
||||
)
|
||||
)
|
||||
tried.append((
|
||||
Origin(template_file, template_name, self),
|
||||
'Source does not exist',
|
||||
))
|
||||
else:
|
||||
return Template(template_code)
|
||||
raise TemplateDoesNotExist(template_name, tried=tried, backend=self)
|
||||
|
||||
|
||||
class Template(string.Template):
|
||||
|
||||
def render(self, context=None, request=None):
|
||||
if context is None:
|
||||
context = {}
|
||||
else:
|
||||
context = {k: conditional_escape(v) for k, v in context.items()}
|
||||
if request is not None:
|
||||
context["csrf_input"] = csrf_input_lazy(request)
|
||||
context["csrf_token"] = csrf_token_lazy(request)
|
||||
context['csrf_input'] = csrf_input_lazy(request)
|
||||
context['csrf_token'] = csrf_token_lazy(request)
|
||||
return self.safe_substitute(context)
|
||||
|
||||
@@ -12,25 +12,24 @@ from .base import BaseEngine
|
||||
|
||||
class Jinja2(BaseEngine):
|
||||
|
||||
app_dirname = "jinja2"
|
||||
app_dirname = 'jinja2'
|
||||
|
||||
def __init__(self, params):
|
||||
params = params.copy()
|
||||
options = params.pop("OPTIONS").copy()
|
||||
options = params.pop('OPTIONS').copy()
|
||||
super().__init__(params)
|
||||
|
||||
self.context_processors = options.pop("context_processors", [])
|
||||
self.context_processors = options.pop('context_processors', [])
|
||||
|
||||
environment = options.pop("environment", "jinja2.Environment")
|
||||
environment = options.pop('environment', 'jinja2.Environment')
|
||||
environment_cls = import_string(environment)
|
||||
|
||||
if "loader" not in options:
|
||||
options["loader"] = jinja2.FileSystemLoader(self.template_dirs)
|
||||
options.setdefault("autoescape", True)
|
||||
options.setdefault("auto_reload", settings.DEBUG)
|
||||
options.setdefault(
|
||||
"undefined", jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined
|
||||
)
|
||||
if 'loader' not in options:
|
||||
options['loader'] = jinja2.FileSystemLoader(self.template_dirs)
|
||||
options.setdefault('autoescape', True)
|
||||
options.setdefault('auto_reload', settings.DEBUG)
|
||||
options.setdefault('undefined',
|
||||
jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined)
|
||||
|
||||
self.env = environment_cls(**options)
|
||||
|
||||
@@ -53,23 +52,22 @@ class Jinja2(BaseEngine):
|
||||
|
||||
|
||||
class Template:
|
||||
|
||||
def __init__(self, template, backend):
|
||||
self.template = template
|
||||
self.backend = backend
|
||||
self.origin = Origin(
|
||||
name=template.filename,
|
||||
template_name=template.name,
|
||||
name=template.filename, template_name=template.name,
|
||||
)
|
||||
|
||||
def render(self, context=None, request=None):
|
||||
from .utils import csrf_input_lazy, csrf_token_lazy
|
||||
|
||||
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)
|
||||
context['request'] = request
|
||||
context['csrf_input'] = csrf_input_lazy(request)
|
||||
context['csrf_token'] = csrf_token_lazy(request)
|
||||
for context_processor in self.backend.template_context_processors:
|
||||
context.update(context_processor(request))
|
||||
try:
|
||||
@@ -85,7 +83,6 @@ class Origin:
|
||||
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
|
||||
@@ -102,27 +99,27 @@ def get_exception_info(exception):
|
||||
if source is None:
|
||||
exception_file = Path(exception.filename)
|
||||
if exception_file.exists():
|
||||
with open(exception_file, "r") as fp:
|
||||
with open(exception_file, 'r') as fp:
|
||||
source = fp.read()
|
||||
if source is not None:
|
||||
lines = list(enumerate(source.strip().split("\n"), start=1))
|
||||
lines = list(enumerate(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)
|
||||
else:
|
||||
during = ""
|
||||
during = ''
|
||||
lines = []
|
||||
total = top = bottom = 0
|
||||
return {
|
||||
"name": exception.filename,
|
||||
"message": exception.message,
|
||||
"source_lines": lines[top:bottom],
|
||||
"line": lineno,
|
||||
"before": "",
|
||||
"during": during,
|
||||
"after": "",
|
||||
"total": total,
|
||||
"top": top,
|
||||
"bottom": bottom,
|
||||
'name': exception.filename,
|
||||
'message': exception.message,
|
||||
'source_lines': lines[top:bottom],
|
||||
'line': lineno,
|
||||
'before': '',
|
||||
'during': during,
|
||||
'after': '',
|
||||
'total': total,
|
||||
'top': top,
|
||||
'bottom': bottom,
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ from django.utils.safestring import SafeString
|
||||
def csrf_input(request):
|
||||
return format_html(
|
||||
'<input type="hidden" name="csrfmiddlewaretoken" value="{}">',
|
||||
get_token(request),
|
||||
)
|
||||
get_token(request))
|
||||
|
||||
|
||||
csrf_input_lazy = lazy(csrf_input, SafeString, str)
|
||||
|
||||
Reference in New Issue
Block a user