Site Sections: Satchmo Main | Wiki | Demo Store |

Satchmo Project Gets RSS

I recently updated the blog software to include rss feeds. I had been delaying doing this because I've never really worked with rss before and didn't have a chunk of time to devote to figuring it out. Once I sat down, it truly was trivial to get it all setup. I did have to do a couple small tricks to get the tags functionality working the way I wanted to.

The 2 main feeds I wanted were a list of the most recent posts, as well as recent posts sorted by tag. The first is really straightforward while the later required a little more work.

First, I made the following changes to my blog's urls.py file:

from blog.feeds import LatestEntries, LatestEntriesByTag
feeds = {
  'latest': LatestEntries,
  'tags': LatestEntriesByTag,
  }

urlpatterns += patterns('django.contrib.syndication.views',
  (r'^feeds/(?P<url>.*)/$', 'feed', {'feed_dict': feeds}))

This allows me to setup two feed urls:

/blog/feeds/latest
/blog/feeds/tags/django

Now, I needed to create the feeds.py file.

The LatestEntries code is pretty simple:

from django.contrib.syndication.feeds import Feed
from blog.models import Entry
from tagging.models import Tag, TaggedItem

class LatestEntries(Feed):
    title = "Satchmoproject.com blog entries"
    link = "http://www.satchmoproject.com/blog/"
    description = "Updates on the latest postings at the satchmo project web site."

    def items(self):
        return Entry.objects.published().order_by('-pub_date')[:5]

This all works just fine. The challenge was figuring out how to create the tags model. Here's what I ended up with (heavily influenced by the django documentation):

class LatestEntriesByTag(Feed):

    def get_object(self, bits):
        # In case of "/rss/beats/0613/foo/bar/baz/", or other such clutter,
        # check that bits has only one member.
        if len(bits) != 1:
            raise ObjectDoesNotExist
        entry_tag = Tag.objects.get(name=bits[0])
        return TaggedItem.objects.get_by_model(Entry, entry_tag), bits[0]

    def title(self, obj):
        return "Latest satchmo project news by tag"

    def link(self, obj):
        return "http://www.satchmoproject.com/blog/tag/%s/" % obj[1]

    def description(self, obj):
        return "Satchmo items tagged with %s" % obj[1]

    def items(self, obj):
        return obj[0].order_by('-pub_date')[:5]

The only bit of trickiness is that I needed to keep track of which tag was being used. Since an object could have multiple tags, I had to pass that piece of information around separately. You'll notice that the last line get_object returns bits[0] which contains the tag. So, now everytime, the object is returned, a tuple containing the object and the tag are returned. Simple and easy.The final piece of this was setting up the feed templates for both of these. You can see the templates and all of the blog source code The only other thing I needed to do was update my site templates with links to the feeds.Now, I'm off to try to get this feed posted on the Django community page!

Posted on August 15, 2007 by chris django

0 Comments

Post a comment

Your name:

Comment: