Anonymous avatar Anonymous committed a481442

Switch to using assignment tag

Comments (0)

Files changed (2)

gnocchi/blog/models.py

 
     class Meta:
         ordering = ('-post_date',)
+
     def __unicode__(self):
         return self.title
+
     @models.permalink
     def get_absolute_url(self):
         kwargs = {'slug': self.slug,}
 
     class Meta:
         ordering = ('post_date',)
+
     def __unicode__(self):
         return u'%s...' % self.content[:50]
 

gnocchi/blog/templatetags/blog.py

 from django import template
-from django.template.base import FilterExpression
 from gnocchi.blog.models import Post
-from gnocchi.tools.template import parse_args
 
 register = template.Library()
 
-class RecentPostsNode(template.Node):
-    def __init__(self, count, varname, username=None, *tags):
-        self.count = count
-        self.varname = varname
-        self.tags = tags
-        self.username = username
-    def render(self, context):
-        def resolve(var):
-            if isinstance(var, FilterExpression):
-                return var.resolve(context)
-            return var
-
-        count = self.count.resolve(context)
-
-        tags = filter(None, [tag.resolve(context) for tag in self.tags])
-
-        varname = resolve(self.varname)
-        username = resolve(self.username)
-
-        qset = Post.objects.current()
-        for tag in tags:
-            qset = qset.filter(tags__name=tag)
-        if username:
-            qset = qset.filter(posted_by__username=username)
-        context[varname] = qset[:count]
-        return ''
-
-@register.tag
-def get_recent_posts(parser, token):
-    args, kwargs, as_name = parse_args(parser, token, 'posts')
-    count = args.pop(0)
-    username = kwargs.pop('username', None)
-    return RecentPostsNode(count, as_name, username, *args, **kwargs)
+@regiser.assignment_tag
+def get_recent_posts(count, username=None, *tags, **kwargs):
+    tags = filter(None, tags)
+    qset = Post.objects.current()
+    for tag in tags:
+        qset = qset.filter(tags__name=tag)
+    if username:
+        qset = qset.filter(posted_bt__username=username)
+    return qset
 
 @register.filter
 def recent_posts(value, username=None):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.