Ticket #12978: 12978.diff

File 12978.diff, 5.3 KB (added by Yuval Adam, 14 years ago)

Updated proposed patch, with tests and docs

  • django/utils/feedgenerator.py

    ### Eclipse Workspace Patch 1.0
    #P django-trunk
     
    7171    "Base class for all syndication feeds. Subclasses should provide write()"
    7272    def __init__(self, title, link, description, language=None, author_email=None,
    7373            author_name=None, author_link=None, subtitle=None, categories=None,
    74             feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs):
     74            feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, stylesheets=None, **kwargs):
    7575        to_unicode = lambda s: force_unicode(s, strings_only=True)
    7676        if categories:
    7777            categories = [force_unicode(c) for c in categories]
    7878        if ttl is not None:
    7979            # Force ints to unicode
    8080            ttl = force_unicode(ttl)
     81        if stylesheets:
     82            stylesheets = [iri_to_uri(s) for s in stylesheets]
    8183        self.feed = {
    8284            'title': to_unicode(title),
    8385            'link': iri_to_uri(link),
     
    9294            'feed_copyright': to_unicode(feed_copyright),
    9395            'id': feed_guid or link,
    9496            'ttl': ttl,
     97            'stylesheets': stylesheets or (),
    9598        }
    9699        self.feed.update(kwargs)
    97100        self.items = []
     
    198201    def write(self, outfile, encoding):
    199202        handler = SimplerXMLGenerator(outfile, encoding)
    200203        handler.startDocument()
     204        for s in self.feed['stylesheets']:
     205            handler.processingInstruction('xml-stylesheet', 'type="text/css" href="%s"' % s) # http://www.w3.org/TR/xml-stylesheet/
    201206        handler.startElement(u"rss", self.rss_attributes())
    202207        handler.startElement(u"channel", self.root_attributes())
    203208        self.add_root_elements(handler)
  • docs/ref/contrib/syndication.txt

     
    286286location. The syndication framework populates this automatically, using the
    287287domain of the current site according to the :setting:`SITE_ID` setting.
    288288
     289Stylesheets
     290-----------
     291
     292RSS feeds support the inclusion of stylesheets via the :attr:`stylesheets`
     293attribute, as a sequence of stylesheets to be added to the feed.
     294
    289295Publishing Atom and RSS feeds in tandem
    290296---------------------------------------
    291297
     
    532538            """
    533539
    534540        ttl = 600 # Hard-coded Time To Live.
     541       
     542        stylesheets = ('http://www.example.com/example.css',) # Hard-coded stylesheets to be added to feed
    535543
    536544        # ITEMS -- One of the following three is required. The framework looks
    537545        # for them in this order.
     
    820828        * ``feed_copyright``
    821829        * ``feed_guid``
    822830        * ``ttl``
     831        * ``stylesheets``
    823832
    824833    Any extra keyword arguments you pass to ``__init__`` will be stored in
    825834    ``self.feed`` for use with `custom feed generators`_.
    826835
    827     All parameters should be Unicode objects, except ``categories``, which
     836    All parameters should be Unicode objects, except ``categories`` and ``stylesheets``, which
    828837    should be a sequence of Unicode objects.
    829838
    830839.. method:: SyndicationFeed.add_item(**kwargs)
  • tests/regressiontests/syndication/tests.py

     
    5050        self.assertEqual(len(feed_elem), 1)
    5151        feed = feed_elem[0]
    5252        self.assertEqual(feed.getAttribute('version'), '2.0')
    53 
     53       
     54        # Verify document has a stylesheet element (processing instruction <?xml-stylesheet ... ?>)
     55        self.assertEqual(doc.firstChild.target, 'xml-stylesheet')
     56        self.assertEqual(doc.firstChild.data, 'type="text/css" href="http://www.example.com/example.css"')
     57       
    5458        # Making sure there's only one `channel` element w/in the
    5559        # `rss` element.
    5660        chan_elem = feed.getElementsByTagName('channel')
  • django/contrib/syndication/views.py

     
    114114            feed_copyright = self.__get_dynamic_attr('feed_copyright', obj),
    115115            feed_guid = self.__get_dynamic_attr('feed_guid', obj),
    116116            ttl = self.__get_dynamic_attr('ttl', obj),
     117            stylesheets = self.__get_dynamic_attr('stylesheets', obj),
    117118            **self.feed_extra_kwargs(obj)
    118119        )
    119120
  • tests/regressiontests/syndication/feeds.py

     
    2222    categories = ('python', 'django')
    2323    feed_copyright = 'Copyright (c) 2007, Sally Smith'
    2424    ttl = 600
    25 
     25    stylesheets = ('http://www.example.com/example.css',)   
     26   
    2627    def items(self):
    2728        return Entry.objects.all()
    2829
Back to Top