def value_from_settings(parser, token):
bits = token.split_contents()
if len(bits) < 2:
raise TemplateSyntaxError("'%s' takes at least one " \
"argument (settings constant to retrieve)" % bits[0])
settingsvar = bits[1]
settingsvar = settingsvar[1:-1] if settingsvar[0] == '"' else settingsvar
asvar = None
bits = bits[2:]
if len(bits) >= 2 and bits[-2] == 'as':
asvar = bits[-1]
bits = bits[:-2]
if len(bits):
raise TemplateSyntaxError("'value_from_settings' didn't recognise " \
"the arguments '%s'" % ", ".join(bits))
if settingsvar not in settings.TEMPLATE_ALLOWABLE_SETTINGS_VALUES:
raise TemplateSyntaxError("The settings Variable %s is not allowed to be acessed" % settingsvar)
return ValueFromSettings(settingsvar, asvar)
python类Variable()的实例源码
def handle_var(value, context):
"""
Handle template tag variable
"""
# Resolve FilterExpression and Variable immediately
if isinstance(value, FilterExpression) or isinstance(value, Variable):
return value.resolve(context)
# Return quoted strings unquoted
# http://djangosnippets.org/snippets/886
stringval = QUOTED_STRING.search(value)
if stringval:
return stringval.group('noquotes')
# Resolve variable or return string value
try:
return Variable(value).resolve(context)
except VariableDoesNotExist:
return value
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def __init__(self, number, key, var_name):
# Retrieve the page number.
self.page_number_variable = None
if number is None:
self.page_number = 1
elif number.isdigit():
self.page_number = int(number)
else:
self.page_number_variable = template.Variable(number)
# Get the queystring key.
self.querystring_key_variable = None
if key is None:
self.querystring_key = settings.PAGE_LABEL
elif key[0] in ('"', "'") and key[-1] == key[0]:
self.querystring_key = key[1:-1]
else:
self.querystring_key_variable = template.Variable(key)
# Get the template variable name.
self.var_name = var_name
def style_attribute(attribute_name, package):
mappings = {
'title': style_title,
'repo_description': style_repo_description,
'commits_over_52': style_commits,
}
as_var = template.Variable('package.' + attribute_name)
try:
value = as_var.resolve({'package': package})
except template.VariableDoesNotExist:
value = ''
if attribute_name in list(mappings.keys()):
return mappings[attribute_name](value)
return style_default(value)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
raise template.TemplateSyntaxError('%r tag must have the form field name as the first value, followed by optional key="value" attributes.' % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x+1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
raise template.TemplateSyntaxError('%r tag must have the form field name as the first value, followed by optional key="value" attributes.' % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x+1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def handle_var(value, context):
"""
Handle template tag variable
"""
# Resolve FilterExpression and Variable immediately
if isinstance(value, FilterExpression) or isinstance(value, Variable):
return value.resolve(context)
# Return quoted strings unquoted
# http://djangosnippets.org/snippets/886
stringval = QUOTED_STRING.search(value)
if stringval:
return stringval.group('noquotes')
# Resolve variable or return string value
try:
return Variable(value).resolve(context)
except VariableDoesNotExist:
return value
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)