From 937f3190baabb5c98398b391bcf8250bbb738da5 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 10 Feb 2007 08:36:39 +0000 Subject: [PATCH] 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 --- AUTHORS | 3 ++- django/contrib/syndication/feeds.py | 2 ++ django/utils/feedgenerator.py | 16 ++++++++++--- docs/syndication_feeds.txt | 35 ++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index 4538512c06..4bee17e6e8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -44,6 +44,7 @@ answer newbie questions, and generally made Django that much better: adurdin@gmail.com akaihola Andreas + andy@jadedplanet.net ant9000@netwise.it David Ascher Arthur @@ -55,7 +56,7 @@ answer newbie questions, and generally made Django that much better: Paul Bissex Simon Blanchard Andrew Brehaut - andy@jadedplanet.net + Jonathan Buchanan Antonio Cavedoni C8E Chris Chamberlin diff --git a/django/contrib/syndication/feeds.py b/django/contrib/syndication/feeds.py index 119615a0b9..cdb4e8170f 100644 --- a/django/contrib/syndication/feeds.py +++ b/django/contrib/syndication/feeds.py @@ -78,6 +78,7 @@ class Feed(object): author_link = self.__get_dynamic_attr('author_link', obj), author_email = self.__get_dynamic_attr('author_email', obj), categories = self.__get_dynamic_attr('categories', obj), + feed_copyright = self.__get_dynamic_attr('feed_copyright', obj), ) try: @@ -116,5 +117,6 @@ class Feed(object): author_email = author_email, author_link = author_link, categories = self.__get_dynamic_attr('item_categories', item), + item_copyright = self.__get_dynamic_attr('item_copyright', item), ) return feed diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index 2eb27a40b7..9397789d6a 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -40,7 +40,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_url=None, feed_copyright=None): self.feed = { 'title': title, 'link': link, @@ -52,12 +52,13 @@ class SyndicationFeed(object): 'subtitle': subtitle, 'categories': categories or (), 'feed_url': feed_url, + 'feed_copyright': feed_copyright, } 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=()): + unique_id=None, enclosure=None, categories=(), item_copyright=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 @@ -75,6 +76,7 @@ class SyndicationFeed(object): 'unique_id': unique_id, 'enclosure': enclosure, 'categories': categories or (), + 'item_copyright': item_copyright, }) def num_items(self): @@ -128,6 +130,8 @@ class RssFeed(SyndicationFeed): handler.addQuickElement(u"language", self.feed['language']) for cat in self.feed['categories']: 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.endChannelElement(handler) handler.endElement(u"rss") @@ -212,6 +216,8 @@ class Atom1Feed(SyndicationFeed): handler.addQuickElement(u"subtitle", self.feed['subtitle']) for cat in self.feed['categories']: 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) handler.endElement(u"feed") @@ -252,10 +258,14 @@ class Atom1Feed(SyndicationFeed): u"length": item['enclosure'].length, u"type": item['enclosure'].mime_type}) - # Categories: + # Categories. for cat in item['categories']: 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") # This isolates the decision of what the system default is, so calling code can diff --git a/docs/syndication_feeds.txt b/docs/syndication_feeds.txt index 72b05ff16a..a64914de3f 100644 --- a/docs/syndication_feeds.txt +++ b/docs/syndication_feeds.txt @@ -127,7 +127,7 @@ put into those elements. it two template context variables: * ``{{ obj }}`` -- The current object (one of whichever objects you - returned in ``items()``). + returned in ``items()``). * ``{{ site }}`` -- A ``django.models.core.sites.Site`` object representing the current site. This is useful for ``{{ site.domain }}`` or ``{{ site.name }}``. @@ -478,6 +478,22 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas 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 # 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 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 =======================