Code

Ticket #8253: feed_root_attributes.diff

File feed_root_attributes.diff, 2.8 KB (added by jbronn, 6 years ago)

Allows customization of RSS element attributes (w/tests); re-enables XMLNS suport for Atom feeds.

Line 
1Index: django/utils/feedgenerator.py
2===================================================================
3--- django/utils/feedgenerator.py       (revision 8328)
4+++ django/utils/feedgenerator.py       (working copy)
5@@ -178,13 +178,16 @@
6     def write(self, outfile, encoding):
7         handler = SimplerXMLGenerator(outfile, encoding)
8         handler.startDocument()
9-        handler.startElement(u"rss", {u"version": self._version})
10+        handler.startElement(u"rss", self.rss_attributes())
11         handler.startElement(u"channel", self.root_attributes())
12         self.add_root_elements(handler)
13         self.write_items(handler)
14         self.endChannelElement(handler)
15         handler.endElement(u"rss")
16 
17+    def rss_attributes(self):
18+        return {u"version": self._version}
19+
20     def write_items(self, handler):
21         for item in self.items:
22             handler.startElement(u'item', self.item_attributes(item))
23@@ -266,7 +269,7 @@
24         self.write_items(handler)
25         handler.endElement(u"feed")
26 
27-    def root_element_attributes(self):
28+    def root_attributes(self):
29         if self.feed['language'] is not None:
30             return {u"xmlns": self.ns, u"xml:lang": self.feed['language']}
31         else:
32Index: tests/regressiontests/syndication/tests.py
33===================================================================
34--- tests/regressiontests/syndication/tests.py  (revision 8328)
35+++ tests/regressiontests/syndication/tests.py  (working copy)
36@@ -20,9 +20,19 @@
37     def test_rss_feed(self):
38         response = self.client.get('/syndication/feeds/rss/')
39         doc = minidom.parseString(response.content)
40-        self.assertEqual(len(doc.getElementsByTagName('channel')), 1)
41-
42-        chan = doc.getElementsByTagName('channel')[0]
43+       
44+        # Making sure there's only 1 `rss` element and that the correct
45+        # RSS version was specified.
46+        feed_elem = doc.getElementsByTagName('rss')
47+        self.assertEqual(len(feed_elem), 1)
48+        feed = feed_elem[0]
49+        self.assertEqual(feed.getAttribute('version'), '2.0')
50+       
51+        # Making sure there's only one `channel` element w/in the
52+        # `rss` element.
53+        chan_elem = feed.getElementsByTagName('channel')
54+        self.assertEqual(len(chan_elem), 1)
55+        chan = chan_elem[0]
56         self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item'])
57     
58         items = chan.getElementsByTagName('item')
59@@ -36,6 +46,7 @@
60         
61         feed = doc.firstChild
62         self.assertEqual(feed.nodeName, 'feed')
63+        self.assertEqual(feed.getAttribute('xmlns'), 'http://www.w3.org/2005/Atom')
64         self.assertChildNodes(feed, ['title', 'link', 'id', 'updated', 'entry'])       
65         
66         entries = feed.getElementsByTagName('entry')