Code

Ticket #2762: 2007-11-24.diff

File 2007-11-24.diff, 5.8 KB (added by Jonathan Buchanan <jonathan.buchanan@…>, 7 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

Line 
1Index: django/utils/feedgenerator.py
2===================================================================
3--- django/utils/feedgenerator.py       (revision 4421)
4+++ django/utils/feedgenerator.py       (working copy)
5@@ -40,7 +40,7 @@
6     "Base class for all syndication feeds. Subclasses should provide write()"
7     def __init__(self, title, link, description, language=None, author_email=None,
8             author_name=None, author_link=None, subtitle=None, categories=None,
9-            feed_url=None):
10+            feed_url=None, feed_copyright=None):
11         self.feed = {
12             'title': title,
13             'link': link,
14@@ -52,12 +52,13 @@
15             'subtitle': subtitle,
16             'categories': categories or (),
17             'feed_url': feed_url,
18+            'feed_copyright': feed_copyright,
19         }
20         self.items = []
21 
22     def add_item(self, title, link, description, author_email=None,
23         author_name=None, author_link=None, pubdate=None, comments=None,
24-        unique_id=None, enclosure=None, categories=()):
25+        unique_id=None, enclosure=None, categories=(), item_copyright=None):
26         """
27         Adds an item to the feed. All args are expected to be Python Unicode
28         objects except pubdate, which is a datetime.datetime object, and
29@@ -75,6 +76,7 @@
30             'unique_id': unique_id,
31             'enclosure': enclosure,
32             'categories': categories or (),
33+            'item_copyright': item_copyright,
34         })
35 
36     def num_items(self):
37@@ -128,6 +130,8 @@
38             handler.addQuickElement(u"language", self.feed['language'])
39         for cat in self.feed['categories']:
40             handler.addQuickElement(u"category", cat)
41+        if self.feed['feed_copyright'] is not None:
42+            handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
43         self.write_items(handler)
44         self.endChannelElement(handler)
45         handler.endElement(u"rss")
46@@ -212,6 +216,8 @@
47             handler.addQuickElement(u"subtitle", self.feed['subtitle'])
48         for cat in self.feed['categories']:
49             handler.addQuickElement(u"category", "", {u"term": cat})
50+        if self.feed['feed_copyright'] is not None:
51+            handler.addQuickElement(u"rights", self.feed['feed_copyright'])
52         self.write_items(handler)
53         handler.endElement(u"feed")
54 
55@@ -252,10 +258,14 @@
56                      u"length": item['enclosure'].length,
57                      u"type": item['enclosure'].mime_type})
58 
59-            # Categories:
60+            # Categories.
61             for cat in item['categories']:
62                 handler.addQuickElement(u"category", u"", {u"term": cat})
63 
64+            # Rights.
65+            if item['item_copyright'] is not None:
66+                handler.addQuickElement(u"rights", item['item_copyright'])
67+
68             handler.endElement(u"entry")
69 
70 # This isolates the decision of what the system default is, so calling code can
71Index: django/contrib/syndication/feeds.py
72===================================================================
73--- django/contrib/syndication/feeds.py (revision 4421)
74+++ django/contrib/syndication/feeds.py (working copy)
75@@ -78,6 +78,7 @@
76             author_link = self.__get_dynamic_attr('author_link', obj),
77             author_email = self.__get_dynamic_attr('author_email', obj),
78             categories = self.__get_dynamic_attr('categories', obj),
79+            feed_copyright = self.__get_dynamic_attr('feed_copyright', obj),
80         )
81 
82         try:
83@@ -116,5 +117,6 @@
84                 author_email = author_email,
85                 author_link = author_link,
86                 categories = self.__get_dynamic_attr('item_categories', item),
87+                item_copyright = self.__get_dynamic_attr('item_copyright', item),
88             )
89         return feed
90Index: docs/syndication_feeds.txt
91===================================================================
92--- docs/syndication_feeds.txt  (revision 4421)
93+++ docs/syndication_feeds.txt  (working copy)
94@@ -127,7 +127,7 @@
95       it two template context variables:
96 
97           * ``{{ obj }}`` -- The current object (one of whichever objects you
98-            returned in ``items()``).
99+            returned in ``items()``).
100           * ``{{ site }}`` -- A ``django.models.core.sites.Site`` object
101             representing the current site. This is useful for
102             ``{{ site.domain }}`` or ``{{ site.name }}``.
103@@ -478,6 +478,22 @@
104 
105         categories = ("python", "django") # Hard-coded list of categories.
106 
107+        # COPYRIGHT NOTICE -- One of the following three is optional. The
108+        # framework looks for them in this order.
109+
110+        def copyright(self, obj):
111+            """
112+            Takes the object returned by get_object() and returns the feed's
113+            copyright notice as a normal Python string.
114+            """
115+
116+        def copyright(self):
117+            """
118+            Returns the feed's copyright notice as a normal Python string.
119+            """
120+
121+        copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
122+
123         # ITEMS -- One of the following three is required. The framework looks
124         # for them in this order.
125 
126@@ -659,7 +675,23 @@
127 
128         item_categories = ("python", "django") # Hard-coded categories.
129 
130+        # ITEM COPYRIGHT NOTICE -- One of the following three is optional. The
131+        # framework looks for them in this order.
132 
133+        def item_copyright(self, obj):
134+            """
135+            Takes an item, as returned by items(), and returns the item's
136+            copyright notice as a normal Python string.
137+            """
138+
139+        def item_copyright(self):
140+            """
141+            Returns the copyright notice for every item in the feed.
142+            """
143+
144+        item_copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
145+
146+
147 The low-level framework
148 =======================
149