Skip to content

Commit 86640f3

Browse files
committed
Fixed #3760 -- Added the ability to manually set feed- and item-level id
elements in Atom feeds. This is fully backwards compatible. Based on a patch from spark343@cs.ubc.ca. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5643 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 87d8976 commit 86640f3

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

django/contrib/syndication/feeds.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def get_feed(self, url=None):
8383
author_email = self.__get_dynamic_attr('author_email', obj),
8484
categories = self.__get_dynamic_attr('categories', obj),
8585
feed_copyright = self.__get_dynamic_attr('feed_copyright', obj),
86+
feed_guid = self.__get_dynamic_attr('feed_guid', obj),
8687
)
8788

8889
try:
@@ -114,7 +115,7 @@ def get_feed(self, url=None):
114115
title = title_tmp.render(Context({'obj': item, 'site': current_site})),
115116
link = link,
116117
description = description_tmp.render(Context({'obj': item, 'site': current_site})),
117-
unique_id = link,
118+
unique_id = self.__get_dynamic_attr('item_guid', item, link),
118119
enclosure = enc,
119120
pubdate = self.__get_dynamic_attr('item_pubdate', item),
120121
author_name = author_name,

django/utils/feedgenerator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SyndicationFeed(object):
4141
"Base class for all syndication feeds. Subclasses should provide write()"
4242
def __init__(self, title, link, description, language=None, author_email=None,
4343
author_name=None, author_link=None, subtitle=None, categories=None,
44-
feed_url=None, feed_copyright=None):
44+
feed_url=None, feed_copyright=None, feed_guid=None):
4545
to_unicode = lambda s: force_unicode(s, strings_only=True)
4646
if categories:
4747
categories = [force_unicode(c) for c in categories]
@@ -57,6 +57,7 @@ def __init__(self, title, link, description, language=None, author_email=None,
5757
'categories': categories or (),
5858
'feed_url': iri_to_uri(feed_url),
5959
'feed_copyright': to_unicode(feed_copyright),
60+
'id': feed_guid or link,
6061
}
6162
self.items = []
6263

@@ -213,7 +214,7 @@ def write(self, outfile, encoding):
213214
handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']})
214215
if self.feed['feed_url'] is not None:
215216
handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']})
216-
handler.addQuickElement(u"id", self.feed['link'])
217+
handler.addQuickElement(u"id", self.feed['id'])
217218
handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('ascii'))
218219
if self.feed['author_name'] is not None:
219220
handler.startElement(u"author", {})

docs/syndication_feeds.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,24 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas
416416

417417
link = '/foo/bar/' # Hard-coded link.
418418

419+
# GUID -- One of the following three is optional. The framework looks
420+
# for them in this order. This property is only used for Atom feeds
421+
# (where it is the feed-level id element). If not provided, the feed
422+
# link is used as the id.
423+
424+
def feed_guid(self, obj):
425+
"""
426+
Takes the object returned by get_object() and returns the globally
427+
unique id for the feed as a normal Python string.
428+
"""
429+
430+
def feed_guid(self):
431+
"""
432+
Returns the feed's globally unique id as a normal Python string.
433+
"""
434+
435+
feed_guid = '/foo/bar/1234' # Hard-coded guid.
436+
419437
# DESCRIPTION -- One of the following three is required. The framework
420438
# looks for them in this order.
421439

@@ -556,6 +574,15 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas
556574
Returns the URL for every item in the feed.
557575
"""
558576

577+
# ITEM_GUID -- The following method is optional. This property is
578+
# only used for Atom feeds (it is the id element for an item in an
579+
# Atom feed). If not provided, the item's link is used by default.
580+
581+
def item_guid(self, obj):
582+
"""
583+
Takes an item, as return by items(), and returns the item's id.
584+
"""
585+
559586
# ITEM AUTHOR NAME --One of the following three is optional. The
560587
# framework looks for them in this order.
561588

0 commit comments

Comments
 (0)