2015-05-09 04:10:36 +08:00
|
|
|
import functools
|
|
|
|
from importlib import import_module
|
2019-05-07 10:02:08 +08:00
|
|
|
from inspect import getfullargspec, unwrap
|
2015-05-09 04:10:36 +08:00
|
|
|
|
2015-06-15 18:17:09 +08:00
|
|
|
from django.utils.html import conditional_escape
|
2015-05-09 04:10:36 +08:00
|
|
|
from django.utils.itercompat import is_iterable
|
|
|
|
|
|
|
|
from .base import Node, Template, token_kwargs
|
|
|
|
from .exceptions import TemplateSyntaxError
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidTemplateLibrary(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Library:
|
2015-05-09 04:10:36 +08:00
|
|
|
"""
|
|
|
|
A class for registering template tags and filters. Compiled filter and
|
|
|
|
template tag functions are stored in the filters and tags attributes.
|
|
|
|
The filter, simple_tag, and inclusion_tag methods provide a convenient
|
|
|
|
way to register callables as tags.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.filters = {}
|
|
|
|
self.tags = {}
|
|
|
|
|
|
|
|
def tag(self, name=None, compile_function=None):
|
|
|
|
if name is None and compile_function is None:
|
|
|
|
# @register.tag()
|
|
|
|
return self.tag_function
|
|
|
|
elif name is not None and compile_function is None:
|
|
|
|
if callable(name):
|
|
|
|
# @register.tag
|
|
|
|
return self.tag_function(name)
|
|
|
|
else:
|
|
|
|
# @register.tag('somename') or @register.tag(name='somename')
|
|
|
|
def dec(func):
|
|
|
|
return self.tag(name, func)
|
|
|
|
return dec
|
|
|
|
elif name is not None and compile_function is not None:
|
|
|
|
# register.tag('somename', somefunc)
|
|
|
|
self.tags[name] = compile_function
|
|
|
|
return compile_function
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"Unsupported arguments to Library.tag: (%r, %r)" %
|
|
|
|
(name, compile_function),
|
|
|
|
)
|
|
|
|
|
|
|
|
def tag_function(self, func):
|
2022-01-10 15:31:43 +08:00
|
|
|
self.tags[func.__name__] = func
|
2015-05-09 04:10:36 +08:00
|
|
|
return func
|
|
|
|
|
|
|
|
def filter(self, name=None, filter_func=None, **flags):
|
|
|
|
"""
|
|
|
|
Register a callable as a template filter. Example:
|
|
|
|
|
|
|
|
@register.filter
|
|
|
|
def lower(value):
|
|
|
|
return value.lower()
|
|
|
|
"""
|
|
|
|
if name is None and filter_func is None:
|
|
|
|
# @register.filter()
|
|
|
|
def dec(func):
|
|
|
|
return self.filter_function(func, **flags)
|
|
|
|
return dec
|
|
|
|
elif name is not None and filter_func is None:
|
|
|
|
if callable(name):
|
|
|
|
# @register.filter
|
|
|
|
return self.filter_function(name, **flags)
|
|
|
|
else:
|
|
|
|
# @register.filter('somename') or @register.filter(name='somename')
|
|
|
|
def dec(func):
|
|
|
|
return self.filter(name, func, **flags)
|
|
|
|
return dec
|
|
|
|
elif name is not None and filter_func is not None:
|
|
|
|
# register.filter('somename', somefunc)
|
|
|
|
self.filters[name] = filter_func
|
|
|
|
for attr in ('expects_localtime', 'is_safe', 'needs_autoescape'):
|
|
|
|
if attr in flags:
|
|
|
|
value = flags[attr]
|
|
|
|
# set the flag on the filter for FilterExpression.resolve
|
|
|
|
setattr(filter_func, attr, value)
|
|
|
|
# set the flag on the innermost decorated function
|
|
|
|
# for decorators that need it, e.g. stringfilter
|
2022-01-10 15:31:43 +08:00
|
|
|
setattr(unwrap(filter_func), attr, value)
|
2015-05-09 04:10:36 +08:00
|
|
|
filter_func._filter_name = name
|
|
|
|
return filter_func
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"Unsupported arguments to Library.filter: (%r, %r)" %
|
|
|
|
(name, filter_func),
|
|
|
|
)
|
|
|
|
|
|
|
|
def filter_function(self, func, **flags):
|
2022-01-10 15:31:43 +08:00
|
|
|
return self.filter(func.__name__, func, **flags)
|
2015-05-09 04:10:36 +08:00
|
|
|
|
|
|
|
def simple_tag(self, func=None, takes_context=None, name=None):
|
|
|
|
"""
|
|
|
|
Register a callable as a compiled template tag. Example:
|
|
|
|
|
|
|
|
@register.simple_tag
|
|
|
|
def hello(*args, **kwargs):
|
|
|
|
return 'world'
|
|
|
|
"""
|
|
|
|
def dec(func):
|
2019-05-07 10:02:08 +08:00
|
|
|
params, varargs, varkw, defaults, kwonly, kwonly_defaults, _ = getfullargspec(unwrap(func))
|
2022-01-10 15:31:43 +08:00
|
|
|
function_name = (name or func.__name__)
|
2015-05-09 04:10:36 +08:00
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def compile_func(parser, token):
|
|
|
|
bits = token.split_contents()[1:]
|
|
|
|
target_var = None
|
|
|
|
if len(bits) >= 2 and bits[-2] == 'as':
|
|
|
|
target_var = bits[-1]
|
|
|
|
bits = bits[:-2]
|
2016-03-29 06:33:29 +08:00
|
|
|
args, kwargs = parse_bits(
|
|
|
|
parser, bits, params, varargs, varkw, defaults,
|
2017-05-11 22:09:44 +08:00
|
|
|
kwonly, kwonly_defaults, takes_context, function_name,
|
2016-03-29 06:33:29 +08:00
|
|
|
)
|
2015-05-09 04:10:36 +08:00
|
|
|
return SimpleNode(func, takes_context, args, kwargs, target_var)
|
|
|
|
self.tag(function_name, compile_func)
|
|
|
|
return func
|
|
|
|
|
|
|
|
if func is None:
|
|
|
|
# @register.simple_tag(...)
|
|
|
|
return dec
|
|
|
|
elif callable(func):
|
|
|
|
# @register.simple_tag
|
|
|
|
return dec(func)
|
|
|
|
else:
|
|
|
|
raise ValueError("Invalid arguments provided to simple_tag")
|
|
|
|
|
|
|
|
def inclusion_tag(self, filename, func=None, takes_context=None, name=None):
|
|
|
|
"""
|
|
|
|
Register a callable as an inclusion tag:
|
|
|
|
|
|
|
|
@register.inclusion_tag('results.html')
|
|
|
|
def show_results(poll):
|
|
|
|
choices = poll.choice_set.all()
|
|
|
|
return {'choices': choices}
|
|
|
|
"""
|
|
|
|
def dec(func):
|
2019-05-07 10:02:08 +08:00
|
|
|
params, varargs, varkw, defaults, kwonly, kwonly_defaults, _ = getfullargspec(unwrap(func))
|
2022-01-10 15:31:43 +08:00
|
|
|
function_name = name or func.__name__
|
2015-05-09 04:10:36 +08:00
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def compile_func(parser, token):
|
|
|
|
bits = token.split_contents()[1:]
|
|
|
|
args, kwargs = parse_bits(
|
|
|
|
parser, bits, params, varargs, varkw, defaults,
|
2017-05-11 22:09:44 +08:00
|
|
|
kwonly, kwonly_defaults, takes_context, function_name,
|
2015-05-09 04:10:36 +08:00
|
|
|
)
|
|
|
|
return InclusionNode(
|
|
|
|
func, takes_context, args, kwargs, filename,
|
|
|
|
)
|
|
|
|
self.tag(function_name, compile_func)
|
|
|
|
return func
|
|
|
|
return dec
|
|
|
|
|
|
|
|
|
|
|
|
class TagHelperNode(Node):
|
|
|
|
"""
|
|
|
|
Base class for tag helper nodes such as SimpleNode and InclusionNode.
|
|
|
|
Manages the positional and keyword arguments to be passed to the decorated
|
|
|
|
function.
|
|
|
|
"""
|
|
|
|
def __init__(self, func, takes_context, args, kwargs):
|
|
|
|
self.func = func
|
|
|
|
self.takes_context = takes_context
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
def get_resolved_arguments(self, context):
|
|
|
|
resolved_args = [var.resolve(context) for var in self.args]
|
|
|
|
if self.takes_context:
|
|
|
|
resolved_args = [context] + resolved_args
|
|
|
|
resolved_kwargs = {k: v.resolve(context) for k, v in self.kwargs.items()}
|
|
|
|
return resolved_args, resolved_kwargs
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleNode(TagHelperNode):
|
2021-07-06 21:53:03 +08:00
|
|
|
child_nodelists = ()
|
2015-05-09 04:10:36 +08:00
|
|
|
|
|
|
|
def __init__(self, func, takes_context, args, kwargs, target_var):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(func, takes_context, args, kwargs)
|
2015-05-09 04:10:36 +08:00
|
|
|
self.target_var = target_var
|
|
|
|
|
|
|
|
def render(self, context):
|
|
|
|
resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
|
|
|
|
output = self.func(*resolved_args, **resolved_kwargs)
|
|
|
|
if self.target_var is not None:
|
|
|
|
context[self.target_var] = output
|
|
|
|
return ''
|
2015-06-15 18:17:09 +08:00
|
|
|
if context.autoescape:
|
|
|
|
output = conditional_escape(output)
|
2015-05-09 04:10:36 +08:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
class InclusionNode(TagHelperNode):
|
|
|
|
|
|
|
|
def __init__(self, func, takes_context, args, kwargs, filename):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(func, takes_context, args, kwargs)
|
2015-05-09 04:10:36 +08:00
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
def render(self, context):
|
|
|
|
"""
|
|
|
|
Render the specified template and context. Cache the template object
|
|
|
|
in render_context to avoid reparsing and loading when used in a for
|
|
|
|
loop.
|
|
|
|
"""
|
|
|
|
resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
|
|
|
|
_dict = self.func(*resolved_args, **resolved_kwargs)
|
|
|
|
|
|
|
|
t = context.render_context.get(self)
|
|
|
|
if t is None:
|
|
|
|
if isinstance(self.filename, Template):
|
|
|
|
t = self.filename
|
|
|
|
elif isinstance(getattr(self.filename, 'template', None), Template):
|
|
|
|
t = self.filename.template
|
2016-12-29 23:27:49 +08:00
|
|
|
elif not isinstance(self.filename, str) and is_iterable(self.filename):
|
2015-05-09 04:10:36 +08:00
|
|
|
t = context.template.engine.select_template(self.filename)
|
|
|
|
else:
|
|
|
|
t = context.template.engine.get_template(self.filename)
|
|
|
|
context.render_context[self] = t
|
|
|
|
new_context = context.new(_dict)
|
|
|
|
# Copy across the CSRF token, if present, because inclusion tags are
|
|
|
|
# often used for forms, and we need instructions for using CSRF
|
|
|
|
# protection to be as simple as possible.
|
|
|
|
csrf_token = context.get('csrf_token')
|
|
|
|
if csrf_token is not None:
|
|
|
|
new_context['csrf_token'] = csrf_token
|
|
|
|
return t.render(new_context)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_bits(parser, bits, params, varargs, varkw, defaults,
|
2017-05-11 22:09:44 +08:00
|
|
|
kwonly, kwonly_defaults, takes_context, name):
|
2015-05-09 04:10:36 +08:00
|
|
|
"""
|
|
|
|
Parse bits for template tag helpers simple_tag and inclusion_tag, in
|
|
|
|
particular by detecting syntax errors and by extracting positional and
|
|
|
|
keyword arguments.
|
|
|
|
"""
|
|
|
|
if takes_context:
|
2021-08-19 05:11:42 +08:00
|
|
|
if params and params[0] == 'context':
|
2015-05-09 04:10:36 +08:00
|
|
|
params = params[1:]
|
|
|
|
else:
|
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' is decorated with takes_context=True so it must "
|
|
|
|
"have a first argument of 'context'" % name)
|
|
|
|
args = []
|
|
|
|
kwargs = {}
|
|
|
|
unhandled_params = list(params)
|
2017-05-11 22:09:44 +08:00
|
|
|
unhandled_kwargs = [
|
|
|
|
kwarg for kwarg in kwonly
|
|
|
|
if not kwonly_defaults or kwarg not in kwonly_defaults
|
|
|
|
]
|
2015-05-09 04:10:36 +08:00
|
|
|
for bit in bits:
|
|
|
|
# First we try to extract a potential kwarg from the bit
|
|
|
|
kwarg = token_kwargs([bit], parser)
|
|
|
|
if kwarg:
|
|
|
|
# The kwarg was successfully extracted
|
|
|
|
param, value = kwarg.popitem()
|
2019-12-30 05:04:14 +08:00
|
|
|
if param not in params and param not in kwonly and varkw is None:
|
2015-05-09 04:10:36 +08:00
|
|
|
# An unexpected keyword argument was supplied
|
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' received unexpected keyword argument '%s'" %
|
|
|
|
(name, param))
|
|
|
|
elif param in kwargs:
|
|
|
|
# The keyword argument has already been supplied once
|
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' received multiple values for keyword argument '%s'" %
|
|
|
|
(name, param))
|
|
|
|
else:
|
|
|
|
# All good, record the keyword argument
|
|
|
|
kwargs[str(param)] = value
|
|
|
|
if param in unhandled_params:
|
|
|
|
# If using the keyword syntax for a positional arg, then
|
|
|
|
# consume it.
|
|
|
|
unhandled_params.remove(param)
|
2017-05-11 22:09:44 +08:00
|
|
|
elif param in unhandled_kwargs:
|
|
|
|
# Same for keyword-only arguments
|
|
|
|
unhandled_kwargs.remove(param)
|
2015-05-09 04:10:36 +08:00
|
|
|
else:
|
|
|
|
if kwargs:
|
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' received some positional argument(s) after some "
|
|
|
|
"keyword argument(s)" % name)
|
|
|
|
else:
|
|
|
|
# Record the positional argument
|
|
|
|
args.append(parser.compile_filter(bit))
|
|
|
|
try:
|
|
|
|
# Consume from the list of expected positional arguments
|
|
|
|
unhandled_params.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
if varargs is None:
|
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' received too many positional arguments" %
|
|
|
|
name)
|
|
|
|
if defaults is not None:
|
|
|
|
# Consider the last n params handled, where n is the
|
|
|
|
# number of defaults.
|
|
|
|
unhandled_params = unhandled_params[:-len(defaults)]
|
2017-05-11 22:09:44 +08:00
|
|
|
if unhandled_params or unhandled_kwargs:
|
2015-05-09 04:10:36 +08:00
|
|
|
# Some positional arguments were not supplied
|
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' did not receive value(s) for the argument(s): %s" %
|
2017-05-11 22:09:44 +08:00
|
|
|
(name, ", ".join("'%s'" % p for p in unhandled_params + unhandled_kwargs)))
|
2015-05-09 04:10:36 +08:00
|
|
|
return args, kwargs
|
|
|
|
|
|
|
|
|
|
|
|
def import_library(name):
|
|
|
|
"""
|
|
|
|
Load a Library object from a template tag module.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
module = import_module(name)
|
|
|
|
except ImportError as e:
|
|
|
|
raise InvalidTemplateLibrary(
|
|
|
|
"Invalid template library specified. ImportError raised when "
|
|
|
|
"trying to load '%s': %s" % (name, e)
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
return module.register
|
|
|
|
except AttributeError:
|
|
|
|
raise InvalidTemplateLibrary(
|
|
|
|
"Module %s does not have a variable named 'register'" % name,
|
|
|
|
)
|