Merge branch 'master' into gilbert
[philo.git] / templatetags / nodes.py
index 6cee439..5ae507d 100644 (file)
@@ -3,6 +3,7 @@ from django.conf import settings
 from django.contrib.sites.models import Site
 from django.core.urlresolvers import reverse, NoReverseMatch
 from django.template.defaulttags import kwarg_re
+from django.utils.encoding import smart_str
 from philo.exceptions import ViewCanNotProvideSubpath
 
 
@@ -24,34 +25,37 @@ class NodeURLNode(template.Node):
                if self.node:
                        node = self.node.resolve(context)
                else:
-                       node = context['node']
+                       node = context.get('node', None)
                
-               if self.with_obj is not None:
-                       try:
-                               view_name, args, kwargs = node.view.get_reverse_params(self.with_obj.resolve(context))
-                       except ViewCanNotProvideSubpath:
-                               return settings.TEMPLATE_STRING_IF_INVALID
-               elif self.view_name is not None:
-                       view_name = self.view_name
-                       args = [arg.resolve(context) for arg in self.args]
-                       kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context)) for k, v in self.kwargs.items()])
-               else:
-                       return node.get_absolute_url()
-               
-               if not node.view.accepts_subpath:
+               if not node:
                        return settings.TEMPLATE_STRING_IF_INVALID
                
-               url = ''
-               try:
-                       subpath = reverse(view_name, urlconf=node.view, args=args, kwargs=kwargs)
-               except NoReverseMatch:
-                       if self.as_var is None:
-                               raise
+               if self.with_obj is None and self.view_name is None:
+                       url = node.get_absolute_url()
                else:
-                       if subpath[0] == '/':
-                               subpath = subpath[1:]
+                       if not node.view.accepts_subpath:
+                               return settings.TEMPLATE_STRING_IF_INVALID
+                       
+                       if self.with_obj is not None:
+                               try:
+                                       view_name, args, kwargs = node.view.get_reverse_params(self.with_obj.resolve(context))
+                               except ViewCanNotProvideSubpath:
+                                       return settings.TEMPLATE_STRING_IF_INVALID
+                       else: # self.view_name is not None
+                               view_name = self.view_name
+                               args = [arg.resolve(context) for arg in self.args]
+                               kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context)) for k, v in self.kwargs.items()])
                        
-                       url = node.get_absolute_url() + subpath
+                       url = ''
+                       try:
+                               subpath = reverse(view_name, urlconf=node.view, args=args, kwargs=kwargs)
+                       except NoReverseMatch:
+                               if self.as_var is None:
+                                       if settings.TEMPLATE_DEBUG:
+                                               raise
+                                       return settings.TEMPLATE_STRING_IF_INVALID
+                       else:
+                               url = node.construct_url(subpath)
                
                if self.as_var:
                        context[self.as_var] = url
@@ -63,7 +67,7 @@ class NodeURLNode(template.Node):
 @register.tag(name='node_url')
 def do_node_url(parser, token):
        """
-       {% node_url [for <node>] [as <var] %}
+       {% node_url [for <node>] [as <var>] %}
        {% node_url with <obj> [for <node>] [as <var>] %}
        {% node_url <view_name> [<arg1> [<arg2> ...] ] [for <node>] [as <var>] %}
        {% node_url <view_name> [<key1>=<value1> [<key2>=<value2> ...] ] [for <node>] [as <var>]%}