Ticket #8253: feed_root_attributes.diff

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

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

  • django/utils/feedgenerator.py

     
    178178    def write(self, outfile, encoding):
    179179        handler = SimplerXMLGenerator(outfile, encoding)
    180180        handler.startDocument()
    181         handler.startElement(u"rss", {u"version": self._version})
     181        handler.startElement(u"rss", self.rss_attributes())
    182182        handler.startElement(u"channel", self.root_attributes())
    183183        self.add_root_elements(handler)
    184184        self.write_items(handler)
    185185        self.endChannelElement(handler)
    186186        handler.endElement(u"rss")
    187187
     188    def rss_attributes(self):
     189        return {u"version": self._version}
     190
    188191    def write_items(self, handler):
    189192        for item in self.items:
    190193            handler.startElement(u'item', self.item_attributes(item))
     
    266269        self.write_items(handler)
    267270        handler.endElement(u"feed")
    268271
    269     def root_element_attributes(self):
     272    def root_attributes(self):
    270273        if self.feed['language'] is not None:
    271274            return {u"xmlns": self.ns, u"xml:lang": self.feed['language']}
    272275        else:
  • tests/regressiontests/syndication/tests.py

     
    2020    def test_rss_feed(self):
    2121        response = self.client.get('/syndication/feeds/rss/')
    2222        doc = minidom.parseString(response.content)
    23         self.assertEqual(len(doc.getElementsByTagName('channel')), 1)
    24 
    25         chan = doc.getElementsByTagName('channel')[0]
     23       
     24        # Making sure there's only 1 `rss` element and that the correct
     25        # RSS version was specified.
     26        feed_elem = doc.getElementsByTagName('rss')
     27        self.assertEqual(len(feed_elem), 1)
     28        feed = feed_elem[0]
     29        self.assertEqual(feed.getAttribute('version'), '2.0')
     30       
     31        # Making sure there's only one `channel` element w/in the
     32        # `rss` element.
     33        chan_elem = feed.getElementsByTagName('channel')
     34        self.assertEqual(len(chan_elem), 1)
     35        chan = chan_elem[0]
    2636        self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item'])
    2737   
    2838        items = chan.getElementsByTagName('item')
     
    3646       
    3747        feed = doc.firstChild
    3848        self.assertEqual(feed.nodeName, 'feed')
     49        self.assertEqual(feed.getAttribute('xmlns'), 'http://www.w3.org/2005/Atom')
    3950        self.assertChildNodes(feed, ['title', 'link', 'id', 'updated', 'entry'])       
    4051       
    4152        entries = feed.getElementsByTagName('entry')
Back to Top