Improved Atom feed-generating framework to output <link rel='self'>. Added a feed_url hook to feedgenerator for this purpose, and changed the syndication Feed and views to use it. Also updated docs.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1227 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-14 04:59:20 +00:00
parent 7b315b47aa
commit 7626cb0581
4 changed files with 28 additions and 6 deletions

View File

@ -17,8 +17,9 @@ class Feed:
item_enclosure_url = None item_enclosure_url = None
feed_type = feedgenerator.DefaultFeed feed_type = feedgenerator.DefaultFeed
def __init__(self, slug): def __init__(self, slug, feed_url):
self.slug = slug self.slug = slug
self.feed_url = feed_url
def item_link(self, item): def item_link(self, item):
try: try:
@ -56,7 +57,8 @@ class Feed:
title = self.__get_dynamic_attr('title', obj), title = self.__get_dynamic_attr('title', obj),
link = link, link = link,
description = self.__get_dynamic_attr('description', obj), description = self.__get_dynamic_attr('description', obj),
language = LANGUAGE_CODE.decode() language = LANGUAGE_CODE.decode(),
feed_url = add_domain(current_site, self.feed_url),
) )
try: try:

View File

@ -17,7 +17,7 @@ def feed(request, url, feed_dict=None):
raise Http404, "Slug %r isn't registered." % slug raise Http404, "Slug %r isn't registered." % slug
try: try:
feedgen = f(slug).get_feed(param) feedgen = f(slug, request.path).get_feed(param)
except feeds.FeedDoesNotExist: except feeds.FeedDoesNotExist:
raise Http404, "Invalid feed parameters. Slug %r is valid, but other parameters, or lack thereof, are not." % slug raise Http404, "Invalid feed parameters. Slug %r is valid, but other parameters, or lack thereof, are not." % slug

View File

@ -41,7 +41,8 @@ def get_tag_uri(url, date):
class SyndicationFeed: class SyndicationFeed:
"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):
self.feed = { self.feed = {
'title': title, 'title': title,
'link': link, 'link': link,
@ -52,6 +53,7 @@ class SyndicationFeed:
'author_link': author_link, 'author_link': author_link,
'subtitle': subtitle, 'subtitle': subtitle,
'categories': categories or (), 'categories': categories or (),
'feed_url': feed_url,
} }
self.items = [] self.items = []
@ -190,7 +192,9 @@ class Atom1Feed(SyndicationFeed):
else: else:
handler.startElement(u"feed", {u"xmlns": self.ns}) handler.startElement(u"feed", {u"xmlns": self.ns})
handler.addQuickElement(u"title", self.feed['title']) handler.addQuickElement(u"title", self.feed['title'])
handler.addQuickElement(u"link", "", {u"href": self.feed['link']}) handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']})
if self.feed['feed_url'] is not None:
handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']})
handler.addQuickElement(u"id", self.feed['link']) handler.addQuickElement(u"id", self.feed['link'])
handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('ascii')) handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('ascii'))
if self.feed['author_name'] is not None: if self.feed['author_name'] is not None:

View File

@ -266,6 +266,21 @@ comes directly from your `LANGUAGE_CODE setting`_.
.. _LANGUAGE_CODE setting: http://www.djangoproject.com/documentation/settings/#language-code .. _LANGUAGE_CODE setting: http://www.djangoproject.com/documentation/settings/#language-code
URLs
----
The ``link`` method/attribute can return either an absolute URL (e.g.
``"/blog/"``) or a URL with the fully-qualified domain and protocol (e.g.
``"http://www.example.com/blog/"``). If ``link`` doesn't return the domain,
the syndication framework will insert the domain of the current site, according
to your `SITE_ID setting`_.
Atom feeds require a ``<link rel="self">`` that defines the feed's current
location. The syndication framework populates this automatically, using the
domain of the current site according to the SITE_ID setting.
.. _SITE_ID setting: http://www.djangoproject.com/documentation/settings/#site-id
Publishing Atom and RSS feeds in tandem Publishing Atom and RSS feeds in tandem
--------------------------------------- ---------------------------------------
@ -501,7 +516,8 @@ Each of these three classes knows how to render a certain type of feed as XML.
They share this interface: They share this interface:
``__init__(title, link, description, language=None, author_email=None,`` ``__init__(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)``
Initializes the feed with the given metadata, which applies to the entire feed Initializes the feed with the given metadata, which applies to the entire feed
(i.e., not just to a specific item in the feed). (i.e., not just to a specific item in the feed).