Code

Ticket #14202: patch2.diff

File patch2.diff, 2.4 KB (added by tmitchell, 3 years ago)

Patch incorporating code and tests, applies cleanly to trunk

Line 
1diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py
2index 5ec20f6..9269d82 100644
3--- a/django/utils/feedgenerator.py
4+++ b/django/utils/feedgenerator.py
5@@ -226,7 +226,9 @@ class RssFeed(SyndicationFeed):
6         handler.addQuickElement(u"title", self.feed['title'])
7         handler.addQuickElement(u"link", self.feed['link'])
8         handler.addQuickElement(u"description", self.feed['description'])
9-        handler.addQuickElement(u"atom:link", None, {u"rel": u"self", u"href": self.feed['feed_url']})
10+        if self.feed['feed_url'] is not None:
11+            handler.addQuickElement(u"atom:link", None, {u"rel": u"self",
12+                u"href": self.feed['feed_url']})
13         if self.feed['language'] is not None:
14             handler.addQuickElement(u"language", self.feed['language'])
15         for cat in self.feed['categories']:
16diff --git a/tests/regressiontests/utils/feedgenerator.py b/tests/regressiontests/utils/feedgenerator.py
17index c62bd2b..4d5063c 100644
18--- a/tests/regressiontests/utils/feedgenerator.py
19+++ b/tests/regressiontests/utils/feedgenerator.py
20@@ -60,3 +60,34 @@ class FeedgeneratorTest(unittest.TestCase):
21             "2008-11-14T13:37:00+02:00"
22         )
23 
24+
25+class Issue14202PatchTest(unittest.TestCase):
26+    """
27+    There is a bug in django.utils.feedgenerator.Rss201rev2Feed that has been
28+    reported in #14202 before.
29+
30+    http://code.djangoproject.com/ticket/14202
31+
32+    The feed_url property is optional but was required later on to write the
33+    atom:link attribute to the RSS-feed.
34+
35+    It is perfectly fine to leave the feed_url property as optional since it
36+    is not required by RSS (http://bit.ly/bAZpKT). Therefore the patch only
37+    renders the atom:link attribute if a feed_url is given.
38+    """
39+
40+    def test_feed_without_feed_url_gets_rendered_without_atom_link(self):
41+        feed = feedgenerator.Rss201rev2Feed('title', '/link/', 'descr')
42+        feed_content = feed.writeString('utf-8')
43+
44+        self.assertEquals(feed.feed['feed_url'], None)
45+        self.assertFalse('<atom:link href=' in feed_content)
46+
47+    def test_feed_with_feed_url_gets_rendered_with_atom_link(self):
48+        feed = feedgenerator.Rss201rev2Feed('title', '/link/', 'descr', feed_url='/feed/')
49+        feed_content = feed.writeString('utf-8')
50+
51+        self.assertEquals(feed.feed['feed_url'], '/feed/')
52+        self.assertTrue('<atom:link href="/feed/" rel="self"></atom:link>' in \
53+            feed_content)
54+