From 8a3cf46e6021e9e9b5640c3f79625564527aa165 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 20 Oct 2007 14:54:38 +0000 Subject: [PATCH] Fixed #3502 -- Added TTL support for RSS (not Atom) feeds. Patch from jason.sidabras@gmail.com and Thomas Kerpe. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6570 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 2 ++ django/contrib/syndication/feeds.py | 1 + django/utils/feedgenerator.py | 10 ++++++++-- docs/syndication_feeds.txt | 16 ++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index c11e274c37..c25b7ee327 100644 --- a/AUTHORS +++ b/AUTHORS @@ -170,6 +170,7 @@ answer newbie questions, and generally made Django that much better: Nagy Károly Ben Dean Kawamura Ian G. Kelly + Thomas Kerpe Ben Khoo Garth Kidd kilian @@ -273,6 +274,7 @@ answer newbie questions, and generally made Django that much better: serbaut@gmail.com John Shaffer Pete Shinners + jason.sidabras@gmail.com Jozko Skrablin SmileyChris smurf@smurf.noris.de diff --git a/django/contrib/syndication/feeds.py b/django/contrib/syndication/feeds.py index eb5a9f0607..57d092a5b5 100644 --- a/django/contrib/syndication/feeds.py +++ b/django/contrib/syndication/feeds.py @@ -89,6 +89,7 @@ class Feed(object): categories = self.__get_dynamic_attr('categories', obj), feed_copyright = self.__get_dynamic_attr('feed_copyright', obj), feed_guid = self.__get_dynamic_attr('feed_guid', obj), + ttl = self.__get_dynamic_attr('ttl', obj), ) try: diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index e296331324..3e0826c968 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -45,7 +45,7 @@ class SyndicationFeed(object): "Base class for all syndication feeds. Subclasses should provide write()" def __init__(self, title, link, description, language=None, author_email=None, author_name=None, author_link=None, subtitle=None, categories=None, - feed_url=None, feed_copyright=None, feed_guid=None): + feed_url=None, feed_copyright=None, feed_guid=None, ttl=None): to_unicode = lambda s: force_unicode(s, strings_only=True) if categories: categories = [force_unicode(c) for c in categories] @@ -62,12 +62,13 @@ class SyndicationFeed(object): 'feed_url': iri_to_uri(feed_url), 'feed_copyright': to_unicode(feed_copyright), 'id': feed_guid or link, + 'ttl': ttl, } self.items = [] def add_item(self, title, link, description, author_email=None, author_name=None, author_link=None, pubdate=None, comments=None, - unique_id=None, enclosure=None, categories=(), item_copyright=None): + unique_id=None, enclosure=None, categories=(), item_copyright=None, ttl=None): """ Adds an item to the feed. All args are expected to be Python Unicode objects except pubdate, which is a datetime.datetime object, and @@ -89,6 +90,7 @@ class SyndicationFeed(object): 'enclosure': enclosure, 'categories': categories or (), 'item_copyright': to_unicode(item_copyright), + 'ttl': ttl, }) def num_items(self): @@ -146,6 +148,8 @@ class RssFeed(SyndicationFeed): if self.feed['feed_copyright'] is not None: handler.addQuickElement(u"copyright", self.feed['feed_copyright']) handler.addQuickElement(u"lastBuildDate", rfc2822_date(self.latest_post_date()).decode('ascii')) + if self.feed['ttl'] is not None: + handler.addQuickElement(u"ttl", self.feed['ttl']) self.write_items(handler) self.endChannelElement(handler) handler.endElement(u"rss") @@ -190,6 +194,8 @@ class Rss201rev2Feed(RssFeed): handler.addQuickElement(u"comments", item['comments']) if item['unique_id'] is not None: handler.addQuickElement(u"guid", item['unique_id']) + if item['ttl'] is not None: + handler.addQuickElement(u"ttl", item['ttl']) # Enclosure. if item['enclosure'] is not None: diff --git a/docs/syndication_feeds.txt b/docs/syndication_feeds.txt index 393572f3e2..30943591b8 100644 --- a/docs/syndication_feeds.txt +++ b/docs/syndication_feeds.txt @@ -547,6 +547,22 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice. + # TTL -- One of the following three is optional. The framework looks + # for them in this order. Ignored for Atom feeds. + + def ttl(self, obj): + """ + Takes the object returned by get_object() and returns the feed's + TTL (Time to live) as a normal Python string. + """ + + def ttl(self): + """ + Returns the feed's ttl as a normal Python string. + """ + + ttl = 600 # Hard-coded Time to live. + # ITEMS -- One of the following three is required. The framework looks # for them in this order.