Added NodeOverrideInlineFormSet to clean up admin editing of node overrides by only...
[philo.git] / 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                                         raise
55                         else:
56                                 if subpath[0] == '/':
57                                         subpath = subpath[1:]
58                                 
59                                 url = node.get_absolute_url() + subpath
60                 
61                 if self.as_var:
62                         context[self.as_var] = url
63                         return ''
64                 else:
65                         return url
66
67
68 @register.tag(name='node_url')
69 def do_node_url(parser, token):
70         """
71         {% node_url [for <node>] [as <var] %}
72         {% node_url with <obj> [for <node>] [as <var>] %}
73         {% node_url <view_name> [<arg1> [<arg2> ...] ] [for <node>] [as <var>] %}
74         {% node_url <view_name> [<key1>=<value1> [<key2>=<value2> ...] ] [for <node>] [as <var>]%}
75         """
76         params = token.split_contents()
77         tag = params[0]
78         as_var = None
79         with_obj = None
80         node = None
81         params = params[1:]
82         
83         if len(params) >= 2 and params[-2] == 'as':
84                 as_var = params[-1]
85                 params = params[:-2]
86         
87         if len(params) >= 2 and params[-2] == 'for':
88                 node = parser.compile_filter(params[-1])
89                 params = params[:-2]
90         
91         if len(params) >= 2 and params[-2] == 'with':
92                 with_obj = parser.compile_filter(params[-1])
93                 params = params[:-2]
94         
95         if with_obj is not None:
96                 if params:
97                         raise template.TemplateSyntaxError('`%s` template tag accepts no arguments or keyword arguments if with <obj> is specified.' % tag)
98                 return NodeURLNode(with_obj=with_obj, node=node, as_var=as_var)
99         
100         if params:
101                 args = []
102                 kwargs = {}
103                 view_name = params.pop(0)
104                 for param in params:
105                         match = kwarg_re.match(param)
106                         if not match:
107                                 raise TemplateSyntaxError("Malformed arguments to `%s` tag" % tag)
108                         name, value = match.groups()
109                         if name:
110                                 kwargs[name] = parser.compile_filter(value)
111                         else:
112                                 args.append(parser.compile_filter(value))
113                 return NodeURLNode(view_name=view_name, args=args, kwargs=kwargs, node=node, as_var=as_var)
114         
115         return NodeURLNode(node=node, as_var=as_var)
116
117
118 class NavigationNode(template.Node):
119         def __init__(self, node=None, as_var=None):
120                 self.as_var = as_var
121                 self.node = node
122         
123         def render(self, context):
124                 if 'request' not in context:
125                         return settings.TEMPLATE_STRING_IF_INVALID
126                 
127                 if self.node:
128                         node = self.node.resolve(context)
129                 else:
130                         node = context.get('node', None)
131                 
132                 if not node:
133                         return settings.TEMPLATE_STRING_IF_INVALID
134                 
135                 try:
136                         nav_root = node.attributes['navigation_root']
137                 except KeyError:
138                         if settings.TEMPLATE_DEBUG:
139                                 raise
140                         return settings.TEMPLATE_STRING_IF_INVALID
141                 
142                 # Should I get its override and check for a max depth override there?
143                 navigation = nav_root.get_navigation()
144                 
145                 if self.as_var:
146                         context[self.as_var] = navigation
147                         return ''
148                 
149                 return self.compile(navigation, context['request'].path, nav_root.get_absolute_url(), nav_root.get_level(), nav_root.get_level() + 3)
150         
151         def compile(self, navigation, active_path, root_url, current_depth, max_depth):
152                 compiled = ""
153                 for item in navigation:
154                         if item['url'] in active_path and (item['url'] != root_url or root_url == active_path):
155                                 compiled += "<li class='active'>"
156                         else:
157                                 compiled += "<li>"
158                         
159                         if item['url']:
160                                 compiled += "<a href='%s'>" % item['url']
161                         
162                         compiled += item['title']
163                         
164                         if item['url']:
165                                 compiled += "</a>"
166                         
167                         if 'children' in item and current_depth < max_depth:
168                                 compiled += "<ul>%s</ul>" % self.compile(item['children'], active_path, root_url, current_depth + 1, max_depth)
169                         
170                         compiled += "</li>"
171                 return compiled
172
173
174 @register.tag(name='navigation')
175 def do_navigation(parser, token):
176         """
177         {% navigation [for <node>] [as <var>] %}
178         """
179         bits = token.split_contents()
180         tag = bits[0]
181         bits = bits[1:]
182         node = None
183         as_var = None
184         
185         if len(bits) >= 2 and bits[-2] == 'as':
186                 as_var = bits[-1]
187                 bits = bits[:-2]
188         
189         if len(bits) >= 2 and bits[-2] == 'for':
190                 node = parser.compile_filter(bits[-1])
191                 bits = bits[-2]
192         
193         if bits:
194                 raise template.TemplateSyntaxError('`%s` template tag expects the syntax {%% %s [for <node>] [as <var>] %}' % (tag, tag))
195         return NavigationNode(node, as_var)