Ticket #2762: 2007-11-24.diff

File 2007-11-24.diff, 5.8 KB (added by Jonathan Buchanan <jonathan.buchanan@…>, 18 years ago)

Adds a feed_copyright attribute for feeds and an item_copyright attribute for feed items (Atom allows a different rights attribute for each item); also contains documentation for these additions

  • django/utils/feedgenerator.py

     
    4040    "Base class for all syndication feeds. Subclasses should provide write()"
    4141    def __init__(self, title, link, description, language=None, author_email=None,
    4242            author_name=None, author_link=None, subtitle=None, categories=None,
    43             feed_url=None):
     43            feed_url=None, feed_copyright=None):
    4444        self.feed = {
    4545            'title': title,
    4646            'link': link,
     
    5252            'subtitle': subtitle,
    5353            'categories': categories or (),
    5454            'feed_url': feed_url,
     55            'feed_copyright': feed_copyright,
    5556        }
    5657        self.items = []
    5758
    5859    def add_item(self, title, link, description, author_email=None,
    5960        author_name=None, author_link=None, pubdate=None, comments=None,
    60         unique_id=None, enclosure=None, categories=()):
     61        unique_id=None, enclosure=None, categories=(), item_copyright=None):
    6162        """
    6263        Adds an item to the feed. All args are expected to be Python Unicode
    6364        objects except pubdate, which is a datetime.datetime object, and
     
    7576            'unique_id': unique_id,
    7677            'enclosure': enclosure,
    7778            'categories': categories or (),
     79            'item_copyright': item_copyright,
    7880        })
    7981
    8082    def num_items(self):
     
    128130            handler.addQuickElement(u"language", self.feed['language'])
    129131        for cat in self.feed['categories']:
    130132            handler.addQuickElement(u"category", cat)
     133        if self.feed['feed_copyright'] is not None:
     134            handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
    131135        self.write_items(handler)
    132136        self.endChannelElement(handler)
    133137        handler.endElement(u"rss")
     
    212216            handler.addQuickElement(u"subtitle", self.feed['subtitle'])
    213217        for cat in self.feed['categories']:
    214218            handler.addQuickElement(u"category", "", {u"term": cat})
     219        if self.feed['feed_copyright'] is not None:
     220            handler.addQuickElement(u"rights", self.feed['feed_copyright'])
    215221        self.write_items(handler)
    216222        handler.endElement(u"feed")
    217223
     
    252258                     u"length": item['enclosure'].length,
    253259                     u"type": item['enclosure'].mime_type})
    254260
    255             # Categories:
     261            # Categories.
    256262            for cat in item['categories']:
    257263                handler.addQuickElement(u"category", u"", {u"term": cat})
    258264
     265            # Rights.
     266            if item['item_copyright'] is not None:
     267                handler.addQuickElement(u"rights", item['item_copyright'])
     268
    259269            handler.endElement(u"entry")
    260270
    261271# This isolates the decision of what the system default is, so calling code can
  • django/contrib/syndication/feeds.py

     
    7878            author_link = self.__get_dynamic_attr('author_link', obj),
    7979            author_email = self.__get_dynamic_attr('author_email', obj),
    8080            categories = self.__get_dynamic_attr('categories', obj),
     81            feed_copyright = self.__get_dynamic_attr('feed_copyright', obj),
    8182        )
    8283
    8384        try:
     
    116117                author_email = author_email,
    117118                author_link = author_link,
    118119                categories = self.__get_dynamic_attr('item_categories', item),
     120                item_copyright = self.__get_dynamic_attr('item_copyright', item),
    119121            )
    120122        return feed
  • docs/syndication_feeds.txt

     
    127127      it two template context variables:
    128128
    129129          * ``{{ obj }}`` -- The current object (one of whichever objects you
    130             returned in ``items()``). 
     130            returned in ``items()``).
    131131          * ``{{ site }}`` -- A ``django.models.core.sites.Site`` object
    132132            representing the current site. This is useful for
    133133            ``{{ site.domain }}`` or ``{{ site.name }}``.
     
    478478
    479479        categories = ("python", "django") # Hard-coded list of categories.
    480480
     481        # COPYRIGHT NOTICE -- One of the following three is optional. The
     482        # framework looks for them in this order.
     483
     484        def copyright(self, obj):
     485            """
     486            Takes the object returned by get_object() and returns the feed's
     487            copyright notice as a normal Python string.
     488            """
     489
     490        def copyright(self):
     491            """
     492            Returns the feed's copyright notice as a normal Python string.
     493            """
     494
     495        copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
     496
    481497        # ITEMS -- One of the following three is required. The framework looks
    482498        # for them in this order.
    483499
     
    659675
    660676        item_categories = ("python", "django") # Hard-coded categories.
    661677
     678        # ITEM COPYRIGHT NOTICE -- One of the following three is optional. The
     679        # framework looks for them in this order.
    662680
     681        def item_copyright(self, obj):
     682            """
     683            Takes an item, as returned by items(), and returns the item's
     684            copyright notice as a normal Python string.
     685            """
     686
     687        def item_copyright(self):
     688            """
     689            Returns the copyright notice for every item in the feed.
     690            """
     691
     692        item_copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
     693
     694
    663695The low-level framework
    664696=======================
    665697
Back to Top