Kaydet (Commit) 8a3cf46e authored tarafından Malcolm Tredinnick's avatar Malcolm Tredinnick

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
üst 18e1f56a
...@@ -170,6 +170,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -170,6 +170,7 @@ answer newbie questions, and generally made Django that much better:
Nagy Károly <charlie@rendszergazda.com> Nagy Károly <charlie@rendszergazda.com>
Ben Dean Kawamura <ben.dean.kawamura@gmail.com> Ben Dean Kawamura <ben.dean.kawamura@gmail.com>
Ian G. Kelly <ian.g.kelly@gmail.com> Ian G. Kelly <ian.g.kelly@gmail.com>
Thomas Kerpe <thomas@kerpe.net>
Ben Khoo <khoobks@westnet.com.au> Ben Khoo <khoobks@westnet.com.au>
Garth Kidd <http://www.deadlybloodyserious.com/> Garth Kidd <http://www.deadlybloodyserious.com/>
kilian <kilian.cavalotti@lip6.fr> kilian <kilian.cavalotti@lip6.fr>
...@@ -273,6 +274,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -273,6 +274,7 @@ answer newbie questions, and generally made Django that much better:
serbaut@gmail.com serbaut@gmail.com
John Shaffer <jshaffer2112@gmail.com> John Shaffer <jshaffer2112@gmail.com>
Pete Shinners <pete@shinners.org> Pete Shinners <pete@shinners.org>
jason.sidabras@gmail.com
Jozko Skrablin <jozko.skrablin@gmail.com> Jozko Skrablin <jozko.skrablin@gmail.com>
SmileyChris <smileychris@gmail.com> SmileyChris <smileychris@gmail.com>
smurf@smurf.noris.de smurf@smurf.noris.de
......
...@@ -89,6 +89,7 @@ class Feed(object): ...@@ -89,6 +89,7 @@ class Feed(object):
categories = self.__get_dynamic_attr('categories', obj), categories = self.__get_dynamic_attr('categories', obj),
feed_copyright = self.__get_dynamic_attr('feed_copyright', obj), feed_copyright = self.__get_dynamic_attr('feed_copyright', obj),
feed_guid = self.__get_dynamic_attr('feed_guid', obj), feed_guid = self.__get_dynamic_attr('feed_guid', obj),
ttl = self.__get_dynamic_attr('ttl', obj),
) )
try: try:
......
...@@ -45,7 +45,7 @@ class SyndicationFeed(object): ...@@ -45,7 +45,7 @@ class SyndicationFeed(object):
"Base class for all syndication feeds. Subclasses should provide write()" "Base class for all syndication feeds. Subclasses should provide write()"
def __init__(self, title, link, description, language=None, author_email=None, def __init__(self, title, link, description, language=None, author_email=None,
author_name=None, author_link=None, subtitle=None, categories=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) to_unicode = lambda s: force_unicode(s, strings_only=True)
if categories: if categories:
categories = [force_unicode(c) for c in categories] categories = [force_unicode(c) for c in categories]
...@@ -62,12 +62,13 @@ class SyndicationFeed(object): ...@@ -62,12 +62,13 @@ class SyndicationFeed(object):
'feed_url': iri_to_uri(feed_url), 'feed_url': iri_to_uri(feed_url),
'feed_copyright': to_unicode(feed_copyright), 'feed_copyright': to_unicode(feed_copyright),
'id': feed_guid or link, 'id': feed_guid or link,
'ttl': ttl,
} }
self.items = [] self.items = []
def add_item(self, title, link, description, author_email=None, def add_item(self, title, link, description, author_email=None,
author_name=None, author_link=None, pubdate=None, comments=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 Adds an item to the feed. All args are expected to be Python Unicode
objects except pubdate, which is a datetime.datetime object, and objects except pubdate, which is a datetime.datetime object, and
...@@ -89,6 +90,7 @@ class SyndicationFeed(object): ...@@ -89,6 +90,7 @@ class SyndicationFeed(object):
'enclosure': enclosure, 'enclosure': enclosure,
'categories': categories or (), 'categories': categories or (),
'item_copyright': to_unicode(item_copyright), 'item_copyright': to_unicode(item_copyright),
'ttl': ttl,
}) })
def num_items(self): def num_items(self):
...@@ -146,6 +148,8 @@ class RssFeed(SyndicationFeed): ...@@ -146,6 +148,8 @@ class RssFeed(SyndicationFeed):
if self.feed['feed_copyright'] is not None: if self.feed['feed_copyright'] is not None:
handler.addQuickElement(u"copyright", self.feed['feed_copyright']) handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
handler.addQuickElement(u"lastBuildDate", rfc2822_date(self.latest_post_date()).decode('ascii')) 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.write_items(handler)
self.endChannelElement(handler) self.endChannelElement(handler)
handler.endElement(u"rss") handler.endElement(u"rss")
...@@ -190,6 +194,8 @@ class Rss201rev2Feed(RssFeed): ...@@ -190,6 +194,8 @@ class Rss201rev2Feed(RssFeed):
handler.addQuickElement(u"comments", item['comments']) handler.addQuickElement(u"comments", item['comments'])
if item['unique_id'] is not None: if item['unique_id'] is not None:
handler.addQuickElement(u"guid", item['unique_id']) handler.addQuickElement(u"guid", item['unique_id'])
if item['ttl'] is not None:
handler.addQuickElement(u"ttl", item['ttl'])
# Enclosure. # Enclosure.
if item['enclosure'] is not None: if item['enclosure'] is not None:
......
...@@ -547,6 +547,22 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas ...@@ -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. 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 # ITEMS -- One of the following three is required. The framework looks
# for them in this order. # for them in this order.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment