changeset 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 776028f4bced
children 9b63ad1f2ad2
files gpp/core/functions.py gpp/forums/feeds.py gpp/news/feeds.py gpp/templates/forums/feed_description.html gpp/templates/forums/feed_title.html gpp/templates/home.html gpp/templates/news/base.html gpp/templates/news/feed_description.html gpp/templates/news/feed_title.html gpp/urls.py
diffstat 10 files changed, 75 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/core/functions.py	Wed Mar 03 04:09:42 2010 +0000
+++ b/gpp/core/functions.py	Thu Mar 11 02:34:07 2010 +0000
@@ -1,4 +1,5 @@
 """This file houses various core utility functions for GPP"""
+import datetime
 
 import django.core.mail
 from django.contrib.sites.models import Site
@@ -49,3 +50,14 @@
         return full_name
     return user.username
 
+
+BASE_YEAR = 2010
+
+def copyright_str():
+    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
--- a/gpp/forums/feeds.py	Wed Mar 03 04:09:42 2010 +0000
+++ b/gpp/forums/feeds.py	Thu Mar 11 02:34:07 2010 +0000
@@ -1,26 +1,36 @@
 """This file contains the feed class for the forums application."""
-import datetime
 
-from django.contrib.syndication.feeds import Feed
+from django.contrib.syndication.views import Feed
 from django.core.exceptions import ObjectDoesNotExist
+from django.shortcuts import get_object_or_404
+from django.utils.decorators import method_decorator
+from django.views.decorators.cache import cache_page
 
 from forums.models import Forum
 from forums.models import Post
+from core.functions import copyright_str
 
-BASE_YEAR = 2010
 
 class ForumsFeed(Feed):
-    """The Feed class for the forums application"""
+    """The Feed class for a specific forum"""
 
-    def get_object(self, bits):
+    ttl = '720'
+    author_name = 'Brian Neal'
+    author_email = 'admin@surfguitar101.com'
+
+    @method_decorator(cache_page(15 * 60))
+    def __call__(self, request, *args, **kwargs):
+        return super(ForumsFeed, self).__call__(request, *args, **kwargs)
+
+    def get_object(self, request, forum_slug):
         # only return public forums
-        if len(bits) == 1:
-            forum = Forum.objects.get(slug=bits[0])
+        if forum_slug:
+            forum = get_object_or_404(Forum, slug=forum_slug)
             if forum.category.groups.count() > 0:
                 raise ObjectDoesNotExist
             return forum
 
-        elif len(bits) == 0:
+        else:
             # return None to indicate we want a combined feed
             return None
 
@@ -45,20 +55,9 @@
             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)
+    def feed_copyright(self):
+        return copyright_str()
 
-        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
@@ -68,6 +67,15 @@
             items = Post.objects.filter(topic__forum__id=obj.id)
         return items.order_by('-creation_date').select_related()[:10]
 
+    def item_title(self, item):
+        return item.topic.name
+
+    def item_description(self, item):
+        return item.html
+
+    def item_author_name(self, item):
+        return item.user.username
+
     def item_pubdate(self, item):
        return item.creation_date
 
--- a/gpp/news/feeds.py	Wed Mar 03 04:09:42 2010 +0000
+++ b/gpp/news/feeds.py	Thu Mar 11 02:34:07 2010 +0000
@@ -1,13 +1,13 @@
 """
 This file contains the feed classes for the news application.
 """
-import datetime
-
-from django.contrib.syndication.feeds import Feed
+from django.contrib.syndication.views import Feed
+from django.utils.decorators import method_decorator
+from django.views.decorators.cache import cache_page
 
 from news.models import Story
-
-BASE_YEAR = 2010
+from core.functions import get_full_name
+from core.functions import copyright_str
 
 
 class LatestNewsFeed(Feed):
@@ -17,22 +17,28 @@
     link = '/feeds/news/'
     description = 'News articles and stories from SurfGuitar101.com'
     ttl = '720'
+    author_name = 'Brian Neal'
+    author_email = 'admin@surfguitar101.com'
 
-    title_template = 'news/feed_title.html'
-    description_template = 'news/feed_description.html'
+    @method_decorator(cache_page(4 * 60 * 60))
+    def __call__(self, request, *args, **kwargs):
+        return super(LatestNewsFeed, self).__call__(request, *args, **kwargs)
 
-    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
+    def feed_copyright(self):
+        return copyright_str()
     
     def items(self):
         return Story.objects.order_by('-date_published')[:5]
 
+    def item_title(self, item):
+        return item.title
+
+    def item_description(self, item):
+        return item.short_text + item.long_text
+
+    def item_author_name(self, item):
+        return get_full_name(item.submitter)
+
     def item_pubdate(self, item):
         return item.date_published
 
--- a/gpp/templates/forums/feed_description.html	Wed Mar 03 04:09:42 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-{{ obj.html|safe }}
--- a/gpp/templates/forums/feed_title.html	Wed Mar 03 04:09:42 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-{{ obj.topic.name }}
--- a/gpp/templates/home.html	Wed Mar 03 04:09:42 2010 +0000
+++ b/gpp/templates/home.html	Thu Mar 11 02:34:07 2010 +0000
@@ -8,7 +8,7 @@
 {% load cache %}
 {% block title %}Home{% endblock %}
 {% block custom_head %}
-<link rel="alternate" type="application/rss+xml" title="SurfGuitar101 News" href="{% url feeds-news url="news" %}" />
+<link rel="alternate" type="application/rss+xml" title="SurfGuitar101 News" href="{% url feeds-news %}" />
 {% endblock %}
 {% block custom_css %}
 <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/news.css" />
--- a/gpp/templates/news/base.html	Wed Mar 03 04:09:42 2010 +0000
+++ b/gpp/templates/news/base.html	Thu Mar 11 02:34:07 2010 +0000
@@ -1,13 +1,13 @@
 {% extends 'base.html' %}
 {% block custom_head %}
-<link rel="alternate" type="application/rss+xml" title="SurfGuitar101 News" href="{% url feeds-news url="news" %}" />
+<link rel="alternate" type="application/rss+xml" title="SurfGuitar101 News" href="{% url feeds-news %}" />
 {% endblock %}
 {% block custom_css %}
 <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/news.css" />
 {% block news_css %}{% endblock %}
 {% endblock %}
 {% block content %}
-<h2>SurfGuitar101 News &amp; Articles <a href="{% url feeds-news url="news" %}"><img src="{{ MEDIA_URL }}icons/feed.png" alt="News Feed" title="News Feed" /></a></h2>
+<h2>SurfGuitar101 News &amp; Articles <a href="{% url feeds-news %}"><img src="{{ MEDIA_URL }}icons/feed.png" alt="News Feed" title="News Feed" /></a></h2>
 {% if search_form %}
 <div class="news-search">
 <form action="{% url news-search_page page=1 %}" method="post">
--- a/gpp/templates/news/feed_description.html	Wed Mar 03 04:09:42 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-{{ obj.short_text|safe }}{{ obj.long_text|safe }}
--- a/gpp/templates/news/feed_title.html	Wed Mar 03 04:09:42 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-{{ obj.title|safe }}
--- a/gpp/urls.py	Wed Mar 03 04:09:42 2010 +0000
+++ b/gpp/urls.py	Thu Mar 11 02:34:07 2010 +0000
@@ -1,8 +1,6 @@
 from django.conf.urls.defaults import *
 from django.conf import settings
 from django.contrib import admin
-from django.views.decorators.cache import cache_page
-from django.contrib.syndication.views import feed as syndication_feed
 
 from news.feeds import LatestNewsFeed
 from forums.feeds import ForumsFeed
@@ -10,11 +8,6 @@
 
 admin.autodiscover()
 
-feeds = {
-   'news': LatestNewsFeed,
-   'forums': ForumsFeed,
-}
-
 urlpatterns = patterns('',
    url(r'^$', 'views.home', name='home'),
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
@@ -26,10 +19,16 @@
    (r'^core/', include('core.urls')),
    (r'^donations/', include('donations.urls')),
    (r'^downloads/', include('downloads.urls')),
-   url(r'^feeds/(?P<url>.*)/$',
-      cache_page(syndication_feed, 60 * 15),
-      {'feed_dict': feeds},
-      'feeds-news'),
+   url(r'^feeds/news/$',
+       LatestNewsFeed(),
+       name='feeds-news'),
+   url(r'^feeds/forums/$',
+       ForumsFeed(),
+       {'forum_slug': None},
+       'feeds-forum_combined'),
+   url(r'^feeds/forums/(?P<forum_slug>[\w\d-]+)/$',
+       ForumsFeed(),
+       name='feeds-forum'),
    (r'^forums/', include('forums.urls')),
    (r'^irc/', include('irc.urls')),
    (r'^links/', include('weblinks.urls')),