Adding an RSS feed

14 June 2008 10:48

Adding an RSS feed to Grokstar was something which I'd put off for a while because I thought it would be tricky. In the end, it was surprisingly simple.

The main thing which had put me off in the past was the vast number of different RSS (and Atom) formats, many of which are slightly incompatible, not to mention imprecisely specified. But core RSS 2.0 is a good simple standard, supported by pretty much all RSS feed readers, so I went with that.

All I had to do was the create a view for the blog in rss.py:

import grok

from grokstar.blog import Blog

class RSS(grok.View):
    grok.context(Blog)
    grok.name('feed.rss')

    def items(self, max=10):
        return sorted(self.context['entries'].values(),
                      key=lambda obj:obj.updated,
                      reverse=True)[:max]

And create a template in rss_templates/rss.pt:

<?xml version="1.0"?>
<rss version="2.0" xmlns:tal="http://xml.zope.org/namespaces/tal">
 <channel>
   <title tal:content="context/title"/>
   <link tal:content="view/application_url"/>
   <description tal:content="context/tagline"/>
   <item tal:repeat="item view/items">
     <title tal:content="item/title"/>
     <link tal:content="item/@@absolute_url"/>
     <description tal:content="item/summary"/>
     <pubDate tal:content="item/updated"/>
   </item>
 </channel>
</rss>

And I was pretty much done. The only remaining change was to add a link in the main page macro so that browsers know I have an RSS feed, like this:

<link rel="alternate"
      type="application/rss+xml"
      tal:attributes="title python:view.application().title;
                      href string:${view/application_url}/feed.rss">

Leave a comment