Debian Codegraph @

tests/syndication_tests/feeds.py

raw
1 from functools import wraps
2
3 from django.contrib.syndication import views
4 from django.utils import feedgenerator
5 from django.utils.timezone import get_fixed_timezone
6
7 from .models import Article, Entry
8
9
10 def wraps_decorator(f):
11 @wraps(f)
12 def wrapper(*args, **kwargs):
13 value = f(*args, **kwargs)
14 return f"{value} -- decorated by @wraps."
15
16 return wrapper
17
18
19 def common_decorator(f):
20 def wrapper(*args, **kwargs):
21 value = f(*args, **kwargs)
22 return f"{value} -- common decorated."
23
24 return wrapper
25
26
27 class TestRss2Feed(views.Feed):
28 title = "My blog"
29 description = "A more thorough description of my blog."
30 link = "/blog/"
31 feed_guid = "/foo/bar/1234"
32 author_name = "Sally Smith"
33 author_email = "[email protected]"
34 author_link = "http://www.example.com/"
35 categories = ("python", "django")
36 feed_copyright = "Copyright (c) 2007, Sally Smith"
37 ttl = 600
38
39 def items(self):
40 return Entry.objects.all()
41
42 def item_description(self, item):
43 return "Overridden description: %s" % item
44
45 def item_pubdate(self, item):
46 return item.published
47
48 def item_updateddate(self, item):
49 return item.updated
50
51 def item_comments(self, item):
52 return "%scomments" % item.get_absolute_url()
53
54 item_author_name = "Sally Smith"
55 item_author_email = "[email protected]"
56 item_author_link = "http://www.example.com/"
57 item_categories = ("python", "testing")
58 item_copyright = "Copyright (c) 2007, Sally Smith"
59
60
61 class TestRss2FeedWithCallableObject(TestRss2Feed):
62 class TimeToLive:
63 def __call__(self):
64 return 700
65
66 ttl = TimeToLive()
67
68
69 class TestRss2FeedWithDecoratedMethod(TestRss2Feed):
70 class TimeToLive:
71 @wraps_decorator
72 def __call__(self):
73 return 800
74
75 @staticmethod
76 @wraps_decorator
77 def feed_copyright():
78 return "Copyright (c) 2022, John Doe"
79
80 ttl = TimeToLive()
81
82 @staticmethod
83 def categories():
84 return ("javascript", "vue")
85
86 @wraps_decorator
87 def title(self):
88 return "Overridden title"
89
90 @wraps_decorator
91 def item_title(self, item):
92 return f"Overridden item title: {item.title}"
93
94 @wraps_decorator
95 def description(self, obj):
96 return "Overridden description"
97
98 @wraps_decorator
99 def item_description(self):
100 return "Overridden item description"
101
102
103 class TestRss2FeedWithWrongDecoratedMethod(TestRss2Feed):
104 @common_decorator
105 def item_description(self, item):
106 return f"Overridden item description: {item.title}"
107
108
109 class TestRss2FeedWithGuidIsPermaLinkTrue(TestRss2Feed):
110 def item_guid_is_permalink(self, item):
111 return True
112
113
114 class TestRss2FeedWithGuidIsPermaLinkFalse(TestRss2Feed):
115 def item_guid(self, item):
116 return str(item.pk)
117
118 def item_guid_is_permalink(self, item):
119 return False
120
121
122 class TestRss091Feed(TestRss2Feed):
123 feed_type = feedgenerator.RssUserland091Feed
124
125
126 class TestNoPubdateFeed(views.Feed):
127 title = "Test feed"
128 link = "/feed/"
129
130 def items(self):
131 return Entry.objects.all()
132
133
134 class TestAtomFeed(TestRss2Feed):
135 feed_type = feedgenerator.Atom1Feed
136 subtitle = TestRss2Feed.description
137
138
139 class TestLatestFeed(TestRss2Feed):
140 """
141 A feed where the latest entry date is an `updated` element.
142 """
143
144 feed_type = feedgenerator.Atom1Feed
145 subtitle = TestRss2Feed.description
146
147 def items(self):
148 return Entry.objects.exclude(title="My last entry")
149
150
151 class ArticlesFeed(TestRss2Feed):
152 """
153 A feed to test no link being defined. Articles have no get_absolute_url()
154 method, and item_link() is not defined.
155 """
156
157 def items(self):
158 return Article.objects.all()
159
160
161 class TestSingleEnclosureRSSFeed(TestRss2Feed):
162 """
163 A feed to test that RSS feeds work with a single enclosure.
164 """
165
166 def item_enclosure_url(self, item):
167 return "http://example.com"
168
169 def item_enclosure_size(self, item):
170 return 0
171
172 def item_mime_type(self, item):
173 return "image/png"
174
175
176 class TestMultipleEnclosureRSSFeed(TestRss2Feed):
177 """
178 A feed to test that RSS feeds raise an exception with multiple enclosures.
179 """
180
181 def item_enclosures(self, item):
182 return [
183 feedgenerator.Enclosure("http://example.com/hello.png", 0, "image/png"),
184 feedgenerator.Enclosure("http://example.com/goodbye.png", 0, "image/png"),
185 ]
186
187
188 class TemplateFeed(TestRss2Feed):
189 """
190 A feed to test defining item titles and descriptions with templates.
191 """
192
193 title_template = "syndication/title.html"
194 description_template = "syndication/description.html"
195
196 # Defining a template overrides any item_title definition
197 def item_title(self):
198 return "Not in a template"
199
200
201 class TemplateContextFeed(TestRss2Feed):
202 """
203 A feed to test custom context data in templates for title or description.
204 """
205
206 title_template = "syndication/title_context.html"
207 description_template = "syndication/description_context.html"
208
209 def get_context_data(self, **kwargs):
210 context = super().get_context_data(**kwargs)
211 context["foo"] = "bar"
212 return context
213
214
215 class TestLanguageFeed(TestRss2Feed):
216 language = "de"
217
218
219 class TestGetObjectFeed(TestRss2Feed):
220 def get_object(self, request, entry_id):
221 return Entry.objects.get(pk=entry_id)
222
223 def items(self, obj):
224 return Article.objects.filter(entry=obj)
225
226 def item_link(self, item):
227 return "%sarticle/%s/" % (item.entry.get_absolute_url(), item.pk)
228
229 def item_comments(self, item):
230 return "%scomments" % self.item_link(item)
231
232 def item_description(self, item):
233 return "Article description: %s" % item.title
234
235 def item_title(self, item):
236 return "Title: %s" % item.title
237
238
239 class TestFeedWithStylesheets(TestRss2Feed):
240 stylesheets = [
241 "/stylesheet1.xsl",
242 feedgenerator.Stylesheet("/stylesheet2.xsl"),
243 ]
244
245
246 class NaiveDatesFeed(TestAtomFeed):
247 """
248 A feed with naive (non-timezone-aware) dates.
249 """
250
251 def item_pubdate(self, item):
252 return item.published
253
254
255 class TZAwareDatesFeed(TestAtomFeed):
256 """
257 A feed with timezone-aware dates.
258 """
259
260 def item_pubdate(self, item):
261 # Provide a weird offset so that the test can know it's getting this
262 # specific offset and not accidentally getting on from
263 # settings.TIME_ZONE.
264 return item.published.replace(tzinfo=get_fixed_timezone(42))
265
266
267 class TestFeedUrlFeed(TestAtomFeed):
268 feed_url = "http://example.com/customfeedurl/"
269
270
271 class MyCustomAtom1Feed(feedgenerator.Atom1Feed):
272 """
273 Test of a custom feed generator class.
274 """
275
276 def root_attributes(self):
277 attrs = super().root_attributes()
278 attrs["django"] = "rocks"
279 return attrs
280
281 def add_root_elements(self, handler):
282 super().add_root_elements(handler)
283 handler.addQuickElement("spam", "eggs")
284
285 def item_attributes(self, item):
286 attrs = super().item_attributes(item)
287 attrs["bacon"] = "yum"
288 return attrs
289
290 def add_item_elements(self, handler, item):
291 super().add_item_elements(handler, item)
292 handler.addQuickElement("ministry", "silly walks")
293
294
295 class TestCustomFeed(TestAtomFeed):
296 feed_type = MyCustomAtom1Feed
297
298
299 class TestSingleEnclosureAtomFeed(TestAtomFeed):
300 """
301 A feed to test that Atom feeds work with a single enclosure.
302 """
303
304 def item_enclosure_url(self, item):
305 return "http://example.com"
306
307 def item_enclosure_size(self, item):
308 return 0
309
310 def item_mime_type(self, item):
311 return "image/png"
312
313
314 class TestMultipleEnclosureAtomFeed(TestAtomFeed):
315 """
316 A feed to test that Atom feeds work with multiple enclosures.
317 """
318
319 def item_enclosures(self, item):
320 return [
321 feedgenerator.Enclosure("http://example.com/hello.png", "0", "image/png"),
322 feedgenerator.Enclosure("http://example.com/goodbye.png", "0", "image/png"),
323 ]