bgneal@170: """This file contains the feed class for the forums application.""" bgneal@170: import datetime bgneal@170: bgneal@170: from django.contrib.syndication.feeds import Feed bgneal@170: from django.core.exceptions import ObjectDoesNotExist bgneal@170: bgneal@170: from forums.models import Forum bgneal@170: from forums.models import Post bgneal@170: bgneal@170: BASE_YEAR = 2010 bgneal@170: bgneal@170: class ForumsFeed(Feed): bgneal@170: """The Feed class for the forums application""" bgneal@170: bgneal@170: def get_object(self, bits): bgneal@170: # only return public forums bgneal@170: if len(bits) == 1: bgneal@170: forum = Forum.objects.get(slug=bits[0]) bgneal@170: if forum.category.groups.count() > 0: bgneal@170: raise ObjectDoesNotExist bgneal@170: return forum bgneal@170: bgneal@170: elif len(bits) == 0: bgneal@170: # return None to indicate we want a combined feed bgneal@170: return None bgneal@170: bgneal@170: def title(self, obj): bgneal@170: if obj is None: bgneal@170: forum_name = 'Combined' bgneal@170: else: bgneal@170: forum_name = obj.name bgneal@170: bgneal@170: return 'SurfGuitar101.com %s Forum Feed' % forum_name bgneal@170: bgneal@170: def link(self, obj): bgneal@170: if obj is None: bgneal@170: bits = '' bgneal@170: else: bgneal@170: bits = obj.slug + '/' bgneal@170: bgneal@170: return '/feeds/forums/' + bits bgneal@170: bgneal@170: def description(self, obj): bgneal@170: if obj is None: bgneal@170: return "User posts to SurfGuitar101.com forums." bgneal@170: return obj.description bgneal@170: bgneal@170: def copyright(self): bgneal@170: curr_year = datetime.datetime.now().year bgneal@170: if curr_year == BASE_YEAR: bgneal@170: year_range = str(BASE_YEAR) bgneal@170: else: bgneal@170: year_range = "%d - %d" % (BASE_YEAR, curr_year) bgneal@170: bgneal@170: return 'Copyright (C) %s, SurfGuitar101.com' % year_range bgneal@170: bgneal@170: ttl = '720' bgneal@170: bgneal@170: title_template = 'forums/feed_title.html' bgneal@170: description_template = 'forums/feed_description.html' bgneal@170: bgneal@170: def items(self, obj): bgneal@170: if obj is None: bgneal@170: # return a combined feed of public forum threads bgneal@170: items = Post.objects.filter( bgneal@170: topic__forum__in=Forum.objects.public_forums()) bgneal@170: else: bgneal@170: items = Post.objects.filter(topic__forum__id=obj.id) bgneal@170: return items.order_by('-creation_date').select_related()[:10] bgneal@170: bgneal@170: def item_pubdate(self, item): bgneal@170: return item.creation_date bgneal@170: bgneal@170: def item_categories(self, item): bgneal@170: return (item.topic.forum.name, )