Shunted the responsibility in Entity proxy models for setting an Attribute's value...
[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
5
6 register = template.Library()
7
8
9 class NodeURLNode(template.Node):
10         def __init__(self, node, with_obj, as_var):
11                 if node is not None:
12                         self.node = template.Variable(node)
13                 else:
14                         self.node = None
15                 
16                 if with_obj is not None:
17                         self.with_obj = template.Variable(with_obj)
18                 else:
19                         self.with_obj = None
20                 
21                 self.as_var = as_var
22         
23         def render(self, context):
24                 try:
25                         if self.node:
26                                 node = self.node.resolve(context)
27                         else:
28                                 node = context['node']
29                         current_site = Site.objects.get_current()
30                         if node.has_ancestor(current_site.root_node):
31                                 url = '/' + node.get_path(root=current_site.root_node)
32                                 if self.with_obj:
33                                         with_obj = self.with_obj.resolve(context)
34                                         subpath = node.view.get_subpath(with_obj)
35                                         if subpath[0] is '/':
36                                                 subpath = subpath[1:]
37                                         url += subpath
38                         else:
39                                 return settings.TEMPLATE_STRING_IF_INVALID
40                         
41                         if self.as_var:
42                                 context[self.as_var] = url
43                                 return settings.TEMPLATE_STRING_IF_INVALID
44                         else:
45                                 return url
46                 except:
47                         return settings.TEMPLATE_STRING_IF_INVALID
48
49
50 @register.tag(name='node_url')
51 def do_node_url(parser, token):
52         """
53         {% node_url [<node>] [with <obj>] [as <var>] %}
54         """
55         params = token.split_contents()
56         tag = params[0]
57         
58         if len(params) <= 6:
59                 node = None
60                 with_obj = None
61                 as_var = None
62                 remaining_tokens = params[1:]
63                 while remaining_tokens:
64                         option_token = remaining_tokens.pop(0)
65                         if option_token == 'with':
66                                 try:
67                                         with_obj = remaining_tokens.pop(0)
68                                 except IndexError:
69                                         raise template.TemplateSyntaxError('"%s" template tag option "with" requires an argument specifying an object handled by the view on the node' % tag)
70                         elif option_token == 'as':
71                                 try:
72                                         as_var = remaining_tokens.pop(0)
73                                 except IndexError:
74                                         raise template.TemplateSyntaxError('"%s" template tag option "as" requires an argument specifying a variable name' % tag)
75                         else: # node
76                                 node = option_token
77                 return NodeURLNode(node=node, with_obj=with_obj, as_var=as_var)
78         else:
79                 raise template.TemplateSyntaxError('"%s" template tag cannot accept more than five arguments' % tag)