Minor correction to shipherd recursenavigation docstring.
[philo.git] / philo / templatetags / nodes.py
1 from django import template
2 from django.conf import settings
3 from django.contrib.sites.models import Site
4 from django.core.urlresolvers import reverse, NoReverseMatch
5 from django.template.defaulttags import kwarg_re
6 from django.utils.encoding import smart_str
7 from philo.exceptions import ViewCanNotProvideSubpath
8
9
10 register = template.Library()
11
12
13 class NodeURLNode(template.Node):
14         def __init__(self, node, as_var, with_obj=None, view_name=None, args=None, kwargs=None):
15                 self.as_var = as_var
16                 self.view_name = view_name
17                 
18                 # Because the following variables have already been compiled as filters if they exist, they don't need to be re-scanned as template variables.
19                 self.node = node
20                 self.with_obj = with_obj
21                 self.args = args
22                 self.kwargs = kwargs
23         
24         def render(self, context):
25                 if self.node:
26                         node = self.node.resolve(context)
27                 else:
28                         node = context.get('node', None)
29                 
30                 if not node:
31                         return settings.TEMPLATE_STRING_IF_INVALID
32                 
33                 if self.with_obj is None and self.view_name is None:
34                         url = node.get_absolute_url()
35                 else:
36                         if not node.view.accepts_subpath:
37                                 return settings.TEMPLATE_STRING_IF_INVALID
38                         
39                         if self.with_obj is not None:
40                                 try:
41                                         view_name, args, kwargs = node.view.get_reverse_params(self.with_obj.resolve(context))
42                                 except ViewCanNotProvideSubpath:
43                                         return settings.TEMPLATE_STRING_IF_INVALID
44                         else: # self.view_name is not None
45                                 view_name = self.view_name
46                                 args = [arg.resolve(context) for arg in self.args]
47                                 kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context)) for k, v in self.kwargs.items()])
48                         
49                         url = ''
50                         try:
51                                 subpath = reverse(view_name, urlconf=node.view, args=args, kwargs=kwargs)
52                         except NoReverseMatch:
53                                 if self.as_var is None:
54                                         if settings.TEMPLATE_DEBUG:
55                                                 raise
56                                         return settings.TEMPLATE_STRING_IF_INVALID
57                         else:
58                                 url = node.construct_url(subpath)
59                 
60                 if self.as_var:
61                         context[self.as_var] = url
62                         return ''
63                 else:
64                         return url
65
66
67 @register.tag(name='node_url')
68 def do_node_url(parser, token):
69         """
70         {% node_url [for <node>] [as <var>] %}
71         {% node_url with <obj> [for <node>] [as <var>] %}
72         {% node_url <view_name> [<arg1> [<arg2> ...] ] [for <node>] [as <var>] %}
73         {% node_url <view_name> [<key1>=<value1> [<key2>=<value2> ...] ] [for <node>] [as <var>]%}
74         """
75         params = token.split_contents()
76         tag = params[0]
77         as_var = None
78         with_obj = None
79         node = None
80         params = params[1:]
81         
82         if len(params) >= 2 and params[-2] == 'as':
83                 as_var = params[-1]
84                 params = params[:-2]
85         
86         if len(params) >= 2 and params[-2] == 'for':
87                 node = parser.compile_filter(params[-1])
88                 params = params[:-2]
89         
90         if len(params) >= 2 and params[-2] == 'with':
91                 with_obj = parser.compile_filter(params[-1])
92                 params = params[:-2]
93         
94         if with_obj is not None:
95                 if params:
96                         raise template.TemplateSyntaxError('`%s` template tag accepts no arguments or keyword arguments if with <obj> is specified.' % tag)
97                 return NodeURLNode(with_obj=with_obj, node=node, as_var=as_var)
98         
99         if params:
100                 args = []
101                 kwargs = {}
102                 view_name = params.pop(0)
103                 for param in params:
104                         match = kwarg_re.match(param)
105                         if not match:
106                                 raise TemplateSyntaxError("Malformed arguments to `%s` tag" % tag)
107                         name, value = match.groups()
108                         if name:
109                                 kwargs[name] = parser.compile_filter(value)
110                         else:
111                                 args.append(parser.compile_filter(value))
112                 return NodeURLNode(view_name=view_name, args=args, kwargs=kwargs, node=node, as_var=as_var)
113         
114         return NodeURLNode(node=node, as_var=as_var)