Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 7626cb058107ac1ecb0795162e4c9fc4f92356dd 1 parent 7b315b4
Adrian Holovaty authored November 14, 2005
6  django/contrib/syndication/feeds.py
@@ -17,8 +17,9 @@ class Feed:
17 17
     item_enclosure_url = None
18 18
     feed_type = feedgenerator.DefaultFeed
19 19
 
20  
-    def __init__(self, slug):
  20
+    def __init__(self, slug, feed_url):
21 21
         self.slug = slug
  22
+        self.feed_url = feed_url
22 23
 
23 24
     def item_link(self, item):
24 25
         try:
@@ -56,7 +57,8 @@ def get_feed(self, url=None):
56 57
             title = self.__get_dynamic_attr('title', obj),
57 58
             link = link,
58 59
             description = self.__get_dynamic_attr('description', obj),
59  
-            language = LANGUAGE_CODE.decode()
  60
+            language = LANGUAGE_CODE.decode(),
  61
+            feed_url = add_domain(current_site, self.feed_url),
60 62
         )
61 63
 
62 64
         try:
2  django/contrib/syndication/views.py
@@ -17,7 +17,7 @@ def feed(request, url, feed_dict=None):
17 17
         raise Http404, "Slug %r isn't registered." % slug
18 18
 
19 19
     try:
20  
-        feedgen = f(slug).get_feed(param)
  20
+        feedgen = f(slug, request.path).get_feed(param)
21 21
     except feeds.FeedDoesNotExist:
22 22
         raise Http404, "Invalid feed parameters. Slug %r is valid, but other parameters, or lack thereof, are not." % slug
23 23
 
8  django/utils/feedgenerator.py
@@ -41,7 +41,8 @@ def get_tag_uri(url, date):
41 41
 class SyndicationFeed:
42 42
     "Base class for all syndication feeds. Subclasses should provide write()"
43 43
     def __init__(self, title, link, description, language=None, author_email=None,
44  
-            author_name=None, author_link=None, subtitle=None, categories=None):
  44
+            author_name=None, author_link=None, subtitle=None, categories=None,
  45
+            feed_url=None):
45 46
         self.feed = {
46 47
             'title': title,
47 48
             'link': link,
@@ -52,6 +53,7 @@ def __init__(self, title, link, description, language=None, author_email=None,
52 53
             'author_link': author_link,
53 54
             'subtitle': subtitle,
54 55
             'categories': categories or (),
  56
+            'feed_url': feed_url,
55 57
         }
56 58
         self.items = []
57 59
 
@@ -190,7 +192,9 @@ def write(self, outfile, encoding):
190 192
         else:
191 193
             handler.startElement(u"feed", {u"xmlns": self.ns})
192 194
         handler.addQuickElement(u"title", self.feed['title'])
193  
-        handler.addQuickElement(u"link", "", {u"href": self.feed['link']})
  195
+        handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']})
  196
+        if self.feed['feed_url'] is not None:
  197
+            handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']})
194 198
         handler.addQuickElement(u"id", self.feed['link'])
195 199
         handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('ascii'))
196 200
         if self.feed['author_name'] is not None:
18  docs/syndication_feeds.txt
@@ -266,6 +266,21 @@ comes directly from your `LANGUAGE_CODE setting`_.
266 266
 
267 267
 .. _LANGUAGE_CODE setting: http://www.djangoproject.com/documentation/settings/#language-code
268 268
 
  269
+URLs
  270
+----
  271
+
  272
+The ``link`` method/attribute can return either an absolute URL (e.g.
  273
+``"/blog/"``) or a URL with the fully-qualified domain and protocol (e.g.
  274
+``"http://www.example.com/blog/"``). If ``link`` doesn't return the domain,
  275
+the syndication framework will insert the domain of the current site, according
  276
+to your `SITE_ID setting`_.
  277
+
  278
+Atom feeds require a ``<link rel="self">`` that defines the feed's current
  279
+location. The syndication framework populates this automatically, using the
  280
+domain of the current site according to the SITE_ID setting.
  281
+
  282
+.. _SITE_ID setting: http://www.djangoproject.com/documentation/settings/#site-id
  283
+
269 284
 Publishing Atom and RSS feeds in tandem
270 285
 ---------------------------------------
271 286
 
@@ -501,7 +516,8 @@ Each of these three classes knows how to render a certain type of feed as XML.
501 516
 They share this interface:
502 517
 
503 518
 ``__init__(title, link, description, language=None, author_email=None,``
504  
-``author_name=None, author_link=None, subtitle=None, categories=None)``
  519
+``author_name=None, author_link=None, subtitle=None, categories=None,``
  520
+``feed_url=None)``
505 521
 
506 522
 Initializes the feed with the given metadata, which applies to the entire feed
507 523
 (i.e., not just to a specific item in the feed).

0 notes on commit 7626cb0

Please sign in to comment.
Something went wrong with that request. Please try again.