Author: leidel
Date: Sat Sep 27 08:06:51 2008
New Revision: 170

Modified:
    trunk/wiki/templatetags/wikiurl.py
    trunk/wiki/views.py

Log:
Fixed #63, enabled wikiurl template tag to pass result to template context  
like Django's url template tag

Modified: trunk/wiki/templatetags/wikiurl.py
==============================================================================
--- trunk/wiki/templatetags/wikiurl.py  (original)
+++ trunk/wiki/templatetags/wikiurl.py  Sat Sep 27 08:06:51 2008
@@ -7,11 +7,12 @@

  class WikiURLNode(Node):
      def __init__(self, url_name, group,
-                 article=None, revision=None):
+                 article=None, revision=None, asvar=None):
          self.url_name = 'wiki_' + url_name
          self.group = group
          self.article = article
          self.revision = revision
+        self.asvar = asvar

      def resolve(self, attrname, context):
          attr = getattr(self, attrname)
@@ -33,11 +34,19 @@
          app = group._meta.app_label
          urlconf = '.'.join([app, 'urls'])

+        url = ''
          try:
              url = reverse(self.url_name, urlconf, kwargs=kw)
              return ''.join(['/', app, url]) # @@@ hardcoding  
/app_name/wiki_url/
          except NoReverseMatch, err:
+            if self.asvar is None:
+                raise
+
+        if self.asvar:
+            context[self.asvar] = url
              return ''
+        else:
+            return url

  def wikiurl(parser, token):
      """
@@ -65,22 +74,45 @@
          {% wikiurl edit group article %}

      The URL will look like ``groups/some_group/mywiki/edit/WikiWord/``.
-    """
-    bits = token.contents.split(' ', 4)
+
+    This tag is also able to set a context variable instead of returning  
the
+    found URL by specifying it with the 'as' keyword::

-    try:
+        {% wikiurl edit group article as wiki_article_url %}
+
+    """
+    bits = token.contents.split(' ')
+    kwargs = {}
+    if len(bits) == 3: # {% wikiurl url_name group %}
          url_name = bits[1]
          group = parser.compile_filter(bits[2])
-    except IndexError:
-        raise TemplateSyntaxError("'%s' takes at least two arguments"
-                                  " (url name and group)" % bits[0])
-    kw = {}
-    try:
-        kw['article'] = parser.compile_filter(bits[3])
-        kw['revision'] = parser.compile_filter(bits[4])
-    except IndexError:
-        pass
-
-    return WikiURLNode(url_name, group, **kw)
+    elif len(bits) == 4: # {% wikiurl url_name group article %}
+        url_name = bits[1]
+        group = parser.compile_filter(bits[2])
+        kwargs['article'] = parser.compile_filter(bits[3])
+    elif len(bits) == 5: # {% wikiurl url_name group as var %} or {%  
wikiurl url_name group article revision %}
+        url_name = bits[1]
+        group = parser.compile_filter(bits[2])
+        if bits[3] == "as":
+            kwargs['asvar'] = bits[4]
+        else:
+            kwargs['article'] = bits[3]
+            kwargs['revision'] = bits[4]
+    elif len(bits) == 6: # {% wikiurl url_name group article as var %}
+        if bits[4] == "as":
+            raise TemplateSyntaxError("4th argument to %s should  
be 'as'" % bits[0])
+        url_name = bits[1]
+        group = parser.compile_filter(bits[2])
+        kwargs['article'] = parser.compile_filter(bits[3])
+        kwargs['asvar'] = parser.compile_filter(bits[5])
+    elif len(bits) == 7: # {% wikiurl url_name group article revision as  
var %}
+        url_name = bits[1]
+        group = parser.compile_filter(bits[2])
+        kwargs['article'] = parser.compile_filter(bits[3])
+        kwargs['revision'] = parser.compile_filter(bits[4])
+        kwargs['asvar'] = parser.compile_filter(bits[6])
+    else:
+        raise TemplateSyntaxError("wrong number of arguments to %s" %  
bits[0])
+    return WikiURLNode(url_name, group, **kwargs)

  wikiurl = register.tag(wikiurl)

Modified: trunk/wiki/views.py
==============================================================================
--- trunk/wiki/views.py (original)
+++ trunk/wiki/views.py Sat Sep 27 08:06:51 2008
@@ -361,7 +361,8 @@

          article = article_qs.get(**article_args)

-        template_params = {'article_title': article.title,
+        template_params = {'article': article,
+                           'article_title': article.title,
                             'changeset': changeset,
                             'allow_write': allow_write}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pinax-updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pinax-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to