wiki:FeedObject
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 8 (modified by jorge.vargas, 13 years ago) (diff)

--

Turbogears 0.9 will have support for rss/atom feeds. Here is a quicklook on how to use them. Any suggestions are welcome.

We assume here that we have blog that we want to add a feed to.

First we need to create the feed object. Then we add a get_feed_data method to it. It is this that will be called to populate the feed everytime a user requests it.

from turbogears.feed import FeedController
    class Feed(FeedController):
        def get_feed_data(self):
          

now lets start adding some things to the feed. We do this by returning a dict from get_feed_data with the elements we wish to have

        def get_feed_data(self):
            entrys = []

            return dict(
                title="my fine blog",
                link="http://blog.foo.org",
                author = {"name":"John Doe","email":"john@foo.org"},
                id = "http://blog.foo.org",
                subtitle="a blog about turbogears"
                entrys=entrys
            )

Now we need to get some elements for the posts in the feed so we extend our get_feed_data method for that.

Plese note that each foo dict is populated with the default for well form feeds but each entry object can be whatever you define, you can even populate it from diferent SQLObjects. So there is no need for you to know the definition of BlogEntry?.

        def get_feed_data(self):

            entrys = []
            # get all latest five blog entries in reversed order from SQLobject
            for entry in list(BlogEntry.select()[:5].reversed()):
                foo = {}
                foo["updated"] = entry.updated
                foo["title"] = entry.title
                foo["published"] = entry.published 
                foo["author"] = {"name":"elvelind grandin", "uri":"midvinter.org"}
                foo["summary"] = entry.post[:30]
                entrys.append(foo)

            return dict(
                title="my fine blog",
                link="http://blog.foo.org",
                author = {"name":"John Doe","email":"john@foo.org"},
                id = "http://blog.foo.org",
                subtitle="a blog about turbogears",
                entrys = entrys
            )

Now all we need is just to mount it in our controller

Class Blog(controllers.Root):

   feed = Feed()

   ....

now we have serveral urls to use. (assuming our blog is mounted as root)

  • blog.foo.org/feed/atom1.0
  • blog.foo.org/feed/atom0.3
  • blog.foo.org/feed/rss2.0
  • blog.foo.org/feed - which will redirect to the default feed (by standard atom1.0)

Note: At this point the feed has no knowledge of which elements are necessary to create valid feed. But this is something I expect to add in the near future.