http.py 12 KB

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