2 The node template tags are automatically included as builtins if :mod:`philo` is an installed app.
6 from django import template
7 from django.conf import settings
8 from django.contrib.sites.models import Site
9 from django.core.urlresolvers import reverse, NoReverseMatch
10 from django.template.defaulttags import kwarg_re
11 from django.utils.encoding import smart_str
13 from philo.exceptions import ViewCanNotProvideSubpath
16 register = template.Library()
19 class NodeURLNode(template.Node):
20 def __init__(self, node, as_var, with_obj=None, view_name=None, args=None, kwargs=None):
22 self.view_name = view_name
24 # Because the following variables have already been compiled as filters if they exist, they don't need to be re-scanned as template variables.
26 self.with_obj = with_obj
30 def render(self, context):
32 node = self.node.resolve(context)
34 node = context.get('node', None)
37 return settings.TEMPLATE_STRING_IF_INVALID
39 if self.with_obj is None and self.view_name is None:
40 url = node.get_absolute_url()
42 if not node.view.accepts_subpath:
43 return settings.TEMPLATE_STRING_IF_INVALID
45 if self.with_obj is not None:
47 view_name, args, kwargs = node.view.get_reverse_params(self.with_obj.resolve(context))
48 except ViewCanNotProvideSubpath:
49 return settings.TEMPLATE_STRING_IF_INVALID
50 else: # self.view_name is not None
51 view_name = self.view_name
52 args = [arg.resolve(context) for arg in self.args]
53 kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context)) for k, v in self.kwargs.items()])
57 subpath = reverse(view_name, urlconf=node.view, args=args, kwargs=kwargs)
58 except NoReverseMatch:
59 if self.as_var is None:
60 if settings.TEMPLATE_DEBUG:
62 return settings.TEMPLATE_STRING_IF_INVALID
64 url = node.construct_url(subpath)
67 context[self.as_var] = url
74 def node_url(parser, token):
76 The :ttag:`node_url` tag allows access to :meth:`.View.reverse` from a template for a :class:`.Node`. By default, the :class:`.Node` that is used for the call is pulled from the context variable ``node``; however, this can be overridden with the ``[for <node>]`` option.
80 {% node_url [for <node>] [as <var>] %}
81 {% node_url with <obj> [for <node>] [as <var>] %}
82 {% node_url <view_name> [<arg1> [<arg2> ...] ] [for <node>] [as <var>] %}
83 {% node_url <view_name> [<key1>=<value1> [<key2>=<value2> ...] ] [for <node>] [as <var>] %}
86 params = token.split_contents()
93 if len(params) >= 2 and params[-2] == 'as':
97 if len(params) >= 2 and params[-2] == 'for':
98 node = parser.compile_filter(params[-1])
101 if len(params) >= 2 and params[-2] == 'with':
102 with_obj = parser.compile_filter(params[-1])
105 if with_obj is not None:
107 raise template.TemplateSyntaxError('`%s` template tag accepts no arguments or keyword arguments if with <obj> is specified.' % tag)
108 return NodeURLNode(with_obj=with_obj, node=node, as_var=as_var)
113 view_name = params.pop(0)
115 match = kwarg_re.match(param)
117 raise TemplateSyntaxError("Malformed arguments to `%s` tag" % tag)
118 name, value = match.groups()
120 kwargs[name] = parser.compile_filter(value)
122 args.append(parser.compile_filter(value))
123 return NodeURLNode(view_name=view_name, args=args, kwargs=kwargs, node=node, as_var=as_var)
125 return NodeURLNode(node=node, as_var=as_var)