comparison gpp/forums/feeds.py @ 176:b7ac381996e8

Implement ticket #59; update RSS feeds for Django 1.2.
author Brian Neal <bgneal@gmail.com>
date Thu, 11 Mar 2010 02:34:07 +0000
parents 6f14970b103a
children 9b63ad1f2ad2
comparison
equal deleted inserted replaced
175:776028f4bced 176:b7ac381996e8
1 """This file contains the feed class for the forums application.""" 1 """This file contains the feed class for the forums application."""
2 import datetime
3 2
4 from django.contrib.syndication.feeds import Feed 3 from django.contrib.syndication.views import Feed
5 from django.core.exceptions import ObjectDoesNotExist 4 from django.core.exceptions import ObjectDoesNotExist
5 from django.shortcuts import get_object_or_404
6 from django.utils.decorators import method_decorator
7 from django.views.decorators.cache import cache_page
6 8
7 from forums.models import Forum 9 from forums.models import Forum
8 from forums.models import Post 10 from forums.models import Post
11 from core.functions import copyright_str
9 12
10 BASE_YEAR = 2010
11 13
12 class ForumsFeed(Feed): 14 class ForumsFeed(Feed):
13 """The Feed class for the forums application""" 15 """The Feed class for a specific forum"""
14 16
15 def get_object(self, bits): 17 ttl = '720'
18 author_name = 'Brian Neal'
19 author_email = 'admin@surfguitar101.com'
20
21 @method_decorator(cache_page(15 * 60))
22 def __call__(self, request, *args, **kwargs):
23 return super(ForumsFeed, self).__call__(request, *args, **kwargs)
24
25 def get_object(self, request, forum_slug):
16 # only return public forums 26 # only return public forums
17 if len(bits) == 1: 27 if forum_slug:
18 forum = Forum.objects.get(slug=bits[0]) 28 forum = get_object_or_404(Forum, slug=forum_slug)
19 if forum.category.groups.count() > 0: 29 if forum.category.groups.count() > 0:
20 raise ObjectDoesNotExist 30 raise ObjectDoesNotExist
21 return forum 31 return forum
22 32
23 elif len(bits) == 0: 33 else:
24 # return None to indicate we want a combined feed 34 # return None to indicate we want a combined feed
25 return None 35 return None
26 36
27 def title(self, obj): 37 def title(self, obj):
28 if obj is None: 38 if obj is None:
43 def description(self, obj): 53 def description(self, obj):
44 if obj is None: 54 if obj is None:
45 return "User posts to SurfGuitar101.com forums." 55 return "User posts to SurfGuitar101.com forums."
46 return obj.description 56 return obj.description
47 57
48 def copyright(self): 58 def feed_copyright(self):
49 curr_year = datetime.datetime.now().year 59 return copyright_str()
50 if curr_year == BASE_YEAR:
51 year_range = str(BASE_YEAR)
52 else:
53 year_range = "%d - %d" % (BASE_YEAR, curr_year)
54 60
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): 61 def items(self, obj):
63 if obj is None: 62 if obj is None:
64 # return a combined feed of public forum threads 63 # return a combined feed of public forum threads
65 items = Post.objects.filter( 64 items = Post.objects.filter(
66 topic__forum__in=Forum.objects.public_forums()) 65 topic__forum__in=Forum.objects.public_forums())
67 else: 66 else:
68 items = Post.objects.filter(topic__forum__id=obj.id) 67 items = Post.objects.filter(topic__forum__id=obj.id)
69 return items.order_by('-creation_date').select_related()[:10] 68 return items.order_by('-creation_date').select_related()[:10]
70 69
70 def item_title(self, item):
71 return item.topic.name
72
73 def item_description(self, item):
74 return item.html
75
76 def item_author_name(self, item):
77 return item.user.username
78
71 def item_pubdate(self, item): 79 def item_pubdate(self, item):
72 return item.creation_date 80 return item.creation_date
73 81
74 def item_categories(self, item): 82 def item_categories(self, item):
75 return (item.topic.forum.name, ) 83 return (item.topic.forum.name, )