def ifusergroup(parser, token):
""" Check to see if the currently logged in user belongs to one or more groups
Requires the Django authentication contrib app and middleware.
Usage: {% ifusergroup Admins %} ... {% endifusergroup %}, or
{% ifusergroup Admins Clients Programmers Managers %} ... {% else %} ... {% endifusergroup %}
"""
try:
tokens = token.split_contents()
groups = []
groups += tokens[1:]
except ValueError:
raise template.TemplateSyntaxError("Tag 'ifusergroup' requires at least 1 argument.")
nodelist_true = parser.parse(('else', 'endifusergroup'))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse(('endifusergroup',))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return GroupCheckNode(groups, nodelist_true, nodelist_false)
python类NodeList()的实例源码
def ifappexists(parser, token):
""" Conditional Django template tag to check if one or more apps exist.
Usage: {% ifappexists tag %} ... {% endifappexists %}, or
{% ifappexists tag inventory %} ... {% else %} ... {% endifappexists %}
"""
try:
tokens = token.split_contents()
apps = []
apps += tokens[1:]
except ValueError:
raise template.TemplateSyntaxError("Tag 'ifappexists' requires at least 1 argument.")
nodelist_true = parser.parse(('else', 'endifappexists'))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse(('endifappexists',))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return AppCheckNode(apps, nodelist_true, nodelist_false)
def feature(parser, token):
bits = token.split_contents()
if len(bits) < 2:
raise template.TemplateSyntaxError("%r tag requires an argument" % token.contents.split()[0])
name = bits[1]
params = bits[2:]
nodelist_true = parser.parse(('else', 'endfeature'))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse(('endfeature',))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return FeatureNode(nodelist_true, nodelist_false, name, params)
def ifopenid(parser, token):
nodelist_true = parser.parse(("else", "endifopenid"))
token = parser.next_token()
if token.contents == "else":
nodelist_false = parser.parse(("endifopenid",))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return IfOpenidNode(nodelist_true, nodelist_false)
def ifopenid(parser, token):
nodelist_true = parser.parse(("else", "endifopenid"))
token = parser.next_token()
if token.contents == "else":
nodelist_false = parser.parse(("endifopenid",))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return IfOpenidNode(nodelist_true, nodelist_false)
def ifsetting(parser, token):
bits = token.split_contents()
nodelist_true = parser.parse(("else", "endifsetting"))
token = parser.next_token()
if token.contents == "else":
nodelist_false = parser.parse(("endifsetting",))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return IfSettingNode(nodelist_true, nodelist_false, bits[1])
def if_can_do_article(parser, token):
args = token.contents.split()
title = args[1]
perm = args[2] if len(args)>2 else 'can_view_article'
lang = args[3] if len(args)>3 else None
nodelist_true = parser.parse(('else', 'endif'))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse(('endif',))
parser.delete_first_token()
else:
nodelist_false = template.NodeList()
return IfCanDoArticle(title, perm, lang, nodelist_true, nodelist_false)
def _scan_placeholders(nodelist, current_block=None, ignore_blocks=None):
from cms.templatetags.cms_tags import Placeholder
placeholders = []
if ignore_blocks is None:
# List of BlockNode instances to ignore.
# This is important to avoid processing overriden block nodes.
ignore_blocks = []
for node in nodelist:
# check if this is a placeholder first
if isinstance(node, Placeholder):
placeholders.append(node.get_name())
elif isinstance(node, IncludeNode):
# if there's an error in the to-be-included template, node.template becomes None
if node.template:
# Check if it quacks like a template object, if not
# presume is a template path and get the object out of it
if not callable(getattr(node.template, 'render', None)):
# If it's a variable there is no way to expand it at this stage so we
# need to skip it
if isinstance(node.template.var, Variable):
continue
else:
template = get_template(node.template.var)
else:
template = node.template
placeholders += _scan_placeholders(_get_nodelist(template), current_block)
# handle {% extends ... %} tags
elif isinstance(node, ExtendsNode):
placeholders += _extend_nodelist(node)
# in block nodes we have to scan for super blocks
elif isinstance(node, VariableNode) and current_block:
if node.filter_expression.token == 'block.super':
if not hasattr(current_block.super, 'nodelist'):
raise TemplateSyntaxError("Cannot render block.super for blocks without a parent.")
placeholders += _scan_placeholders(_get_nodelist(current_block.super), current_block.super)
# ignore nested blocks which are already handled
elif isinstance(node, BlockNode) and node.name in ignore_blocks:
continue
# if the node has the newly introduced 'child_nodelists' attribute, scan
# those attributes for nodelists and recurse them
elif hasattr(node, 'child_nodelists'):
for nodelist_name in node.child_nodelists:
if hasattr(node, nodelist_name):
subnodelist = getattr(node, nodelist_name)
if isinstance(subnodelist, NodeList):
if isinstance(node, BlockNode):
current_block = node
placeholders += _scan_placeholders(subnodelist, current_block, ignore_blocks)
# else just scan the node for nodelist instance attributes
else:
for attr in dir(node):
obj = getattr(node, attr)
if isinstance(obj, NodeList):
if isinstance(node, BlockNode):
current_block = node
placeholders += _scan_placeholders(obj, current_block, ignore_blocks)
return placeholders