comparison gpp/forums/feeds.py @ 170:6f14970b103a

Implement #52 Forums RSS feeds.
author Brian Neal <bgneal@gmail.com>
date Thu, 11 Feb 2010 02:29:03 +0000
parents
children b7ac381996e8
comparison
equal deleted inserted replaced
169:7071b196ddd5 170:6f14970b103a
1 """This file contains the feed class for the forums application."""
2 import datetime
3
4 from django.contrib.syndication.feeds import Feed
5 from django.core.exceptions import ObjectDoesNotExist
6
7 from forums.models import Forum
8 from forums.models import Post
9
10 BASE_YEAR = 2010
11
12 class ForumsFeed(Feed):
13 """The Feed class for the forums application"""
14
15 def get_object(self, bits):
16 # only return public forums
17 if len(bits) == 1:
18 forum = Forum.objects.get(slug=bits[0])
19 if forum.category.groups.count() > 0:
20 raise ObjectDoesNotExist
21 return forum
22
23 elif len(bits) == 0:
24 # return None to indicate we want a combined feed
25 return None
26
27 def title(self, obj):
28 if obj is None:
29 forum_name = 'Combined'
30 else:
31 forum_name = obj.name
32
33 return 'SurfGuitar101.com %s Forum Feed' % forum_name
34
35 def link(self, obj):
36 if obj is None:
37 bits = ''
38 else:
39 bits = obj.slug + '/'
40
41 return '/feeds/forums/' + bits
42
43 def description(self, obj):
44 if obj is None:
45 return "User posts to SurfGuitar101.com forums."
46 return obj.description
47
48 def copyright(self):
49 curr_year = datetime.datetime.now().year
50 if curr_year == BASE_YEAR:
51 year_range = str(BASE_YEAR)
52 else:
53 year_range = "%d - %d" % (BASE_YEAR, curr_year)
54
55 return 'Copyright (C) %s, SurfGuitar101.com' % year_range
56
57 ttl = '720'
58
59 title_template = 'forums/feed_title.html'
60 description_template = 'forums/feed_description.html'
61
62 def items(self, obj):
63 if obj is None:
64 # return a combined feed of public forum threads
65 items = Post.objects.filter(
66 topic__forum__in=Forum.objects.public_forums())
67 else:
68 items = Post.objects.filter(topic__forum__id=obj.id)
69 return items.order_by('-creation_date').select_related()[:10]
70
71 def item_pubdate(self, item):
72 return item.creation_date
73
74 def item_categories(self, item):
75 return (item.topic.forum.name, )