Debian Codegraph @

tests/sitemaps_tests/urls/http.py

raw
1 from datetime import date, datetime
2
3 from django.conf.urls.i18n import i18n_patterns
4 from django.contrib.sitemaps import GenericSitemap, Sitemap, views
5 from django.http import HttpResponse
6 from django.urls import path
7 from django.utils import timezone
8 from django.views.decorators.cache import cache_page
9
10 from ..models import I18nTestModel, TestModel
11
12
13 class SimpleSitemap(Sitemap):
14 changefreq = "never"
15 priority = 0.5
16 location = "/location/"
17 lastmod = date.today()
18
19 def items(self):
20 return [object()]
21
22
23 class SimplePagedSitemap(Sitemap):
24 lastmod = date.today()
25
26 def items(self):
27 return [object() for x in range(Sitemap.limit + 1)]
28
29
30 class SimpleI18nSitemap(Sitemap):
31 changefreq = "never"
32 priority = 0.5
33 i18n = True
34
35 def items(self):
36 return I18nTestModel.objects.order_by("pk").all()
37
38
39 class AlternatesI18nSitemap(SimpleI18nSitemap):
40 alternates = True
41
42
43 class LimitedI18nSitemap(AlternatesI18nSitemap):
44 languages = ["en", "es"]
45
46
47 class XDefaultI18nSitemap(AlternatesI18nSitemap):
48 x_default = True
49
50
51 class ItemByLangSitemap(SimpleI18nSitemap):
52 def get_languages_for_item(self, item):
53 if item.name == "Only for PT":
54 return ["pt"]
55 return super().get_languages_for_item(item)
56
57
58 class ItemByLangAlternatesSitemap(AlternatesI18nSitemap):
59 x_default = True
60
61 def get_languages_for_item(self, item):
62 if item.name == "Only for PT":
63 return ["pt"]
64 return super().get_languages_for_item(item)
65
66
67 class EmptySitemap(Sitemap):
68 changefreq = "never"
69 priority = 0.5
70 location = "/location/"
71
72
73 class FixedLastmodSitemap(SimpleSitemap):
74 lastmod = datetime(2013, 3, 13, 10, 0, 0)
75
76
77 class FixedLastmodMixedSitemap(Sitemap):
78 changefreq = "never"
79 priority = 0.5
80 location = "/location/"
81 loop = 0
82
83 def items(self):
84 o1 = TestModel()
85 o1.lastmod = datetime(2013, 3, 13, 10, 0, 0)
86 o2 = TestModel()
87 return [o1, o2]
88
89
90 class FixedNewerLastmodSitemap(SimpleSitemap):
91 lastmod = datetime(2013, 4, 20, 5, 0, 0)
92
93
94 class DateSiteMap(SimpleSitemap):
95 lastmod = date(2013, 3, 13)
96
97
98 class TimezoneSiteMap(SimpleSitemap):
99 lastmod = datetime(2013, 3, 13, 10, 0, 0, tzinfo=timezone.get_fixed_timezone(-300))
100
101
102 class CallableLastmodPartialSitemap(Sitemap):
103 """Not all items have `lastmod`."""
104
105 location = "/location/"
106
107 def items(self):
108 o1 = TestModel()
109 o1.lastmod = datetime(2013, 3, 13, 10, 0, 0)
110 o2 = TestModel()
111 return [o1, o2]
112
113 def lastmod(self, obj):
114 return obj.lastmod
115
116
117 class CallableLastmodFullSitemap(Sitemap):
118 """All items have `lastmod`."""
119
120 location = "/location/"
121
122 def items(self):
123 o1 = TestModel()
124 o1.lastmod = datetime(2013, 3, 13, 10, 0, 0)
125 o2 = TestModel()
126 o2.lastmod = datetime(2014, 3, 13, 10, 0, 0)
127 return [o1, o2]
128
129 def lastmod(self, obj):
130 return obj.lastmod
131
132
133 class CallableLastmodNoItemsSitemap(Sitemap):
134 location = "/location/"
135
136 def items(self):
137 return []
138
139 def lastmod(self, obj):
140 return obj.lastmod
141
142
143 class GetLatestLastmodNoneSiteMap(Sitemap):
144 changefreq = "never"
145 priority = 0.5
146 location = "/location/"
147
148 def items(self):
149 return [object()]
150
151 def lastmod(self, obj):
152 return datetime(2013, 3, 13, 10, 0, 0)
153
154 def get_latest_lastmod(self):
155 return None
156
157
158 class GetLatestLastmodSiteMap(SimpleSitemap):
159 def get_latest_lastmod(self):
160 return datetime(2013, 3, 13, 10, 0, 0)
161
162
163 def testmodelview(request, id):
164 return HttpResponse()
165
166
167 simple_sitemaps = {
168 "simple": SimpleSitemap,
169 }
170
171 simple_i18n_sitemaps = {
172 "i18n": SimpleI18nSitemap,
173 }
174
175 alternates_i18n_sitemaps = {
176 "i18n-alternates": AlternatesI18nSitemap,
177 }
178
179 limited_i18n_sitemaps = {
180 "i18n-limited": LimitedI18nSitemap,
181 }
182
183 xdefault_i18n_sitemaps = {
184 "i18n-xdefault": XDefaultI18nSitemap,
185 }
186
187 item_by_lang_i18n_sitemaps = {
188 "i18n-item-by-lang": ItemByLangSitemap,
189 }
190
191 item_by_lang_alternates_i18n_sitemaps = {
192 "i18n-item-by-lang-alternates": ItemByLangAlternatesSitemap,
193 }
194
195 simple_sitemaps_not_callable = {
196 "simple": SimpleSitemap(),
197 }
198
199 simple_sitemaps_paged = {
200 "simple": SimplePagedSitemap,
201 }
202
203 empty_sitemaps = {
204 "empty": EmptySitemap,
205 }
206
207 fixed_lastmod_sitemaps = {
208 "fixed-lastmod": FixedLastmodSitemap,
209 }
210
211 fixed_lastmod_mixed_sitemaps = {
212 "fixed-lastmod-mixed": FixedLastmodMixedSitemap,
213 }
214
215 sitemaps_lastmod_mixed_ascending = {
216 "no-lastmod": EmptySitemap,
217 "lastmod": FixedLastmodSitemap,
218 }
219
220 sitemaps_lastmod_mixed_descending = {
221 "lastmod": FixedLastmodSitemap,
222 "no-lastmod": EmptySitemap,
223 }
224
225 sitemaps_lastmod_ascending = {
226 "date": DateSiteMap,
227 "datetime": FixedLastmodSitemap,
228 "datetime-newer": FixedNewerLastmodSitemap,
229 }
230
231 sitemaps_lastmod_descending = {
232 "datetime-newer": FixedNewerLastmodSitemap,
233 "datetime": FixedLastmodSitemap,
234 "date": DateSiteMap,
235 }
236
237 generic_sitemaps = {
238 "generic": GenericSitemap({"queryset": TestModel.objects.order_by("pk").all()}),
239 }
240
241 get_latest_lastmod_none_sitemaps = {
242 "get-latest-lastmod-none": GetLatestLastmodNoneSiteMap,
243 }
244
245 get_latest_lastmod_sitemaps = {
246 "get-latest-lastmod": GetLatestLastmodSiteMap,
247 }
248
249 latest_lastmod_timezone_sitemaps = {
250 "latest-lastmod-timezone": TimezoneSiteMap,
251 }
252
253 generic_sitemaps_lastmod = {
254 "generic": GenericSitemap(
255 {
256 "queryset": TestModel.objects.order_by("pk").all(),
257 "date_field": "lastmod",
258 }
259 ),
260 }
261
262 callable_lastmod_partial_sitemap = {
263 "callable-lastmod": CallableLastmodPartialSitemap,
264 }
265
266 callable_lastmod_full_sitemap = {
267 "callable-lastmod": CallableLastmodFullSitemap,
268 }
269
270 callable_lastmod_no_items_sitemap = {
271 "callable-lastmod": CallableLastmodNoItemsSitemap,
272 }
273
274 urlpatterns = [
275 path("simple/index.xml", views.index, {"sitemaps": simple_sitemaps}),
276 path("simple-paged/index.xml", views.index, {"sitemaps": simple_sitemaps_paged}),
277 path(
278 "simple-not-callable/index.xml",
279 views.index,
280 {"sitemaps": simple_sitemaps_not_callable},
281 ),
282 path(
283 "simple/custom-lastmod-index.xml",
284 views.index,
285 {
286 "sitemaps": simple_sitemaps,
287 "template_name": "custom_sitemap_lastmod_index.xml",
288 },
289 ),
290 path(
291 "simple/sitemap-<section>.xml",
292 views.sitemap,
293 {"sitemaps": simple_sitemaps},
294 name="django.contrib.sitemaps.views.sitemap",
295 ),
296 path(
297 "simple/sitemap.xml",
298 views.sitemap,
299 {"sitemaps": simple_sitemaps},
300 name="django.contrib.sitemaps.views.sitemap",
301 ),
302 path(
303 "simple/i18n.xml",
304 views.sitemap,
305 {"sitemaps": simple_i18n_sitemaps},
306 name="django.contrib.sitemaps.views.sitemap",
307 ),
308 path(
309 "alternates/i18n.xml",
310 views.sitemap,
311 {"sitemaps": alternates_i18n_sitemaps},
312 name="django.contrib.sitemaps.views.sitemap",
313 ),
314 path(
315 "limited/i18n.xml",
316 views.sitemap,
317 {"sitemaps": limited_i18n_sitemaps},
318 name="django.contrib.sitemaps.views.sitemap",
319 ),
320 path(
321 "x-default/i18n.xml",
322 views.sitemap,
323 {"sitemaps": xdefault_i18n_sitemaps},
324 name="django.contrib.sitemaps.views.sitemap",
325 ),
326 path(
327 "simple/custom-sitemap.xml",
328 views.sitemap,
329 {"sitemaps": simple_sitemaps, "template_name": "custom_sitemap.xml"},
330 name="django.contrib.sitemaps.views.sitemap",
331 ),
332 path(
333 "empty/sitemap.xml",
334 views.sitemap,
335 {"sitemaps": empty_sitemaps},
336 name="django.contrib.sitemaps.views.sitemap",
337 ),
338 path(
339 "lastmod/sitemap.xml",
340 views.sitemap,
341 {"sitemaps": fixed_lastmod_sitemaps},
342 name="django.contrib.sitemaps.views.sitemap",
343 ),
344 path(
345 "lastmod-mixed/sitemap.xml",
346 views.sitemap,
347 {"sitemaps": fixed_lastmod_mixed_sitemaps},
348 name="django.contrib.sitemaps.views.sitemap",
349 ),
350 path(
351 "lastmod/date-sitemap.xml",
352 views.sitemap,
353 {"sitemaps": {"date-sitemap": DateSiteMap}},
354 name="django.contrib.sitemaps.views.sitemap",
355 ),
356 path(
357 "lastmod/tz-sitemap.xml",
358 views.sitemap,
359 {"sitemaps": {"tz-sitemap": TimezoneSiteMap}},
360 name="django.contrib.sitemaps.views.sitemap",
361 ),
362 path(
363 "lastmod-sitemaps/mixed-ascending.xml",
364 views.sitemap,
365 {"sitemaps": sitemaps_lastmod_mixed_ascending},
366 name="django.contrib.sitemaps.views.sitemap",
367 ),
368 path(
369 "lastmod-sitemaps/mixed-descending.xml",
370 views.sitemap,
371 {"sitemaps": sitemaps_lastmod_mixed_descending},
372 name="django.contrib.sitemaps.views.sitemap",
373 ),
374 path(
375 "lastmod-sitemaps/ascending.xml",
376 views.sitemap,
377 {"sitemaps": sitemaps_lastmod_ascending},
378 name="django.contrib.sitemaps.views.sitemap",
379 ),
380 path(
381 "item-by-lang/i18n.xml",
382 views.sitemap,
383 {"sitemaps": item_by_lang_i18n_sitemaps},
384 name="django.contrib.sitemaps.views.sitemap",
385 ),
386 path(
387 "item-by-lang-alternates/i18n.xml",
388 views.sitemap,
389 {"sitemaps": item_by_lang_alternates_i18n_sitemaps},
390 name="django.contrib.sitemaps.views.sitemap",
391 ),
392 path(
393 "lastmod-sitemaps/descending.xml",
394 views.sitemap,
395 {"sitemaps": sitemaps_lastmod_descending},
396 name="django.contrib.sitemaps.views.sitemap",
397 ),
398 path(
399 "lastmod/get-latest-lastmod-none-sitemap.xml",
400 views.index,
401 {"sitemaps": get_latest_lastmod_none_sitemaps},
402 name="django.contrib.sitemaps.views.index",
403 ),
404 path(
405 "lastmod/get-latest-lastmod-sitemap.xml",
406 views.index,
407 {"sitemaps": get_latest_lastmod_sitemaps},
408 name="django.contrib.sitemaps.views.index",
409 ),
410 path(
411 "lastmod/latest-lastmod-timezone-sitemap.xml",
412 views.index,
413 {"sitemaps": latest_lastmod_timezone_sitemaps},
414 name="django.contrib.sitemaps.views.index",
415 ),
416 path(
417 "generic/sitemap.xml",
418 views.sitemap,
419 {"sitemaps": generic_sitemaps},
420 name="django.contrib.sitemaps.views.sitemap",
421 ),
422 path(
423 "generic-lastmod/sitemap.xml",
424 views.sitemap,
425 {"sitemaps": generic_sitemaps_lastmod},
426 name="django.contrib.sitemaps.views.sitemap",
427 ),
428 path(
429 "cached/index.xml",
430 cache_page(1)(views.index),
431 {"sitemaps": simple_sitemaps, "sitemap_url_name": "cached_sitemap"},
432 ),
433 path(
434 "cached/sitemap-<section>.xml",
435 cache_page(1)(views.sitemap),
436 {"sitemaps": simple_sitemaps},
437 name="cached_sitemap",
438 ),
439 path(
440 "sitemap-without-entries/sitemap.xml",
441 views.sitemap,
442 {"sitemaps": {}},
443 name="django.contrib.sitemaps.views.sitemap",
444 ),
445 path(
446 "callable-lastmod-partial/index.xml",
447 views.index,
448 {"sitemaps": callable_lastmod_partial_sitemap},
449 ),
450 path(
451 "callable-lastmod-partial/sitemap.xml",
452 views.sitemap,
453 {"sitemaps": callable_lastmod_partial_sitemap},
454 ),
455 path(
456 "callable-lastmod-full/index.xml",
457 views.index,
458 {"sitemaps": callable_lastmod_full_sitemap},
459 ),
460 path(
461 "callable-lastmod-full/sitemap.xml",
462 views.sitemap,
463 {"sitemaps": callable_lastmod_full_sitemap},
464 ),
465 path(
466 "callable-lastmod-no-items/index.xml",
467 views.index,
468 {"sitemaps": callable_lastmod_no_items_sitemap},
469 ),
470 path(
471 "generic-lastmod/index.xml",
472 views.index,
473 {"sitemaps": generic_sitemaps_lastmod},
474 name="django.contrib.sitemaps.views.index",
475 ),
476 ]
477
478 urlpatterns += i18n_patterns(
479 path("i18n/testmodel/<int:id>/", testmodelview, name="i18n_testmodel"),
480 )