Section 8.4. Easy RSS with FeedController

8.4. Easy RSS with FeedController

More and more applications are including RSS and Atom feeds so that updates can be monitored in people's RSS aggregator of choice. Because this need is expressed so often, TurboGears includes a FeedController class that makes it easy to create RSS feeds for your application. And WhatWhat Status takes advantage of the FeedController, which automatically produces valid RSS and Atom feeds when you pass in a properly structured dictionary, with a title, subtitle, author, link, ID (all of which are strings), and an entries list that contains elements for each entry in the feed.

Each item in the entry's list is required to have several fields (updated, title, summary, link, and so on), which are used to create the necessary RSS feeds. The actual construction of the feeds is handled by WhatWhatFeed's superclass, the FeedController, which has exposed rss2_0, atom0_3, and Atom1_0 methods that use their respective Kid templates to generate the feed. But, each of these three methods for the standard feed formats gets its data by calling the get_feed_data method. So, creating your own feeds is as simple as subclassing FeedController and overriding the get_feed_element:

 class WhatWhatFeed(FeedController):     entrys = []     def _feed_item(self, item, title, item_body):         entry= {}         entry["updated"] = item.creation_date         entry["title"] = title + " " +         entry["published"] = item.creation_date         entry["author"] = item.creator.displayName         entry["link"] = cherrypy.config.get('whatwhat.recentchangesurl')         entry["summary"] = textilize(item_body[:30])         self.entrys.append(entry)         entry = {}     def get_feed_data(self):         self.entrys = []         all_projects ="parent_project_id is NULL " +                        "order by upper(name)")         projects = [project for project in all_projects if not project.archived]         print self._timeframe()         recent_proj = getRecentChanges(projects, self._timeframe())         for proj in recent_proj:             self.entry = {}             if len(proj.recent_issues) > 0:                 for issue in proj.recent_issues:                     self._feed_item(issue, "Issue:", issue.issue)         if len(proj.recent_risks) > 0:              for risk in proj.recent_risks:                 self._feed_item(risk, "risk:", risk.description)         if len(proj.recent_notes) > 0:             for note in proj.recent_notes:                 self._feed_item(note, "note:", note.note)         if len(proj.recent_questions) > 0:             for question in proj.recent_questions:                 self._feed_item(question, "Question:", question.question)                 if len(question.answers) > 0:                     for answer in question.answers:                         entry = {}                         entry["updated"] = answer.creation_date                         entry["title"] = "Question:Answer %s:%s" % (                                           question. question[:30],                                                          entry["published"] = answer.creation_date                         entry["author"] = answer.creator.displayName                         entry["link"] = cherrypy.config.get('whatwhat.recentchangesurl')                         entry["summary"] = textilize(answer.answer[:30])                         self.entrys.append(entry)                         entry = {}     return dict(                 title="whatwhat recent changes",                 link="",                 author={ "name" : "Your Name Here",                          "email" : "" },                 ,                 subtitle="recent changes feed",                 entrys=self.entrys            ) def _timeframe(self):         if cherrypy.request.simpleCookie.has_key('time_frame_id'):             return int(cherrypy.request.simpleCookie['time_frame_id'].value)         return 24

For each of the key items in a project (issues, risks, notes, questions, and answers), the get_feed_data loops over each item and creates an entry dictionary. This entry has several required keys, which contain all the information needed to create the various feeds. The required keys are as follows:

  • updated Should contain a datetime object indicating when the element was last updated

  • title Should contain a string containing the feed item's title

  • published Should contain a datetime object indicating when the feed element was originally published

  • author Should contain a string with the author's name

  • link Should contain a link back to the canonical location of the feed element

  • summary Should contain a summary of the feed item's contents

With the exception of answers (which have a slightly different entry format) the creation of individual entries is done by the _feed_item() method, which simply appends each new entry to an overall entries list.

The WhatWhat Status feed controller is only complicated because several different types of objects need to be included in the feed. In general, creating your own feed is as simple as defining what goes in the entries you want to include in the feed and creating a list of entries. The FeedController takes care of everything else.

Rapid Web Applications with TurboGears(c) Using Python to Create Ajax-Powered Sites
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
ISBN: 0132433885
EAN: 2147483647
Year: 2006
Pages: 202

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: