Commits

Kyle Fuller committed a8d94a5

forum: Add rss feeds for posts

Closes #4

  • Participants
  • Parent commits 39cd2d6

Comments (0)

Files changed (2)

File lithium/forum/feeds.py

+from django.core.urlresolvers import reverse
+from django.contrib.syndication.views import Feed
+from django.shortcuts import get_object_or_404
+from lithium.forum.models import Forum, Post
+from lithium.forum.utils import user_permission_level
+
+class LatestPostFeed(Feed):
+    def get_object(self, request, slug=None):
+        self.request = request
+        if slug:
+            user_permission = user_permission_level(request.user)
+            return get_object_or_404(Forum, slug=slug, read__lte=user_permission)
+
+    def link(self, obj):
+        if obj:
+            return obj.get_absolute_url()
+        return reverse('forum.forum_index')
+
+    def title(self, obj):
+        if obj:
+            return obj.title
+        return 'All posts'
+
+    def description(self, obj):
+        if obj:
+            return obj.description
+        return 'All posts'
+
+    def items(self, obj):
+        user_permission = user_permission_level(self.request.user)
+        return Post.objects.prefetch_related('thread__forum').filter(thread__forum__read__lte=user_permission)[:20]
+
+    def item_title(self, item):
+        return item.thread.title
+
+    def item_description(self, item):
+        return item.content
+
+    def item_pubdate(self, item):
+        return item.pub_date
+
+    def item_categories(self, item):
+        return [item.thread.title]
+

File lithium/forum/urls.py

 from django.conf.urls.defaults import *
 from lithium.forum.models import Forum
+from lithium.forum.feeds import *
 
 urlpatterns = patterns('lithium.forum.views',
     url(r'^$', 'forum_index', name='forum.forum_index'),
     url(r'^threads/$', 'thread_list', name='forum.threads'),
+    url(r'^all.rss$', LatestPostFeed()),
+    url(r'^(?P<slug>[-\w]+).rss$', LatestPostFeed()),
     url(r'^(?P<forum>[-\w]+)/$', 'forum_detail', name='forum.forum_detail'),
     url(r'^(?P<forum>[-\w]+)/create/$', 'thread_create', name='forum.thread_create'),
     url(r'^(?P<forum>[-\w]+)/(?P<slug>[-\w]+)/$', 'thread_detail', name='forum.thread_detail'),