# HG changeset patch # User Brian Neal # Date 1265855343 0 # Node ID 6f14970b103a31662f5a675ac69bb024b68a88ba # Parent 7071b196ddd54e18fb3db1e4fad97b7a5569237a Implement #52 Forums RSS feeds. diff -r 7071b196ddd5 -r 6f14970b103a gpp/forums/feeds.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/forums/feeds.py Thu Feb 11 02:29:03 2010 +0000 @@ -0,0 +1,75 @@ +"""This file contains the feed class for the forums application.""" +import datetime + +from django.contrib.syndication.feeds import Feed +from django.core.exceptions import ObjectDoesNotExist + +from forums.models import Forum +from forums.models import Post + +BASE_YEAR = 2010 + +class ForumsFeed(Feed): + """The Feed class for the forums application""" + + def get_object(self, bits): + # only return public forums + if len(bits) == 1: + forum = Forum.objects.get(slug=bits[0]) + if forum.category.groups.count() > 0: + raise ObjectDoesNotExist + return forum + + elif len(bits) == 0: + # return None to indicate we want a combined feed + return None + + def title(self, obj): + if obj is None: + forum_name = 'Combined' + else: + forum_name = obj.name + + return 'SurfGuitar101.com %s Forum Feed' % forum_name + + def link(self, obj): + if obj is None: + bits = '' + else: + bits = obj.slug + '/' + + return '/feeds/forums/' + bits + + def description(self, obj): + if obj is None: + return "User posts to SurfGuitar101.com forums." + return obj.description + + def copyright(self): + curr_year = datetime.datetime.now().year + if curr_year == BASE_YEAR: + year_range = str(BASE_YEAR) + else: + year_range = "%d - %d" % (BASE_YEAR, curr_year) + + return 'Copyright (C) %s, SurfGuitar101.com' % year_range + + ttl = '720' + + title_template = 'forums/feed_title.html' + description_template = 'forums/feed_description.html' + + def items(self, obj): + if obj is None: + # return a combined feed of public forum threads + items = Post.objects.filter( + topic__forum__in=Forum.objects.public_forums()) + else: + items = Post.objects.filter(topic__forum__id=obj.id) + return items.order_by('-creation_date').select_related()[:10] + + def item_pubdate(self, item): + return item.creation_date + + def item_categories(self, item): + return (item.topic.forum.name, ) diff -r 7071b196ddd5 -r 6f14970b103a gpp/forums/models.py --- a/gpp/forums/models.py Sun Jan 31 04:52:08 2010 +0000 +++ b/gpp/forums/models.py Thu Feb 11 02:29:03 2010 +0000 @@ -42,6 +42,10 @@ return self.groups.filter(user__pk=user.id).count() > 0 return False + def is_public(self): + """Returns true if this is a public category, viewable by all.""" + return self.groups.count() == 0 + class ForumManager(models.Manager): """ @@ -62,6 +66,10 @@ qs = self._for_user(user) return qs.values_list('id', flat=True) + def public_forums(self): + """Returns a queryset containing the public forums.""" + return self.filter(category__groups__isnull=True) + def _for_user(self, user): """Common code for the xxx_for_user() methods.""" if user.is_superuser: diff -r 7071b196ddd5 -r 6f14970b103a gpp/forums/views.py --- a/gpp/forums/views.py Sun Jan 31 04:52:08 2010 +0000 +++ b/gpp/forums/views.py Thu Feb 11 02:29:03 2010 +0000 @@ -82,10 +82,20 @@ elif query == "mine": return redirect('forums-my_posts') + public_forums = Forum.objects.public_forums() + feeds = [{'name': 'All Forums', 'feed': '/feeds/forums/'}] + forums = Forum.objects.forums_for_user(request.user) get_forum_unread_status(forums, request.user) cats = {} for forum in forums: + forum.has_feed = forum in public_forums + if forum.has_feed: + feeds.append({ + 'name': '%s Forum' % forum.name, + 'feed': '/feeds/forums/%s/' % forum.slug, + }) + cat = cats.setdefault(forum.category.id, { 'cat': forum.category, 'forums': [], @@ -97,6 +107,7 @@ return render_to_response('forums/index.html', { 'cats': cats, + 'feeds': feeds, }, context_instance=RequestContext(request)) diff -r 7071b196ddd5 -r 6f14970b103a gpp/news/feeds.py --- a/gpp/news/feeds.py Sun Jan 31 04:52:08 2010 +0000 +++ b/gpp/news/feeds.py Thu Feb 11 02:29:03 2010 +0000 @@ -1,28 +1,40 @@ """ This file contains the feed classes for the news application. """ +import datetime from django.contrib.syndication.feeds import Feed + from news.models import Story +BASE_YEAR = 2010 + class LatestNewsFeed(Feed): - """The Feed class for the news application""" + """The Feed class for the news application""" - title = 'SurfGuitar101.com News Feed' - link = '/feeds/news/' - description = 'News articles and stories from SurfGuitar101.com' - copyright = 'Copyright (C) 2008, Brian Neal' - ttl = '720' + title = 'SurfGuitar101.com News Feed' + link = '/feeds/news/' + description = 'News articles and stories from SurfGuitar101.com' + ttl = '720' - title_template = 'news/feed_title.html' - description_template = 'news/feed_description.html' - - def items(self): - return Story.objects.order_by('-date_published')[:5] + title_template = 'news/feed_title.html' + description_template = 'news/feed_description.html' - def item_pubdate(self, item): - return item.date_published + def copyright(self): + curr_year = datetime.datetime.now().year + if curr_year == BASE_YEAR: + year_range = str(BASE_YEAR) + else: + year_range = "%d - %d" % (BASE_YEAR, curr_year) - def item_categories(self, item): - return (item.category.title, ) + return 'Copyright (C) %s, SurfGuitar101.com' % year_range + + def items(self): + return Story.objects.order_by('-date_published')[:5] + + def item_pubdate(self, item): + return item.date_published + + def item_categories(self, item): + return (item.category.title, ) diff -r 7071b196ddd5 -r 6f14970b103a gpp/templates/forums/feed_description.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/feed_description.html Thu Feb 11 02:29:03 2010 +0000 @@ -0,0 +1,1 @@ +{{ obj.html|safe }} diff -r 7071b196ddd5 -r 6f14970b103a gpp/templates/forums/feed_title.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/feed_title.html Thu Feb 11 02:29:03 2010 +0000 @@ -0,0 +1,1 @@ +{{ obj.topic.name }} diff -r 7071b196ddd5 -r 6f14970b103a gpp/templates/forums/index.html --- a/gpp/templates/forums/index.html Sun Jan 31 04:52:08 2010 +0000 +++ b/gpp/templates/forums/index.html Thu Feb 11 02:29:03 2010 +0000 @@ -1,9 +1,14 @@ {% extends 'base.html' %} {% load cache %} {% load forum_tags %} +{% block custom_head %} +{% for feed in feeds %} + +{% endfor %} +{% endblock %} {% block title %}Forums{% endblock %} {% block content %} -

Forums

+

Forums Forums Feed