07Jun

Adding Well-Formed-Web (wfw) Comments to Django’s RSS feeds

Posted by Elf Sternberg as Uncategorized

Django has just about the most awesome syndication system on the planet.  It’s bog stupid simple, and if you want to implement it, the django documentation is great.  But those of us who use and love RSS appreciate the ease and speed with which the WFW standard allows us to syndicate comments as well, and this feature is not, sadly, well implemented in Django.

The authors of Django have rejected an attempt to put WFW into the Django feed collection, which I think is a shame, but the patch for it is still available.

Soeren Sonnenburg’s patch didn’t quite work in Liferea, but one fix made it so.  The biggest problem, however, is that he doesn’t tell you how to use it.  I’m going to show you how, if you have a collection of objects (in this case, “Story” objects) with django’s standard Comment’s (from contrib), how to integrate the two into a syndicated comment engine.

First, download his file and put it somewhere in your project’s feeds app, preferably with a new name like wfw_feeds.py.

Change this line:

handler.startElement(u"rss", {u"version": self._version,
    u"xmlns:wfw": u"http://wellformedweb.org/CommentAPI/"})

To this:

handler.startElement(u"rss", {u"version": self._version,
    u"xmlns:wfw": u"http://wellformedweb.org/CommentAPI/",
    u"xmlns:dc": "http://purl.org/dc/elements/1.1/"})

Adding the Dublin Core seems to be critical to making some RSS readers parse the WFW stuff correctly.

Now that you’ve got that, you must change or add the extra arguments method of your Feed object:

def item_extra_kwargs(self, item):
    return { 'wfw_commentRss': 'http://localhost/feeds/storycomments/%s' % item.slug}

I know, it seems odd to have hand-coded the URL there, but unfortunately the syndication.views does not inform reverse() at all.

Now you must create the Feed.

class StoryComments(Feed):
    feed_type = WellFormedWebRss
    title = "Story Comments"
    link = '/storycomments/'

    def get_object(self, nslug):
        if not nslug:
            raise Http404
        return get_object_or_404(Story, slug=nslug[0])

    def items(self, obj):
        return Comment.objects.filter(
            content_type = ContentType.objects.get_for_model(Story),
            object_pk = obj.id).order_by('-submit_date')

Now you’ve provided a new feed, so add that to your feeds objects:

feeds = { 'newstories': NewStories,  'storycomments': StoryComments }

And that’s it. (Obviously, you’ll have to import all the appropriate tools, such as get_object_or_404 and so on, but I assume you know how to do that if you’re up to the point of wanting to add RSS feeds with comments to your application.)

Comment Form

Subscribe to Feed

Categories

Calendar

June 2010
M T W T F S S
« May   Jul »
 123456
78910111213
14151617181920
21222324252627
282930