1. Curtis Maloney
  2. gnocchi-blog

Source

gnocchi-blog / gnocchi / blog / templatetags / blog.py

from django import template
from gnocchi.blog.models import Post

register = template.Library()

@register.assignment_tag
def get_recent_posts(count, username=None, *tags, **kwargs):
    qset = Post.objects.current()
    for tag in filter(None, tags):
        qset = qset.filter(tags__name=tag)
    if username:
        qset = qset.filter(posted_by__username=username)
    return qset[:count]

@register.filter
def recent_posts(value, username=None):
    qset = Post.objects.current()
    if username:
        qset.filter(posted_by__username=username)
    try:
        return qset[:int(value)]
    except Post.DoesNotExist:
        return []

@register.assignment_tag(name='most_common')
def blog_common_tags():
    return Post.tags.most_common()