Commits

Anonymous committed 4ed014f

port load_last_topics template tag from pybb

Comments (0)

Files changed (1)

djangobb/djangobb_forum/templatetags/forum_extras.py

 # -*- coding: utf-8
 from datetime import datetime, timedelta
 import urllib
+import re
 
 from django import template
 from django.core.urlresolvers import reverse
 from djangobb_forum import settings as forum_settings
 
 if forum_settings.PM_SUPPORT:
-    from messages.models import inbox_count_for
+    from django_messages.models import inbox_count_for
 
 register = template.Library()
 
 
 @register.simple_tag
 def set_markup_editor(user, markup=None):
-    markup_style = '' 
+    markup_style = ''
     if user.is_authenticated():
         markup_style = '''
             <link rel="stylesheet" type="text/css" href="%(media_url)sforum/js/markitup/skins/markitup/style.css" />
             markup=markup if markup else user.forum_profile.markup
         )
     return markup_style
+
+
+@register.tag(name='djangobb_load_last_topics')
+def do_djangobb_load_last_topics(parser, token):
+    """
+    Create new context variable pointed to the list of topics.
+
+    Usage examples:
+
+        {% djangobb_load_last_topics as last_topics %}
+        {% djangobb_load_last_topics as last_topics with limit=10 %}
+        {% djangobb_load_last_topics as last_topics with limit=10, category=cat.pk %}
+        {% djangobb_load_last_topics as last_topics with forum=forum.pk,order_by="updated" %}
+
+    Available arguments for with clause:
+        limit: limitation of number of loaded items
+        category: primary key of category to load topics from
+        forum: primary key of forum to load topics from
+        order_by: the Topic field name to order the topic selection with. Default is "created"
+    """
+
+    try:
+        tag_name, arg = token.contents.split(None, 1)
+    except ValueError:
+        raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0]
+
+    match = re.search(r'as\s+(\w+)(?:\s+with\s+(.+))?', arg)
+    if not match:
+        raise template.TemplateSyntaxError, "%r tag had invalid arguments" % tag_name
+    name = match.group(1)
+
+    limit = '10'
+    category = '0'
+    order_by='"created"'
+
+    if match.group(2):
+        args = dict([x.strip().split('=') for x in match.group(2).split(',')])
+        if 'limit' in args:
+            limit = args['limit']
+        if 'category' in args:
+            category = args['category']
+        if 'order_by' in args:
+            order_by = args['order_by']
+
+    return DjangobbLoadLastTopicsNode(name, limit, category, order_by)
+
+class DjangobbLoadLastTopicsNode(template.Node):
+    def __init__(self, name, limit, category, order_by):
+        self.name = name
+        self.limit = template.Variable(limit)
+        self.category = template.Variable(category)
+        self.order_by = template.Variable(order_by)
+
+    def render(self, context):
+        limit = self.limit.resolve(context)
+        category = self.category.resolve(context)
+        order_by = self.order_by.resolve(context)
+        topics = Topic.objects.all().select_related().order_by('-' + order_by)
+        if category:
+            topics = topics.filter(forum__category__pk=category)
+        context[self.name] = topics[:limit]
+        return ''