Fixed #2762 -- added copyright element support to RSS and Atom feeds. Patch

from Jonathan Buchanan.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4478 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-02-10 08:36:39 +00:00
parent fefcbbfe37
commit 937f3190ba
4 changed files with 51 additions and 5 deletions

View File

@ -44,6 +44,7 @@ answer newbie questions, and generally made Django that much better:
adurdin@gmail.com adurdin@gmail.com
akaihola akaihola
Andreas Andreas
andy@jadedplanet.net
ant9000@netwise.it ant9000@netwise.it
David Ascher <http://ascher.ca/> David Ascher <http://ascher.ca/>
Arthur <avandorp@gmail.com> Arthur <avandorp@gmail.com>
@ -55,7 +56,7 @@ answer newbie questions, and generally made Django that much better:
Paul Bissex <http://e-scribe.com/> Paul Bissex <http://e-scribe.com/>
Simon Blanchard Simon Blanchard
Andrew Brehaut <http://brehaut.net/blog> Andrew Brehaut <http://brehaut.net/blog>
andy@jadedplanet.net Jonathan Buchanan <jonathan.buchanan@gmail.com>
Antonio Cavedoni <http://cavedoni.com/> Antonio Cavedoni <http://cavedoni.com/>
C8E C8E
Chris Chamberlin <dja@cdc.msbx.net> Chris Chamberlin <dja@cdc.msbx.net>

View File

@ -78,6 +78,7 @@ class Feed(object):
author_link = self.__get_dynamic_attr('author_link', obj), author_link = self.__get_dynamic_attr('author_link', obj),
author_email = self.__get_dynamic_attr('author_email', obj), author_email = self.__get_dynamic_attr('author_email', obj),
categories = self.__get_dynamic_attr('categories', obj), categories = self.__get_dynamic_attr('categories', obj),
feed_copyright = self.__get_dynamic_attr('feed_copyright', obj),
) )
try: try:
@ -116,5 +117,6 @@ class Feed(object):
author_email = author_email, author_email = author_email,
author_link = author_link, author_link = author_link,
categories = self.__get_dynamic_attr('item_categories', item), categories = self.__get_dynamic_attr('item_categories', item),
item_copyright = self.__get_dynamic_attr('item_copyright', item),
) )
return feed return feed

View File

@ -40,7 +40,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_url=None, feed_copyright=None):
self.feed = { self.feed = {
'title': title, 'title': title,
'link': link, 'link': link,
@ -52,12 +52,13 @@ class SyndicationFeed(object):
'subtitle': subtitle, 'subtitle': subtitle,
'categories': categories or (), 'categories': categories or (),
'feed_url': feed_url, 'feed_url': feed_url,
'feed_copyright': feed_copyright,
} }
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=()): unique_id=None, enclosure=None, categories=(), item_copyright=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
@ -75,6 +76,7 @@ class SyndicationFeed(object):
'unique_id': unique_id, 'unique_id': unique_id,
'enclosure': enclosure, 'enclosure': enclosure,
'categories': categories or (), 'categories': categories or (),
'item_copyright': item_copyright,
}) })
def num_items(self): def num_items(self):
@ -128,6 +130,8 @@ class RssFeed(SyndicationFeed):
handler.addQuickElement(u"language", self.feed['language']) handler.addQuickElement(u"language", self.feed['language'])
for cat in self.feed['categories']: for cat in self.feed['categories']:
handler.addQuickElement(u"category", cat) handler.addQuickElement(u"category", cat)
if self.feed['feed_copyright'] is not None:
handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
self.write_items(handler) self.write_items(handler)
self.endChannelElement(handler) self.endChannelElement(handler)
handler.endElement(u"rss") handler.endElement(u"rss")
@ -212,6 +216,8 @@ class Atom1Feed(SyndicationFeed):
handler.addQuickElement(u"subtitle", self.feed['subtitle']) handler.addQuickElement(u"subtitle", self.feed['subtitle'])
for cat in self.feed['categories']: for cat in self.feed['categories']:
handler.addQuickElement(u"category", "", {u"term": cat}) handler.addQuickElement(u"category", "", {u"term": cat})
if self.feed['feed_copyright'] is not None:
handler.addQuickElement(u"rights", self.feed['feed_copyright'])
self.write_items(handler) self.write_items(handler)
handler.endElement(u"feed") handler.endElement(u"feed")
@ -252,10 +258,14 @@ class Atom1Feed(SyndicationFeed):
u"length": item['enclosure'].length, u"length": item['enclosure'].length,
u"type": item['enclosure'].mime_type}) u"type": item['enclosure'].mime_type})
# Categories: # Categories.
for cat in item['categories']: for cat in item['categories']:
handler.addQuickElement(u"category", u"", {u"term": cat}) handler.addQuickElement(u"category", u"", {u"term": cat})
# Rights.
if item['item_copyright'] is not None:
handler.addQuickElement(u"rights", item['item_copyright'])
handler.endElement(u"entry") handler.endElement(u"entry")
# This isolates the decision of what the system default is, so calling code can # This isolates the decision of what the system default is, so calling code can

View File

@ -478,6 +478,22 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas
categories = ("python", "django") # Hard-coded list of categories. categories = ("python", "django") # Hard-coded list of categories.
# COPYRIGHT NOTICE -- One of the following three is optional. The
# framework looks for them in this order.
def copyright(self, obj):
"""
Takes the object returned by get_object() and returns the feed's
copyright notice as a normal Python string.
"""
def copyright(self):
"""
Returns the feed's copyright notice as a normal Python string.
"""
copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
# 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.
@ -659,6 +675,23 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas
item_categories = ("python", "django") # Hard-coded categories. item_categories = ("python", "django") # Hard-coded categories.
# ITEM COPYRIGHT NOTICE (only applicable to Atom feeds) -- One of the
# following three is optional. The framework looks for them in this
# order.
def item_copyright(self, obj):
"""
Takes an item, as returned by items(), and returns the item's
copyright notice as a normal Python string.
"""
def item_copyright(self):
"""
Returns the copyright notice for every item in the feed.
"""
item_copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
The low-level framework The low-level framework
======================= =======================