2014-11-11 04:40:26 +08:00
|
|
|
"""
|
|
|
|
This is the Django template system.
|
|
|
|
|
|
|
|
How it works:
|
|
|
|
|
|
|
|
The Lexer.tokenize() function converts a template string (i.e., a string containing
|
|
|
|
markup with custom template tags) to tokens, which can be either plain text
|
|
|
|
(TOKEN_TEXT), variables (TOKEN_VAR) or block statements (TOKEN_BLOCK).
|
|
|
|
|
|
|
|
The Parser() class takes a list of tokens in its constructor, and its parse()
|
|
|
|
method returns a compiled template -- which is, under the hood, a list of
|
|
|
|
Node objects.
|
|
|
|
|
|
|
|
Each Node is responsible for creating some sort of output -- e.g. simple text
|
|
|
|
(TextNode), variable values in a given context (VariableNode), results of basic
|
|
|
|
logic (IfNode), results of looping (ForNode), or anything else. The core Node
|
|
|
|
types are TextNode, VariableNode, IfNode and ForNode, but plugin modules can
|
|
|
|
define their own custom node types.
|
|
|
|
|
|
|
|
Each Node has a render() method, which takes a Context and returns a string of
|
|
|
|
the rendered node. For example, the render() method of a Variable Node returns
|
|
|
|
the variable's value as a string. The render() method of a ForNode returns the
|
|
|
|
rendered output of whatever was inside the loop, recursively.
|
|
|
|
|
|
|
|
The Template class is a convenient wrapper that takes care of template
|
|
|
|
compilation and rendering.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
The only thing you should ever use directly in this file is the Template class.
|
|
|
|
Create a compiled template object with a template_string, then call render()
|
|
|
|
with a context. In the compilation stage, the TemplateSyntaxError exception
|
|
|
|
will be raised if the template doesn't have proper syntax.
|
|
|
|
|
|
|
|
Sample code:
|
|
|
|
|
|
|
|
>>> from django import template
|
|
|
|
>>> s = u'<html>{% if test %}<h1>{{ varvalue }}</h1>{% endif %}</html>'
|
|
|
|
>>> t = template.Template(s)
|
|
|
|
|
|
|
|
(t is now a compiled template, and its render() method can be called multiple
|
|
|
|
times with multiple contexts)
|
|
|
|
|
|
|
|
>>> c = template.Context({'test':True, 'varvalue': 'Hello'})
|
|
|
|
>>> t.render(c)
|
|
|
|
u'<html><h1>Hello</h1></html>'
|
|
|
|
>>> c = template.Context({'test':False, 'varvalue': 'Hello'})
|
|
|
|
>>> t.render(c)
|
|
|
|
u'<html></html>'
|
|
|
|
"""
|
|
|
|
|
2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-18 00:56:18 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
import re
|
2011-03-28 10:11:19 +08:00
|
|
|
from functools import partial
|
2013-07-29 21:50:58 +08:00
|
|
|
from importlib import import_module
|
2013-09-08 21:07:12 +08:00
|
|
|
from inspect import getargspec, getcallargs
|
2014-07-01 21:32:26 +08:00
|
|
|
import warnings
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2013-11-09 23:17:17 +08:00
|
|
|
from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility
|
2011-09-27 20:15:15 +08:00
|
|
|
ContextPopException)
|
2014-11-19 04:50:52 +08:00
|
|
|
from django.utils import lru_cache
|
2014-07-01 21:32:26 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.utils.itercompat import is_iterable
|
2011-09-27 20:15:15 +08:00
|
|
|
from django.utils.text import (smart_split, unescape_string_literal,
|
|
|
|
get_text_list)
|
2012-08-30 04:40:51 +08:00
|
|
|
from django.utils.encoding import force_str, force_text
|
2011-10-19 12:59:47 +08:00
|
|
|
from django.utils.translation import ugettext_lazy, pgettext_lazy
|
2011-09-27 20:15:15 +08:00
|
|
|
from django.utils.safestring import (SafeData, EscapeData, mark_safe,
|
|
|
|
mark_for_escaping)
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.utils.formats import localize
|
2014-12-24 05:29:01 +08:00
|
|
|
from django.utils.html import conditional_escape
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.utils.module_loading import module_has_submodule
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2012-04-29 21:37:23 +08:00
|
|
|
from django.utils.timezone import template_localtime
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
TOKEN_TEXT = 0
|
|
|
|
TOKEN_VAR = 1
|
|
|
|
TOKEN_BLOCK = 2
|
|
|
|
TOKEN_COMMENT = 3
|
2011-09-27 20:15:15 +08:00
|
|
|
TOKEN_MAPPING = {
|
|
|
|
TOKEN_TEXT: 'Text',
|
|
|
|
TOKEN_VAR: 'Var',
|
|
|
|
TOKEN_BLOCK: 'Block',
|
|
|
|
TOKEN_COMMENT: 'Comment',
|
|
|
|
}
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
# template syntax constants
|
|
|
|
FILTER_SEPARATOR = '|'
|
|
|
|
FILTER_ARGUMENT_SEPARATOR = ':'
|
|
|
|
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
|
|
|
|
BLOCK_TAG_START = '{%'
|
|
|
|
BLOCK_TAG_END = '%}'
|
|
|
|
VARIABLE_TAG_START = '{{'
|
|
|
|
VARIABLE_TAG_END = '}}'
|
|
|
|
COMMENT_TAG_START = '{#'
|
|
|
|
COMMENT_TAG_END = '#}'
|
|
|
|
TRANSLATOR_COMMENT_MARK = 'Translators'
|
|
|
|
SINGLE_BRACE_START = '{'
|
|
|
|
SINGLE_BRACE_END = '}'
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
ALLOWED_VARIABLE_CHARS = ('abcdefghijklmnopqrstuvwxyz'
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.')
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
# what to report as the origin for templates that come from non-loader sources
|
|
|
|
# (e.g. strings)
|
|
|
|
UNKNOWN_SOURCE = '<unknown source>'
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
# match a variable or block tag and capture the entire tag, including start/end
|
|
|
|
# delimiters
|
|
|
|
tag_re = (re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' %
|
|
|
|
(re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),
|
|
|
|
re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
|
|
|
|
re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END))))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
# global dictionary of libraries that have been loaded using get_library
|
|
|
|
libraries = {}
|
|
|
|
# global list of libraries to load by default for a new parser
|
|
|
|
builtins = []
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class TemplateSyntaxError(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class TemplateDoesNotExist(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class TemplateEncodingError(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2010-11-27 13:47:30 +08:00
|
|
|
class VariableDoesNotExist(Exception):
|
|
|
|
|
|
|
|
def __init__(self, msg, params=()):
|
|
|
|
self.msg = msg
|
|
|
|
self.params = params
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2013-12-13 04:23:24 +08:00
|
|
|
return self.msg % tuple(force_text(p, errors='replace') for p in self.params)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class InvalidTemplateLibrary(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Origin(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def reload(self):
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Origin must provide a reload() method')
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class StringOrigin(Origin):
|
|
|
|
def __init__(self, source):
|
|
|
|
super(StringOrigin, self).__init__(UNKNOWN_SOURCE)
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
return self.source
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Template(object):
|
2014-11-14 22:48:27 +08:00
|
|
|
def __init__(self, template_string, origin=None, name=None, engine=None):
|
2010-11-27 13:47:30 +08:00
|
|
|
try:
|
2012-08-30 04:40:51 +08:00
|
|
|
template_string = force_text(template_string)
|
2010-11-27 13:47:30 +08:00
|
|
|
except UnicodeDecodeError:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateEncodingError("Templates can only be constructed "
|
|
|
|
"from unicode or UTF-8 strings.")
|
2014-11-29 06:04:19 +08:00
|
|
|
# If Template is instantiated directly rather than from an Engine and
|
|
|
|
# exactly one Django template engine is configured, use that engine.
|
|
|
|
# This is required to preserve backwards-compatibility for direct use
|
|
|
|
# e.g. Template('...').render(Context({...}))
|
2014-11-14 22:48:27 +08:00
|
|
|
if engine is None:
|
|
|
|
from .engine import Engine
|
|
|
|
engine = Engine.get_default()
|
2014-11-23 04:33:40 +08:00
|
|
|
if engine.debug and origin is None:
|
|
|
|
origin = StringOrigin(template_string)
|
2014-11-23 04:21:30 +08:00
|
|
|
self.nodelist = engine.compile_string(template_string, origin)
|
2010-11-27 13:47:30 +08:00
|
|
|
self.name = name
|
2013-08-31 03:08:40 +08:00
|
|
|
self.origin = origin
|
2014-11-14 22:48:27 +08:00
|
|
|
self.engine = engine
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for node in self.nodelist:
|
|
|
|
for subnode in node:
|
|
|
|
yield subnode
|
|
|
|
|
|
|
|
def _render(self, context):
|
|
|
|
return self.nodelist.render(context)
|
|
|
|
|
|
|
|
def render(self, context):
|
|
|
|
"Display stage -- can be called many times"
|
2014-11-21 05:12:21 +08:00
|
|
|
# Set engine attribute here to avoid changing the signature of either
|
|
|
|
# Context.__init__ or Node.render. The engine is set only on the first
|
|
|
|
# call to render. Further calls e.g. for includes don't override it.
|
|
|
|
toplevel_render = context.engine is None
|
|
|
|
if toplevel_render:
|
|
|
|
context.engine = self.engine
|
2010-11-27 13:47:30 +08:00
|
|
|
context.render_context.push()
|
|
|
|
try:
|
|
|
|
return self._render(context)
|
|
|
|
finally:
|
|
|
|
context.render_context.pop()
|
2014-11-21 05:12:21 +08:00
|
|
|
if toplevel_render:
|
|
|
|
context.engine = None
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Token(object):
|
|
|
|
def __init__(self, token_type, contents):
|
2011-09-27 20:15:15 +08:00
|
|
|
# token_type must be TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK or
|
|
|
|
# TOKEN_COMMENT.
|
2010-11-27 13:47:30 +08:00
|
|
|
self.token_type, self.contents = token_type, contents
|
2010-12-05 01:42:54 +08:00
|
|
|
self.lineno = None
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
token_name = TOKEN_MAPPING[self.token_type]
|
|
|
|
return ('<%s token: "%s...">' %
|
|
|
|
(token_name, self.contents[:20].replace('\n', '')))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def split_contents(self):
|
|
|
|
split = []
|
|
|
|
bits = iter(smart_split(self.contents))
|
|
|
|
for bit in bits:
|
|
|
|
# Handle translation-marked template pieces
|
|
|
|
if bit.startswith('_("') or bit.startswith("_('"):
|
|
|
|
sentinal = bit[2] + ')'
|
|
|
|
trans_bit = [bit]
|
|
|
|
while not bit.endswith(sentinal):
|
2012-05-11 02:14:04 +08:00
|
|
|
bit = next(bits)
|
2010-11-27 13:47:30 +08:00
|
|
|
trans_bit.append(bit)
|
|
|
|
bit = ' '.join(trans_bit)
|
|
|
|
split.append(bit)
|
|
|
|
return split
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Lexer(object):
|
|
|
|
def __init__(self, template_string, origin):
|
|
|
|
self.template_string = template_string
|
|
|
|
self.origin = origin
|
2010-12-05 01:42:54 +08:00
|
|
|
self.lineno = 1
|
2012-06-07 15:59:14 +08:00
|
|
|
self.verbatim = False
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def tokenize(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Return a list of tokens from a given template_string.
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
in_tag = False
|
|
|
|
result = []
|
|
|
|
for bit in tag_re.split(self.template_string):
|
|
|
|
if bit:
|
|
|
|
result.append(self.create_token(bit, in_tag))
|
|
|
|
in_tag = not in_tag
|
|
|
|
return result
|
|
|
|
|
|
|
|
def create_token(self, token_string, in_tag):
|
|
|
|
"""
|
|
|
|
Convert the given token string into a new Token object and return it.
|
|
|
|
If in_tag is True, we are processing something that matched a tag,
|
|
|
|
otherwise it should be treated as a literal string.
|
|
|
|
"""
|
2012-06-07 15:59:14 +08:00
|
|
|
if in_tag and token_string.startswith(BLOCK_TAG_START):
|
2012-01-28 07:46:22 +08:00
|
|
|
# The [2:-2] ranges below strip off *_TAG_START and *_TAG_END.
|
|
|
|
# We could do len(BLOCK_TAG_START) to be more "correct", but we've
|
|
|
|
# hard-coded the 2s here for performance. And it's not like
|
|
|
|
# the TAG_START values are going to change anytime, anyway.
|
2012-06-07 15:59:14 +08:00
|
|
|
block_content = token_string[2:-2].strip()
|
|
|
|
if self.verbatim and block_content == self.verbatim:
|
|
|
|
self.verbatim = False
|
|
|
|
if in_tag and not self.verbatim:
|
2010-11-27 13:47:30 +08:00
|
|
|
if token_string.startswith(VARIABLE_TAG_START):
|
2012-01-28 07:46:22 +08:00
|
|
|
token = Token(TOKEN_VAR, token_string[2:-2].strip())
|
2010-11-27 13:47:30 +08:00
|
|
|
elif token_string.startswith(BLOCK_TAG_START):
|
2012-06-19 06:49:30 +08:00
|
|
|
if block_content[:9] in ('verbatim', 'verbatim '):
|
|
|
|
self.verbatim = 'end%s' % block_content
|
2012-06-07 15:59:14 +08:00
|
|
|
token = Token(TOKEN_BLOCK, block_content)
|
2010-11-27 13:47:30 +08:00
|
|
|
elif token_string.startswith(COMMENT_TAG_START):
|
|
|
|
content = ''
|
|
|
|
if token_string.find(TRANSLATOR_COMMENT_MARK):
|
2012-01-28 07:46:22 +08:00
|
|
|
content = token_string[2:-2].strip()
|
2010-11-27 13:47:30 +08:00
|
|
|
token = Token(TOKEN_COMMENT, content)
|
|
|
|
else:
|
|
|
|
token = Token(TOKEN_TEXT, token_string)
|
2010-12-05 01:42:54 +08:00
|
|
|
token.lineno = self.lineno
|
|
|
|
self.lineno += token_string.count('\n')
|
2010-11-27 13:47:30 +08:00
|
|
|
return token
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Parser(object):
|
|
|
|
def __init__(self, tokens):
|
|
|
|
self.tokens = tokens
|
|
|
|
self.tags = {}
|
|
|
|
self.filters = {}
|
|
|
|
for lib in builtins:
|
|
|
|
self.add_library(lib)
|
|
|
|
|
|
|
|
def parse(self, parse_until=None):
|
2011-09-27 20:15:15 +08:00
|
|
|
if parse_until is None:
|
|
|
|
parse_until = []
|
2010-11-27 13:47:30 +08:00
|
|
|
nodelist = self.create_nodelist()
|
|
|
|
while self.tokens:
|
|
|
|
token = self.next_token()
|
2012-01-28 09:50:14 +08:00
|
|
|
# Use the raw values here for TOKEN_* for a tiny performance boost.
|
2013-11-03 03:27:47 +08:00
|
|
|
if token.token_type == 0: # TOKEN_TEXT
|
2010-11-27 13:47:30 +08:00
|
|
|
self.extend_nodelist(nodelist, TextNode(token.contents), token)
|
2013-11-03 03:27:47 +08:00
|
|
|
elif token.token_type == 1: # TOKEN_VAR
|
2010-11-27 13:47:30 +08:00
|
|
|
if not token.contents:
|
|
|
|
self.empty_variable(token)
|
2013-02-14 16:45:55 +08:00
|
|
|
try:
|
|
|
|
filter_expression = self.compile_filter(token.contents)
|
|
|
|
except TemplateSyntaxError as e:
|
|
|
|
if not self.compile_filter_error(token, e):
|
|
|
|
raise
|
2010-11-27 13:47:30 +08:00
|
|
|
var_node = self.create_variable_node(filter_expression)
|
2011-09-27 20:15:15 +08:00
|
|
|
self.extend_nodelist(nodelist, var_node, token)
|
2013-11-03 03:27:47 +08:00
|
|
|
elif token.token_type == 2: # TOKEN_BLOCK
|
2010-11-27 13:47:30 +08:00
|
|
|
try:
|
|
|
|
command = token.contents.split()[0]
|
|
|
|
except IndexError:
|
|
|
|
self.empty_block_tag(token)
|
2011-12-10 06:10:52 +08:00
|
|
|
if command in parse_until:
|
|
|
|
# put token back on token list so calling
|
|
|
|
# code knows why it terminated
|
|
|
|
self.prepend_token(token)
|
|
|
|
return nodelist
|
2011-09-27 20:15:15 +08:00
|
|
|
# execute callback function for this tag and append
|
|
|
|
# resulting node
|
2010-11-27 13:47:30 +08:00
|
|
|
self.enter_command(command, token)
|
|
|
|
try:
|
|
|
|
compile_func = self.tags[command]
|
|
|
|
except KeyError:
|
|
|
|
self.invalid_block_tag(token, command, parse_until)
|
|
|
|
try:
|
|
|
|
compiled_result = compile_func(self, token)
|
2012-04-29 00:09:37 +08:00
|
|
|
except TemplateSyntaxError as e:
|
2010-11-27 13:47:30 +08:00
|
|
|
if not self.compile_function_error(token, e):
|
|
|
|
raise
|
|
|
|
self.extend_nodelist(nodelist, compiled_result, token)
|
|
|
|
self.exit_command()
|
|
|
|
if parse_until:
|
|
|
|
self.unclosed_block_tag(parse_until)
|
|
|
|
return nodelist
|
|
|
|
|
|
|
|
def skip_past(self, endtag):
|
|
|
|
while self.tokens:
|
|
|
|
token = self.next_token()
|
|
|
|
if token.token_type == TOKEN_BLOCK and token.contents == endtag:
|
|
|
|
return
|
|
|
|
self.unclosed_block_tag([endtag])
|
|
|
|
|
|
|
|
def create_variable_node(self, filter_expression):
|
|
|
|
return VariableNode(filter_expression)
|
|
|
|
|
|
|
|
def create_nodelist(self):
|
|
|
|
return NodeList()
|
|
|
|
|
|
|
|
def extend_nodelist(self, nodelist, node, token):
|
|
|
|
if node.must_be_first and nodelist:
|
|
|
|
try:
|
|
|
|
if nodelist.contains_nontext:
|
|
|
|
raise AttributeError
|
|
|
|
except AttributeError:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("%r must be the first tag "
|
|
|
|
"in the template." % node)
|
2010-11-27 13:47:30 +08:00
|
|
|
if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
|
|
|
|
nodelist.contains_nontext = True
|
|
|
|
nodelist.append(node)
|
|
|
|
|
|
|
|
def enter_command(self, command, token):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def exit_command(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def error(self, token, msg):
|
|
|
|
return TemplateSyntaxError(msg)
|
|
|
|
|
|
|
|
def empty_variable(self, token):
|
|
|
|
raise self.error(token, "Empty variable tag")
|
|
|
|
|
|
|
|
def empty_block_tag(self, token):
|
|
|
|
raise self.error(token, "Empty block tag")
|
|
|
|
|
|
|
|
def invalid_block_tag(self, token, command, parse_until=None):
|
|
|
|
if parse_until:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise self.error(token, "Invalid block tag: '%s', expected %s" %
|
|
|
|
(command, get_text_list(["'%s'" % p for p in parse_until])))
|
2010-11-27 13:47:30 +08:00
|
|
|
raise self.error(token, "Invalid block tag: '%s'" % command)
|
|
|
|
|
|
|
|
def unclosed_block_tag(self, parse_until):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise self.error(None, "Unclosed tags: %s " % ', '.join(parse_until))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-02-14 16:45:55 +08:00
|
|
|
def compile_filter_error(self, token, e):
|
|
|
|
pass
|
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
def compile_function_error(self, token, e):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def next_token(self):
|
|
|
|
return self.tokens.pop(0)
|
|
|
|
|
|
|
|
def prepend_token(self, token):
|
|
|
|
self.tokens.insert(0, token)
|
|
|
|
|
|
|
|
def delete_first_token(self):
|
|
|
|
del self.tokens[0]
|
|
|
|
|
|
|
|
def add_library(self, lib):
|
|
|
|
self.tags.update(lib.tags)
|
|
|
|
self.filters.update(lib.filters)
|
|
|
|
|
|
|
|
def compile_filter(self, token):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Convenient wrapper for FilterExpression
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
return FilterExpression(token, self)
|
|
|
|
|
|
|
|
def find_filter(self, filter_name):
|
|
|
|
if filter_name in self.filters:
|
|
|
|
return self.filters[filter_name]
|
|
|
|
else:
|
|
|
|
raise TemplateSyntaxError("Invalid filter: '%s'" % filter_name)
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class TokenParser(object):
|
|
|
|
"""
|
2011-09-27 20:15:15 +08:00
|
|
|
Subclass this and implement the top() method to parse a template line.
|
|
|
|
When instantiating the parser, pass in the line from the Django template
|
|
|
|
parser.
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
The parser's "tagname" instance-variable stores the name of the tag that
|
|
|
|
the filter was called with.
|
|
|
|
"""
|
|
|
|
def __init__(self, subject):
|
|
|
|
self.subject = subject
|
|
|
|
self.pointer = 0
|
|
|
|
self.backout = []
|
|
|
|
self.tagname = self.tag()
|
|
|
|
|
|
|
|
def top(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Overload this method to do the actual parsing and return the result.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Tokenparser must provide a top() method')
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def more(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Returns True if there is more stuff in the tag.
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
return self.pointer < len(self.subject)
|
|
|
|
|
|
|
|
def back(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Undoes the last microparser. Use this for lookahead and backtracking.
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
if not len(self.backout):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("back called without some previous "
|
|
|
|
"parsing")
|
2010-11-27 13:47:30 +08:00
|
|
|
self.pointer = self.backout.pop()
|
|
|
|
|
|
|
|
def tag(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
A microparser that just returns the next tag from the line.
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
subject = self.subject
|
|
|
|
i = self.pointer
|
|
|
|
if i >= len(subject):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("expected another tag, found "
|
|
|
|
"end of string: %s" % subject)
|
2010-11-27 13:47:30 +08:00
|
|
|
p = i
|
|
|
|
while i < len(subject) and subject[i] not in (' ', '\t'):
|
|
|
|
i += 1
|
|
|
|
s = subject[p:i]
|
|
|
|
while i < len(subject) and subject[i] in (' ', '\t'):
|
|
|
|
i += 1
|
|
|
|
self.backout.append(self.pointer)
|
|
|
|
self.pointer = i
|
|
|
|
return s
|
|
|
|
|
|
|
|
def value(self):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
A microparser that parses for a value: some string constant or
|
|
|
|
variable name.
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
subject = self.subject
|
|
|
|
i = self.pointer
|
|
|
|
|
|
|
|
def next_space_index(subject, i):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Increment pointer until a real space (i.e. a space not within
|
|
|
|
quotes) is encountered
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
while i < len(subject) and subject[i] not in (' ', '\t'):
|
|
|
|
if subject[i] in ('"', "'"):
|
|
|
|
c = subject[i]
|
|
|
|
i += 1
|
|
|
|
while i < len(subject) and subject[i] != c:
|
|
|
|
i += 1
|
|
|
|
if i >= len(subject):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Searching for value. "
|
|
|
|
"Unexpected end of string in column %d: %s" %
|
|
|
|
(i, subject))
|
2010-11-27 13:47:30 +08:00
|
|
|
i += 1
|
|
|
|
return i
|
|
|
|
|
|
|
|
if i >= len(subject):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Searching for value. Expected another "
|
|
|
|
"value but found end of string: %s" %
|
|
|
|
subject)
|
2010-11-27 13:47:30 +08:00
|
|
|
if subject[i] in ('"', "'"):
|
|
|
|
p = i
|
|
|
|
i += 1
|
|
|
|
while i < len(subject) and subject[i] != subject[p]:
|
|
|
|
i += 1
|
|
|
|
if i >= len(subject):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Searching for value. Unexpected "
|
|
|
|
"end of string in column %d: %s" %
|
|
|
|
(i, subject))
|
2010-11-27 13:47:30 +08:00
|
|
|
i += 1
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
# Continue parsing until next "real" space,
|
|
|
|
# so that filters are also included
|
2010-11-27 13:47:30 +08:00
|
|
|
i = next_space_index(subject, i)
|
|
|
|
|
|
|
|
res = subject[p:i]
|
|
|
|
while i < len(subject) and subject[i] in (' ', '\t'):
|
|
|
|
i += 1
|
|
|
|
self.backout.append(self.pointer)
|
|
|
|
self.pointer = i
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
p = i
|
|
|
|
i = next_space_index(subject, i)
|
|
|
|
s = subject[p:i]
|
|
|
|
while i < len(subject) and subject[i] in (' ', '\t'):
|
|
|
|
i += 1
|
|
|
|
self.backout.append(self.pointer)
|
|
|
|
self.pointer = i
|
|
|
|
return s
|
|
|
|
|
|
|
|
# This only matches constant *strings* (things in quotes or marked for
|
|
|
|
# translation). Numbers are treated as variables for implementation reasons
|
|
|
|
# (so that they retain their type when passed to filters).
|
|
|
|
constant_string = r"""
|
|
|
|
(?:%(i18n_open)s%(strdq)s%(i18n_close)s|
|
|
|
|
%(i18n_open)s%(strsq)s%(i18n_close)s|
|
|
|
|
%(strdq)s|
|
|
|
|
%(strsq)s)
|
|
|
|
""" % {
|
2011-09-27 20:15:15 +08:00
|
|
|
'strdq': r'"[^"\\]*(?:\\.[^"\\]*)*"', # double-quoted string
|
|
|
|
'strsq': r"'[^'\\]*(?:\\.[^'\\]*)*'", # single-quoted string
|
|
|
|
'i18n_open': re.escape("_("),
|
|
|
|
'i18n_close': re.escape(")"),
|
2013-10-18 17:02:43 +08:00
|
|
|
}
|
2010-11-27 13:47:30 +08:00
|
|
|
constant_string = constant_string.replace("\n", "")
|
|
|
|
|
|
|
|
filter_raw_string = r"""
|
|
|
|
^(?P<constant>%(constant)s)|
|
|
|
|
^(?P<var>[%(var_chars)s]+|%(num)s)|
|
2012-05-01 03:01:06 +08:00
|
|
|
(?:\s*%(filter_sep)s\s*
|
2010-11-27 13:47:30 +08:00
|
|
|
(?P<filter_name>\w+)
|
|
|
|
(?:%(arg_sep)s
|
|
|
|
(?:
|
|
|
|
(?P<constant_arg>%(constant)s)|
|
|
|
|
(?P<var_arg>[%(var_chars)s]+|%(num)s)
|
|
|
|
)
|
|
|
|
)?
|
|
|
|
)""" % {
|
|
|
|
'constant': constant_string,
|
|
|
|
'num': r'[-+\.]?\d[\d\.e]*',
|
2011-09-27 20:15:15 +08:00
|
|
|
'var_chars': "\w\.",
|
2010-11-27 13:47:30 +08:00
|
|
|
'filter_sep': re.escape(FILTER_SEPARATOR),
|
|
|
|
'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR),
|
2013-10-18 06:27:45 +08:00
|
|
|
}
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
filter_re = re.compile(filter_raw_string, re.UNICODE | re.VERBOSE)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class FilterExpression(object):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
Parses a variable token and its optional filters (all as a single string),
|
|
|
|
and return a list of tuples of the filter name and arguments.
|
2011-09-27 20:15:15 +08:00
|
|
|
Sample::
|
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
>>> token = 'variable|default:"Default value"|date:"Y-m-d"'
|
|
|
|
>>> p = Parser('')
|
|
|
|
>>> fe = FilterExpression(token, p)
|
|
|
|
>>> len(fe.filters)
|
|
|
|
2
|
|
|
|
>>> fe.var
|
|
|
|
<Variable: 'variable'>
|
|
|
|
"""
|
|
|
|
def __init__(self, token, parser):
|
|
|
|
self.token = token
|
|
|
|
matches = filter_re.finditer(token)
|
|
|
|
var_obj = None
|
|
|
|
filters = []
|
|
|
|
upto = 0
|
|
|
|
for match in matches:
|
|
|
|
start = match.start()
|
|
|
|
if upto != start:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Could not parse some characters: "
|
|
|
|
"%s|%s|%s" %
|
|
|
|
(token[:upto], token[upto:start],
|
|
|
|
token[start:]))
|
2010-11-27 13:47:30 +08:00
|
|
|
if var_obj is None:
|
|
|
|
var, constant = match.group("var", "constant")
|
|
|
|
if constant:
|
|
|
|
try:
|
|
|
|
var_obj = Variable(constant).resolve({})
|
|
|
|
except VariableDoesNotExist:
|
|
|
|
var_obj = None
|
|
|
|
elif var is None:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Could not find variable at "
|
|
|
|
"start of %s." % token)
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
|
|
|
var_obj = Variable(var)
|
|
|
|
else:
|
|
|
|
filter_name = match.group("filter_name")
|
|
|
|
args = []
|
|
|
|
constant_arg, var_arg = match.group("constant_arg", "var_arg")
|
|
|
|
if constant_arg:
|
|
|
|
args.append((False, Variable(constant_arg).resolve({})))
|
|
|
|
elif var_arg:
|
|
|
|
args.append((True, Variable(var_arg)))
|
|
|
|
filter_func = parser.find_filter(filter_name)
|
|
|
|
self.args_check(filter_name, filter_func, args)
|
|
|
|
filters.append((filter_func, args))
|
|
|
|
upto = match.end()
|
|
|
|
if upto != len(token):
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Could not parse the remainder: '%s' "
|
|
|
|
"from '%s'" % (token[upto:], token))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
self.filters = filters
|
|
|
|
self.var = var_obj
|
|
|
|
|
|
|
|
def resolve(self, context, ignore_failures=False):
|
|
|
|
if isinstance(self.var, Variable):
|
|
|
|
try:
|
|
|
|
obj = self.var.resolve(context)
|
|
|
|
except VariableDoesNotExist:
|
|
|
|
if ignore_failures:
|
|
|
|
obj = None
|
|
|
|
else:
|
2014-11-23 01:52:55 +08:00
|
|
|
string_if_invalid = context.engine.string_if_invalid
|
|
|
|
if string_if_invalid:
|
|
|
|
if '%s' in string_if_invalid:
|
|
|
|
return string_if_invalid % self.var
|
|
|
|
else:
|
|
|
|
return string_if_invalid
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
2014-11-23 01:52:55 +08:00
|
|
|
obj = string_if_invalid
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
|
|
|
obj = self.var
|
|
|
|
for func, args in self.filters:
|
|
|
|
arg_vals = []
|
|
|
|
for lookup, arg in args:
|
|
|
|
if not lookup:
|
|
|
|
arg_vals.append(mark_safe(arg))
|
|
|
|
else:
|
|
|
|
arg_vals.append(arg.resolve(context))
|
2011-11-18 21:01:06 +08:00
|
|
|
if getattr(func, 'expects_localtime', False):
|
2012-04-29 21:37:23 +08:00
|
|
|
obj = template_localtime(obj, context.use_tz)
|
2010-11-27 13:47:30 +08:00
|
|
|
if getattr(func, 'needs_autoescape', False):
|
|
|
|
new_obj = func(obj, autoescape=context.autoescape, *arg_vals)
|
|
|
|
else:
|
|
|
|
new_obj = func(obj, *arg_vals)
|
|
|
|
if getattr(func, 'is_safe', False) and isinstance(obj, SafeData):
|
|
|
|
obj = mark_safe(new_obj)
|
|
|
|
elif isinstance(obj, EscapeData):
|
|
|
|
obj = mark_for_escaping(new_obj)
|
|
|
|
else:
|
|
|
|
obj = new_obj
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def args_check(name, func, provided):
|
|
|
|
provided = list(provided)
|
2013-09-04 18:53:55 +08:00
|
|
|
# First argument, filter input, is implied.
|
|
|
|
plen = len(provided) + 1
|
2010-11-27 13:47:30 +08:00
|
|
|
# Check to see if a decorator is providing the real function.
|
|
|
|
func = getattr(func, '_decorated_function', func)
|
|
|
|
args, varargs, varkw, defaults = getargspec(func)
|
2013-09-04 18:53:55 +08:00
|
|
|
alen = len(args)
|
|
|
|
dlen = len(defaults or [])
|
|
|
|
# Not enough OR Too many
|
|
|
|
if plen < (alen - dlen) or plen > alen:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("%s requires %d arguments, %d provided" %
|
2013-09-04 18:53:55 +08:00
|
|
|
(name, alen - dlen, plen))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
return True
|
|
|
|
args_check = staticmethod(args_check)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.token
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
def resolve_variable(path, context):
|
|
|
|
"""
|
|
|
|
Returns the resolved variable, which may contain attribute syntax, within
|
|
|
|
the given context.
|
|
|
|
|
|
|
|
Deprecated; use the Variable class instead.
|
|
|
|
"""
|
2014-07-01 21:32:26 +08:00
|
|
|
warnings.warn("resolve_variable() is deprecated. Use django.template."
|
|
|
|
"Variable(path).resolve(context) instead",
|
|
|
|
RemovedInDjango20Warning, stacklevel=2)
|
2010-11-27 13:47:30 +08:00
|
|
|
return Variable(path).resolve(context)
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Variable(object):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
A template variable, resolvable against a given context. The variable may
|
|
|
|
be a hard-coded string (if it begins and ends with single or double quote
|
2010-11-27 13:47:30 +08:00
|
|
|
marks)::
|
|
|
|
|
|
|
|
>>> c = {'article': {'section':u'News'}}
|
|
|
|
>>> Variable('article.section').resolve(c)
|
|
|
|
u'News'
|
|
|
|
>>> Variable('article').resolve(c)
|
|
|
|
{'section': u'News'}
|
|
|
|
>>> class AClass: pass
|
|
|
|
>>> c = AClass()
|
|
|
|
>>> c.article = AClass()
|
|
|
|
>>> c.article.section = u'News'
|
|
|
|
|
|
|
|
(The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, var):
|
|
|
|
self.var = var
|
|
|
|
self.literal = None
|
|
|
|
self.lookups = None
|
|
|
|
self.translate = False
|
2011-10-19 12:59:47 +08:00
|
|
|
self.message_context = None
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-09-19 21:27:19 +08:00
|
|
|
if not isinstance(var, six.string_types):
|
|
|
|
raise TypeError(
|
|
|
|
"Variable must be a string or number, got %s" % type(var))
|
2010-11-27 13:47:30 +08:00
|
|
|
try:
|
|
|
|
# First try to treat this variable as a number.
|
|
|
|
#
|
|
|
|
# Note that this could cause an OverflowError here that we're not
|
|
|
|
# catching. Since this should only happen at compile time, that's
|
|
|
|
# probably OK.
|
|
|
|
self.literal = float(var)
|
|
|
|
|
|
|
|
# So it's a float... is it an int? If the original value contained a
|
|
|
|
# dot or an "e" then it was a float, not an int.
|
|
|
|
if '.' not in var and 'e' not in var.lower():
|
|
|
|
self.literal = int(self.literal)
|
|
|
|
|
|
|
|
# "2." is invalid
|
|
|
|
if var.endswith('.'):
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
# A ValueError means that the variable isn't a number.
|
|
|
|
if var.startswith('_(') and var.endswith(')'):
|
|
|
|
# The result of the lookup should be translated at rendering
|
|
|
|
# time.
|
|
|
|
self.translate = True
|
|
|
|
var = var[2:-1]
|
|
|
|
# If it's wrapped with quotes (single or double), then
|
|
|
|
# we're also dealing with a literal.
|
|
|
|
try:
|
|
|
|
self.literal = mark_safe(unescape_string_literal(var))
|
|
|
|
except ValueError:
|
|
|
|
# Otherwise we'll set self.lookups so that resolve() knows we're
|
|
|
|
# dealing with a bonafide variable
|
|
|
|
if var.find(VARIABLE_ATTRIBUTE_SEPARATOR + '_') > -1 or var[0] == '_':
|
2011-09-27 20:15:15 +08:00
|
|
|
raise TemplateSyntaxError("Variables and attributes may "
|
|
|
|
"not begin with underscores: '%s'" %
|
|
|
|
var)
|
2010-11-27 13:47:30 +08:00
|
|
|
self.lookups = tuple(var.split(VARIABLE_ATTRIBUTE_SEPARATOR))
|
|
|
|
|
|
|
|
def resolve(self, context):
|
|
|
|
"""Resolve this variable against a given context."""
|
|
|
|
if self.lookups is not None:
|
|
|
|
# We're dealing with a variable that needs to be resolved
|
|
|
|
value = self._resolve_lookup(context)
|
|
|
|
else:
|
|
|
|
# We're dealing with a literal, so it's already been "resolved"
|
|
|
|
value = self.literal
|
|
|
|
if self.translate:
|
2011-10-19 12:59:47 +08:00
|
|
|
if self.message_context:
|
|
|
|
return pgettext_lazy(self.message_context, value)
|
|
|
|
else:
|
|
|
|
return ugettext_lazy(value)
|
2010-11-27 13:47:30 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s: %r>" % (self.__class__.__name__, self.var)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.var
|
|
|
|
|
|
|
|
def _resolve_lookup(self, context):
|
|
|
|
"""
|
|
|
|
Performs resolution of a real variable (i.e. not a literal) against the
|
|
|
|
given context.
|
|
|
|
|
|
|
|
As indicated by the method's name, this method is an implementation
|
|
|
|
detail and shouldn't be called by external code. Use Variable.resolve()
|
|
|
|
instead.
|
|
|
|
"""
|
|
|
|
current = context
|
2011-09-27 20:15:15 +08:00
|
|
|
try: # catch-all for silent variable failures
|
2010-12-20 07:47:24 +08:00
|
|
|
for bit in self.lookups:
|
2011-09-27 20:15:15 +08:00
|
|
|
try: # dictionary lookup
|
2010-12-20 07:47:24 +08:00
|
|
|
current = current[bit]
|
2014-09-30 07:51:33 +08:00
|
|
|
# ValueError/IndexError are for numpy.array lookup on
|
|
|
|
# numpy < 1.9 and 1.9+ respectively
|
|
|
|
except (TypeError, AttributeError, KeyError, ValueError, IndexError):
|
2011-09-27 20:15:15 +08:00
|
|
|
try: # attribute lookup
|
2013-08-13 00:41:39 +08:00
|
|
|
# Don't return class attributes if the class is the context:
|
|
|
|
if isinstance(current, BaseContext) and getattr(type(current), bit):
|
|
|
|
raise AttributeError
|
2010-12-20 07:47:24 +08:00
|
|
|
current = getattr(current, bit)
|
2014-07-26 19:00:54 +08:00
|
|
|
except (TypeError, AttributeError) as e:
|
|
|
|
# Reraise an AttributeError raised by a @property
|
2014-09-04 20:15:09 +08:00
|
|
|
if (isinstance(e, AttributeError) and
|
|
|
|
not isinstance(current, BaseContext) and bit in dir(current)):
|
2014-07-26 19:00:54 +08:00
|
|
|
raise
|
2011-09-27 20:15:15 +08:00
|
|
|
try: # list-index lookup
|
2010-12-20 07:47:24 +08:00
|
|
|
current = current[int(bit)]
|
2011-09-27 20:15:15 +08:00
|
|
|
except (IndexError, # list index out of range
|
|
|
|
ValueError, # invalid literal for int()
|
|
|
|
KeyError, # current is a dict without `int(bit)` key
|
|
|
|
TypeError): # unsubscriptable object
|
|
|
|
raise VariableDoesNotExist("Failed lookup for key "
|
|
|
|
"[%s] in %r",
|
|
|
|
(bit, current)) # missing attribute
|
2010-12-20 07:47:24 +08:00
|
|
|
if callable(current):
|
2011-04-20 06:06:19 +08:00
|
|
|
if getattr(current, 'do_not_call_in_templates', False):
|
|
|
|
pass
|
|
|
|
elif getattr(current, 'alters_data', False):
|
2014-11-23 01:52:55 +08:00
|
|
|
current = context.engine.string_if_invalid
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
2013-11-03 03:27:47 +08:00
|
|
|
try: # method call (assuming no args required)
|
2010-12-20 07:47:24 +08:00
|
|
|
current = current()
|
2013-09-08 21:07:12 +08:00
|
|
|
except TypeError:
|
|
|
|
try:
|
|
|
|
getcallargs(current)
|
2013-11-03 03:27:47 +08:00
|
|
|
except TypeError: # arguments *were* required
|
2014-11-23 01:52:55 +08:00
|
|
|
current = context.engine.string_if_invalid # invalid method call
|
2013-09-08 21:07:12 +08:00
|
|
|
else:
|
|
|
|
raise
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2010-12-20 07:47:24 +08:00
|
|
|
if getattr(e, 'silent_variable_failure', False):
|
2014-11-23 01:52:55 +08:00
|
|
|
current = context.engine.string_if_invalid
|
2010-12-20 07:47:24 +08:00
|
|
|
else:
|
|
|
|
raise
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
return current
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Node(object):
|
|
|
|
# Set this to True for nodes that must be first in the template (although
|
|
|
|
# they can be preceded by text nodes.
|
|
|
|
must_be_first = False
|
|
|
|
child_nodelists = ('nodelist',)
|
|
|
|
|
|
|
|
def render(self, context):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Return the node rendered as a string.
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
yield self
|
|
|
|
|
|
|
|
def get_nodes_by_type(self, nodetype):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Return a list of all nodes (within this node and its nodelist)
|
|
|
|
of the given type
|
|
|
|
"""
|
2010-11-27 13:47:30 +08:00
|
|
|
nodes = []
|
|
|
|
if isinstance(self, nodetype):
|
|
|
|
nodes.append(self)
|
|
|
|
for attr in self.child_nodelists:
|
|
|
|
nodelist = getattr(self, attr, None)
|
|
|
|
if nodelist:
|
|
|
|
nodes.extend(nodelist.get_nodes_by_type(nodetype))
|
|
|
|
return nodes
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class NodeList(list):
|
|
|
|
# Set to True the first time a non-TextNode is inserted by
|
|
|
|
# extend_nodelist().
|
|
|
|
contains_nontext = False
|
|
|
|
|
|
|
|
def render(self, context):
|
|
|
|
bits = []
|
|
|
|
for node in self:
|
|
|
|
if isinstance(node, Node):
|
2011-10-12 04:43:11 +08:00
|
|
|
bit = self.render_node(node, context)
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
2011-10-12 04:43:11 +08:00
|
|
|
bit = node
|
2012-07-21 16:00:10 +08:00
|
|
|
bits.append(force_text(bit))
|
2012-06-08 00:08:47 +08:00
|
|
|
return mark_safe(''.join(bits))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def get_nodes_by_type(self, nodetype):
|
|
|
|
"Return a list of all nodes of the given type"
|
|
|
|
nodes = []
|
|
|
|
for node in self:
|
|
|
|
nodes.extend(node.get_nodes_by_type(nodetype))
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
def render_node(self, node, context):
|
|
|
|
return node.render(context)
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class TextNode(Node):
|
|
|
|
def __init__(self, s):
|
|
|
|
self.s = s
|
|
|
|
|
|
|
|
def __repr__(self):
|
2012-08-30 04:40:51 +08:00
|
|
|
return force_str("<Text Node: '%s'>" % self.s[:25], 'ascii',
|
2010-11-27 13:47:30 +08:00
|
|
|
errors='replace')
|
|
|
|
|
|
|
|
def render(self, context):
|
|
|
|
return self.s
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2013-02-25 15:33:29 +08:00
|
|
|
def render_value_in_context(value, context):
|
2010-11-27 13:47:30 +08:00
|
|
|
"""
|
|
|
|
Converts any value to a string to become part of a rendered template. This
|
|
|
|
means escaping, if required, and conversion to a unicode object. If value
|
|
|
|
is a string, it is expected to have already been translated.
|
|
|
|
"""
|
2012-04-29 21:37:23 +08:00
|
|
|
value = template_localtime(value, use_tz=context.use_tz)
|
2010-11-27 13:47:30 +08:00
|
|
|
value = localize(value, use_l10n=context.use_l10n)
|
2012-07-21 16:00:10 +08:00
|
|
|
value = force_text(value)
|
2011-09-27 20:15:15 +08:00
|
|
|
if ((context.autoescape and not isinstance(value, SafeData)) or
|
|
|
|
isinstance(value, EscapeData)):
|
2014-12-24 05:29:01 +08:00
|
|
|
return conditional_escape(value)
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class VariableNode(Node):
|
|
|
|
def __init__(self, filter_expression):
|
|
|
|
self.filter_expression = filter_expression
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Variable Node: %s>" % self.filter_expression
|
|
|
|
|
|
|
|
def render(self, context):
|
|
|
|
try:
|
|
|
|
output = self.filter_expression.resolve(context)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
# Unicode conversion can fail sometimes for reasons out of our
|
2011-09-27 20:15:15 +08:00
|
|
|
# control (e.g. exception rendering). In that case, we fail
|
|
|
|
# quietly.
|
2010-11-27 13:47:30 +08:00
|
|
|
return ''
|
2013-02-25 15:33:29 +08:00
|
|
|
return render_value_in_context(output, context)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
# Regex for token keyword arguments
|
|
|
|
kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
def token_kwargs(bits, parser, support_legacy=False):
|
|
|
|
"""
|
|
|
|
A utility method for parsing token keyword arguments.
|
|
|
|
|
|
|
|
:param bits: A list containing remainder of the token (split by spaces)
|
|
|
|
that is to be checked for arguments. Valid arguments will be removed
|
|
|
|
from this list.
|
|
|
|
|
|
|
|
:param support_legacy: If set to true ``True``, the legacy format
|
|
|
|
``1 as foo`` will be accepted. Otherwise, only the standard ``foo=1``
|
|
|
|
format is allowed.
|
|
|
|
|
|
|
|
:returns: A dictionary of the arguments retrieved from the ``bits`` token
|
|
|
|
list.
|
|
|
|
|
|
|
|
There is no requirement for all remaining token ``bits`` to be keyword
|
|
|
|
arguments, so the dictionary will be returned as soon as an invalid
|
|
|
|
argument format is reached.
|
|
|
|
"""
|
|
|
|
if not bits:
|
|
|
|
return {}
|
|
|
|
match = kwarg_re.match(bits[0])
|
|
|
|
kwarg_format = match and match.group(1)
|
|
|
|
if not kwarg_format:
|
|
|
|
if not support_legacy:
|
|
|
|
return {}
|
|
|
|
if len(bits) < 3 or bits[1] != 'as':
|
|
|
|
return {}
|
|
|
|
|
|
|
|
kwargs = {}
|
|
|
|
while bits:
|
|
|
|
if kwarg_format:
|
|
|
|
match = kwarg_re.match(bits[0])
|
|
|
|
if not match or not match.group(1):
|
|
|
|
return kwargs
|
|
|
|
key, value = match.groups()
|
|
|
|
del bits[:1]
|
|
|
|
else:
|
|
|
|
if len(bits) < 3 or bits[1] != 'as':
|
|
|
|
return kwargs
|
|
|
|
key, value = bits[2], bits[0]
|
|
|
|
del bits[:3]
|
|
|
|
kwargs[key] = parser.compile_filter(value)
|
|
|
|
if bits and not kwarg_format:
|
|
|
|
if bits[0] != 'and':
|
|
|
|
return kwargs
|
|
|
|
del bits[:1]
|
|
|
|
return kwargs
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
def parse_bits(parser, bits, params, varargs, varkw, defaults,
|
|
|
|
takes_context, name):
|
|
|
|
"""
|
|
|
|
Parses bits for template tag helpers (simple_tag, include_tag and
|
|
|
|
assignment_tag), in particular by detecting syntax errors and by
|
|
|
|
extracting positional and keyword arguments.
|
|
|
|
"""
|
|
|
|
if takes_context:
|
|
|
|
if params[0] == 'context':
|
|
|
|
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)
|
|
|
|
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
|
2012-07-21 03:14:27 +08:00
|
|
|
param, value = list(six.iteritems(kwarg))[0]
|
2011-09-27 20:15:15 +08:00
|
|
|
if param not in params and varkw is None:
|
|
|
|
# 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)
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
2011-09-27 20:15:15 +08:00
|
|
|
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)]
|
|
|
|
if unhandled_params:
|
|
|
|
# Some positional arguments were not supplied
|
|
|
|
raise TemplateSyntaxError(
|
2012-06-08 00:08:47 +08:00
|
|
|
"'%s' did not receive value(s) for the argument(s): %s" %
|
2013-08-30 07:20:00 +08:00
|
|
|
(name, ", ".join("'%s'" % p for p in unhandled_params)))
|
2011-09-27 20:15:15 +08:00
|
|
|
return args, kwargs
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
def generic_tag_compiler(parser, token, params, varargs, varkw, defaults,
|
|
|
|
name, takes_context, node_class):
|
|
|
|
"""
|
|
|
|
Returns a template.Node subclass.
|
|
|
|
"""
|
|
|
|
bits = token.split_contents()[1:]
|
|
|
|
args, kwargs = parse_bits(parser, bits, params, varargs, varkw,
|
|
|
|
defaults, takes_context, name)
|
|
|
|
return node_class(takes_context, args, kwargs)
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
class TagHelperNode(Node):
|
|
|
|
"""
|
|
|
|
Base class for tag helper nodes such as SimpleNode, InclusionNode and
|
|
|
|
AssignmentNode. Manages the positional and keyword arguments to be passed
|
|
|
|
to the decorated function.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, takes_context, args, kwargs):
|
|
|
|
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
|
2014-12-07 05:00:09 +08:00
|
|
|
resolved_kwargs = {k: v.resolve(context) for k, v in self.kwargs.items()}
|
2011-09-27 20:15:15 +08:00
|
|
|
return resolved_args, resolved_kwargs
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
class Library(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.filters = {}
|
|
|
|
self.tags = {}
|
|
|
|
|
|
|
|
def tag(self, name=None, compile_function=None):
|
2011-09-27 20:15:15 +08:00
|
|
|
if name is None and compile_function is None:
|
2010-11-27 13:47:30 +08:00
|
|
|
# @register.tag()
|
|
|
|
return self.tag_function
|
2011-09-27 20:15:15 +08:00
|
|
|
elif name is not None and compile_function is None:
|
2011-05-02 00:46:02 +08:00
|
|
|
if callable(name):
|
2010-11-27 13:47:30 +08:00
|
|
|
# @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
|
2011-09-27 20:15:15 +08:00
|
|
|
elif name is not None and compile_function is not None:
|
2010-11-27 13:47:30 +08:00
|
|
|
# register.tag('somename', somefunc)
|
|
|
|
self.tags[name] = compile_function
|
|
|
|
return compile_function
|
|
|
|
else:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise InvalidTemplateLibrary("Unsupported arguments to "
|
|
|
|
"Library.tag: (%r, %r)", (name, compile_function))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
def tag_function(self, func):
|
2010-11-27 13:47:30 +08:00
|
|
|
self.tags[getattr(func, "_decorated_function", func).__name__] = func
|
|
|
|
return func
|
|
|
|
|
2011-10-30 15:32:21 +08:00
|
|
|
def filter(self, name=None, filter_func=None, **flags):
|
2011-09-27 20:15:15 +08:00
|
|
|
if name is None and filter_func is None:
|
2010-11-27 13:47:30 +08:00
|
|
|
# @register.filter()
|
2011-10-30 15:32:21 +08:00
|
|
|
def dec(func):
|
|
|
|
return self.filter_function(func, **flags)
|
|
|
|
return dec
|
|
|
|
|
|
|
|
elif name is not None and filter_func is None:
|
2011-05-02 00:46:02 +08:00
|
|
|
if callable(name):
|
2010-11-27 13:47:30 +08:00
|
|
|
# @register.filter
|
2011-10-30 15:32:21 +08:00
|
|
|
return self.filter_function(name, **flags)
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
|
|
|
# @register.filter('somename') or @register.filter(name='somename')
|
|
|
|
def dec(func):
|
2011-10-30 15:32:21 +08:00
|
|
|
return self.filter(name, func, **flags)
|
2010-11-27 13:47:30 +08:00
|
|
|
return dec
|
2011-10-30 15:32:21 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
elif name is not None and filter_func is not None:
|
2010-11-27 13:47:30 +08:00
|
|
|
# register.filter('somename', somefunc)
|
|
|
|
self.filters[name] = filter_func
|
2011-11-18 21:01:06 +08:00
|
|
|
for attr in ('expects_localtime', 'is_safe', 'needs_autoescape'):
|
2011-10-30 15:32:21 +08:00
|
|
|
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
|
|
|
|
if hasattr(filter_func, "_decorated_function"):
|
|
|
|
setattr(filter_func._decorated_function, attr, value)
|
2013-06-26 02:28:35 +08:00
|
|
|
filter_func._filter_name = name
|
2010-11-27 13:47:30 +08:00
|
|
|
return filter_func
|
|
|
|
else:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise InvalidTemplateLibrary("Unsupported arguments to "
|
|
|
|
"Library.filter: (%r, %r)", (name, filter_func))
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-10-30 15:32:21 +08:00
|
|
|
def filter_function(self, func, **flags):
|
|
|
|
name = getattr(func, "_decorated_function", func).__name__
|
|
|
|
return self.filter(name, func, **flags)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-06-12 00:05:28 +08:00
|
|
|
def simple_tag(self, func=None, takes_context=None, name=None):
|
2010-12-19 23:00:50 +08:00
|
|
|
def dec(func):
|
2011-09-27 20:15:15 +08:00
|
|
|
params, varargs, varkw, defaults = getargspec(func)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
class SimpleNode(TagHelperNode):
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2010-12-19 23:00:50 +08:00
|
|
|
def render(self, context):
|
2011-09-27 20:15:15 +08:00
|
|
|
resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
|
|
|
|
return func(*resolved_args, **resolved_kwargs)
|
|
|
|
|
|
|
|
function_name = (name or
|
|
|
|
getattr(func, '_decorated_function', func).__name__)
|
|
|
|
compile_func = partial(generic_tag_compiler,
|
|
|
|
params=params, varargs=varargs, varkw=varkw,
|
|
|
|
defaults=defaults, name=function_name,
|
|
|
|
takes_context=takes_context, node_class=SimpleNode)
|
2010-12-19 23:00:50 +08:00
|
|
|
compile_func.__doc__ = func.__doc__
|
2011-06-12 00:05:28 +08:00
|
|
|
self.tag(function_name, compile_func)
|
2010-12-19 23:00:50 +08:00
|
|
|
return func
|
|
|
|
|
|
|
|
if func is None:
|
|
|
|
# @register.simple_tag(...)
|
|
|
|
return dec
|
|
|
|
elif callable(func):
|
|
|
|
# @register.simple_tag
|
|
|
|
return dec(func)
|
|
|
|
else:
|
|
|
|
raise TemplateSyntaxError("Invalid arguments provided to simple_tag")
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-06-12 00:05:28 +08:00
|
|
|
def assignment_tag(self, func=None, takes_context=None, name=None):
|
2011-05-03 19:52:42 +08:00
|
|
|
def dec(func):
|
2011-09-27 20:15:15 +08:00
|
|
|
params, varargs, varkw, defaults = getargspec(func)
|
2011-05-03 19:52:42 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
class AssignmentNode(TagHelperNode):
|
|
|
|
def __init__(self, takes_context, args, kwargs, target_var):
|
|
|
|
super(AssignmentNode, self).__init__(takes_context, args, kwargs)
|
2011-05-03 19:52:42 +08:00
|
|
|
self.target_var = target_var
|
|
|
|
|
|
|
|
def render(self, context):
|
2011-09-27 20:15:15 +08:00
|
|
|
resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
|
|
|
|
context[self.target_var] = func(*resolved_args, **resolved_kwargs)
|
2011-05-03 19:52:42 +08:00
|
|
|
return ''
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
function_name = (name or
|
|
|
|
getattr(func, '_decorated_function', func).__name__)
|
|
|
|
|
2011-05-03 19:52:42 +08:00
|
|
|
def compile_func(parser, token):
|
2011-09-27 20:15:15 +08:00
|
|
|
bits = token.split_contents()[1:]
|
|
|
|
if len(bits) < 2 or bits[-2] != 'as':
|
2011-05-03 19:52:42 +08:00
|
|
|
raise TemplateSyntaxError(
|
|
|
|
"'%s' tag takes at least 2 arguments and the "
|
2011-09-27 20:15:15 +08:00
|
|
|
"second last argument must be 'as'" % function_name)
|
2011-05-03 19:52:42 +08:00
|
|
|
target_var = bits[-1]
|
2011-09-27 20:15:15 +08:00
|
|
|
bits = bits[:-2]
|
|
|
|
args, kwargs = parse_bits(parser, bits, params,
|
|
|
|
varargs, varkw, defaults, takes_context, function_name)
|
|
|
|
return AssignmentNode(takes_context, args, kwargs, target_var)
|
2011-05-03 19:52:42 +08:00
|
|
|
|
|
|
|
compile_func.__doc__ = func.__doc__
|
2011-06-12 00:05:28 +08:00
|
|
|
self.tag(function_name, compile_func)
|
2011-05-03 19:52:42 +08:00
|
|
|
return func
|
|
|
|
|
|
|
|
if func is None:
|
|
|
|
# @register.assignment_tag(...)
|
|
|
|
return dec
|
|
|
|
elif callable(func):
|
|
|
|
# @register.assignment_tag
|
|
|
|
return dec(func)
|
|
|
|
else:
|
|
|
|
raise TemplateSyntaxError("Invalid arguments provided to assignment_tag")
|
|
|
|
|
2014-12-15 01:29:15 +08:00
|
|
|
def inclusion_tag(self, file_name, takes_context=False, name=None):
|
2010-11-27 13:47:30 +08:00
|
|
|
def dec(func):
|
2011-09-27 20:15:15 +08:00
|
|
|
params, varargs, varkw, defaults = getargspec(func)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
class InclusionNode(TagHelperNode):
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
def render(self, context):
|
2011-09-27 20:15:15 +08:00
|
|
|
resolved_args, resolved_kwargs = self.get_resolved_arguments(context)
|
|
|
|
_dict = func(*resolved_args, **resolved_kwargs)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
if not getattr(self, 'nodelist', False):
|
2011-06-12 00:22:45 +08:00
|
|
|
if isinstance(file_name, Template):
|
|
|
|
t = file_name
|
2014-11-29 05:13:11 +08:00
|
|
|
elif isinstance(getattr(file_name, 'template', None), Template):
|
|
|
|
t = file_name.template
|
2012-07-20 20:22:00 +08:00
|
|
|
elif not isinstance(file_name, six.string_types) and is_iterable(file_name):
|
2014-11-28 06:02:46 +08:00
|
|
|
t = context.engine.select_template(file_name)
|
2010-11-27 13:47:30 +08:00
|
|
|
else:
|
2014-11-28 06:02:46 +08:00
|
|
|
t = context.engine.get_template(file_name)
|
2010-11-27 13:47:30 +08:00
|
|
|
self.nodelist = t.nodelist
|
2014-12-15 01:29:15 +08:00
|
|
|
new_context = context.new(_dict)
|
2011-09-27 20:15:15 +08:00
|
|
|
# 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.
|
2010-11-27 13:47:30 +08:00
|
|
|
csrf_token = context.get('csrf_token', None)
|
|
|
|
if csrf_token is not None:
|
|
|
|
new_context['csrf_token'] = csrf_token
|
|
|
|
return self.nodelist.render(new_context)
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
function_name = (name or
|
|
|
|
getattr(func, '_decorated_function', func).__name__)
|
|
|
|
compile_func = partial(generic_tag_compiler,
|
|
|
|
params=params, varargs=varargs, varkw=varkw,
|
|
|
|
defaults=defaults, name=function_name,
|
|
|
|
takes_context=takes_context, node_class=InclusionNode)
|
2010-11-27 13:47:30 +08:00
|
|
|
compile_func.__doc__ = func.__doc__
|
2011-06-12 00:05:28 +08:00
|
|
|
self.tag(function_name, compile_func)
|
2010-11-27 13:47:30 +08:00
|
|
|
return func
|
|
|
|
return dec
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2011-11-21 18:28:12 +08:00
|
|
|
def is_library_missing(name):
|
|
|
|
"""Check if library that failed to load cannot be found under any
|
|
|
|
templatetags directory or does exist but fails to import.
|
|
|
|
|
|
|
|
Non-existing condition is checked recursively for each subpackage in cases
|
|
|
|
like <appdir>/templatetags/subpackage/package/module.py.
|
|
|
|
"""
|
|
|
|
# Don't bother to check if '.' is in name since any name will be prefixed
|
|
|
|
# with some template root.
|
|
|
|
path, module = name.rsplit('.', 1)
|
|
|
|
try:
|
|
|
|
package = import_module(path)
|
|
|
|
return not module_has_submodule(package, module)
|
|
|
|
except ImportError:
|
|
|
|
return is_library_missing(path)
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
def import_library(taglib_module):
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Load a template tag library module.
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
Verifies that the library contains a 'register' attribute, and
|
|
|
|
returns that attribute as the representation of the library
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
mod = import_module(taglib_module)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2011-09-27 20:15:15 +08:00
|
|
|
# If the ImportError is because the taglib submodule does not exist,
|
|
|
|
# that's not an error that should be raised. If the submodule exists
|
|
|
|
# and raised an ImportError on the attempt to load it, that we want
|
|
|
|
# to raise.
|
2011-11-21 18:28:12 +08:00
|
|
|
if is_library_missing(taglib_module):
|
2010-11-27 13:47:30 +08:00
|
|
|
return None
|
|
|
|
else:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise InvalidTemplateLibrary("ImportError raised loading %s: %s" %
|
|
|
|
(taglib_module, e))
|
2010-11-27 13:47:30 +08:00
|
|
|
try:
|
|
|
|
return mod.register
|
|
|
|
except AttributeError:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise InvalidTemplateLibrary("Template library %s does not have "
|
|
|
|
"a variable named 'register'" %
|
|
|
|
taglib_module)
|
2010-11-27 13:47:30 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2014-11-19 04:50:52 +08:00
|
|
|
@lru_cache.lru_cache()
|
2010-11-27 13:47:30 +08:00
|
|
|
def get_templatetags_modules():
|
2011-09-27 20:15:15 +08:00
|
|
|
"""
|
|
|
|
Return the list of all available template tag modules.
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
Caches the result for faster access.
|
|
|
|
"""
|
2014-11-19 04:50:52 +08:00
|
|
|
templatetags_modules_candidates = ['django.templatetags']
|
2014-12-07 05:00:09 +08:00
|
|
|
templatetags_modules_candidates.extend(
|
2014-11-19 04:50:52 +08:00
|
|
|
'%s.templatetags' % app_config.name
|
2014-12-07 05:00:09 +08:00
|
|
|
for app_config in apps.get_app_configs())
|
2014-11-19 04:50:52 +08:00
|
|
|
|
|
|
|
templatetags_modules = []
|
|
|
|
for templatetag_module in templatetags_modules_candidates:
|
|
|
|
try:
|
|
|
|
import_module(templatetag_module)
|
|
|
|
except ImportError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
templatetags_modules.append(templatetag_module)
|
2010-11-27 13:47:30 +08:00
|
|
|
return templatetags_modules
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
def get_library(library_name):
|
|
|
|
"""
|
|
|
|
Load the template library module with the given name.
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
If library is not already loaded loop over all templatetags modules
|
|
|
|
to locate it.
|
2010-11-27 13:47:30 +08:00
|
|
|
|
|
|
|
{% load somelib %} and {% load someotherlib %} loops twice.
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
Subsequent loads eg. {% load somelib %} in the same process will grab
|
|
|
|
the cached module from libraries.
|
2010-11-27 13:47:30 +08:00
|
|
|
"""
|
|
|
|
lib = libraries.get(library_name, None)
|
|
|
|
if not lib:
|
|
|
|
templatetags_modules = get_templatetags_modules()
|
|
|
|
tried_modules = []
|
|
|
|
for module in templatetags_modules:
|
|
|
|
taglib_module = '%s.%s' % (module, library_name)
|
|
|
|
tried_modules.append(taglib_module)
|
|
|
|
lib = import_library(taglib_module)
|
|
|
|
if lib:
|
|
|
|
libraries[library_name] = lib
|
|
|
|
break
|
|
|
|
if not lib:
|
2011-09-27 20:15:15 +08:00
|
|
|
raise InvalidTemplateLibrary("Template library %s not found, "
|
|
|
|
"tried %s" %
|
|
|
|
(library_name,
|
|
|
|
','.join(tried_modules)))
|
2010-11-27 13:47:30 +08:00
|
|
|
return lib
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
def add_to_builtins(module):
|
|
|
|
builtins.append(import_library(module))
|
|
|
|
|
2011-09-27 20:15:15 +08:00
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
add_to_builtins('django.template.defaulttags')
|
|
|
|
add_to_builtins('django.template.defaultfilters')
|
2014-01-20 03:11:32 +08:00
|
|
|
add_to_builtins('django.template.loader_tags')
|