http.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 EmptySitemap(Sitemap):
  33. changefreq = "never"
  34. priority = 0.5
  35. location = '/location/'
  36. class FixedLastmodSitemap(SimpleSitemap):
  37. lastmod = datetime(2013, 3, 13, 10, 0, 0)
  38. class FixedLastmodMixedSitemap(Sitemap):
  39. changefreq = "never"
  40. priority = 0.5
  41. location = '/location/'
  42. loop = 0
  43. def items(self):
  44. o1 = TestModel()
  45. o1.lastmod = datetime(2013, 3, 13, 10, 0, 0)
  46. o2 = TestModel()
  47. return [o1, o2]
  48. class FixedNewerLastmodSitemap(SimpleSitemap):
  49. lastmod = datetime(2013, 4, 20, 5, 0, 0)
  50. class DateSiteMap(SimpleSitemap):
  51. lastmod = date(2013, 3, 13)
  52. class TimezoneSiteMap(SimpleSitemap):
  53. lastmod = datetime(2013, 3, 13, 10, 0, 0, tzinfo=timezone.get_fixed_timezone(-300))
  54. class CallableLastmodPartialSitemap(Sitemap):
  55. """Not all items have `lastmod`."""
  56. location = '/location/'
  57. def items(self):
  58. o1 = TestModel()
  59. o1.lastmod = datetime(2013, 3, 13, 10, 0, 0)
  60. o2 = TestModel()
  61. return [o1, o2]
  62. def lastmod(self, obj):
  63. return obj.lastmod
  64. class CallableLastmodFullSitemap(Sitemap):
  65. """All items have `lastmod`."""
  66. location = '/location/'
  67. def items(self):
  68. o1 = TestModel()
  69. o1.lastmod = datetime(2013, 3, 13, 10, 0, 0)
  70. o2 = TestModel()
  71. o2.lastmod = datetime(2014, 3, 13, 10, 0, 0)
  72. return [o1, o2]
  73. def lastmod(self, obj):
  74. return obj.lastmod
  75. class GetLatestLastmodNoneSiteMap(Sitemap):
  76. changefreq = "never"
  77. priority = 0.5
  78. location = '/location/'
  79. def items(self):
  80. return [object()]
  81. def lastmod(self, obj):
  82. return datetime(2013, 3, 13, 10, 0, 0)
  83. def get_latest_lastmod(self):
  84. return None
  85. class GetLatestLastmodSiteMap(SimpleSitemap):
  86. def get_latest_lastmod(self):
  87. return datetime(2013, 3, 13, 10, 0, 0)
  88. def testmodelview(request, id):
  89. return HttpResponse()
  90. simple_sitemaps = {
  91. 'simple': SimpleSitemap,
  92. }
  93. simple_i18n_sitemaps = {
  94. 'i18n': SimpleI18nSitemap,
  95. }
  96. alternates_i18n_sitemaps = {
  97. 'i18n-alternates': AlternatesI18nSitemap,
  98. }
  99. limited_i18n_sitemaps = {
  100. 'i18n-limited': LimitedI18nSitemap,
  101. }
  102. xdefault_i18n_sitemaps = {
  103. 'i18n-xdefault': XDefaultI18nSitemap,
  104. }
  105. simple_sitemaps_not_callable = {
  106. 'simple': SimpleSitemap(),
  107. }
  108. simple_sitemaps_paged = {
  109. 'simple': SimplePagedSitemap,
  110. }
  111. empty_sitemaps = {
  112. 'empty': EmptySitemap,
  113. }
  114. fixed_lastmod_sitemaps = {
  115. 'fixed-lastmod': FixedLastmodSitemap,
  116. }
  117. fixed_lastmod_mixed_sitemaps = {
  118. 'fixed-lastmod-mixed': FixedLastmodMixedSitemap,
  119. }
  120. sitemaps_lastmod_mixed_ascending = {
  121. 'no-lastmod': EmptySitemap,
  122. 'lastmod': FixedLastmodSitemap,
  123. }
  124. sitemaps_lastmod_mixed_descending = {
  125. 'lastmod': FixedLastmodSitemap,
  126. 'no-lastmod': EmptySitemap,
  127. }
  128. sitemaps_lastmod_ascending = {
  129. 'date': DateSiteMap,
  130. 'datetime': FixedLastmodSitemap,
  131. 'datetime-newer': FixedNewerLastmodSitemap,
  132. }
  133. sitemaps_lastmod_descending = {
  134. 'datetime-newer': FixedNewerLastmodSitemap,
  135. 'datetime': FixedLastmodSitemap,
  136. 'date': DateSiteMap,
  137. }
  138. generic_sitemaps = {
  139. 'generic': GenericSitemap({'queryset': TestModel.objects.order_by('pk').all()}),
  140. }
  141. get_latest_lastmod_none_sitemaps = {
  142. 'get-latest-lastmod-none': GetLatestLastmodNoneSiteMap,
  143. }
  144. get_latest_lastmod_sitemaps = {
  145. 'get-latest-lastmod': GetLatestLastmodSiteMap,
  146. }
  147. latest_lastmod_timezone_sitemaps = {
  148. 'latest-lastmod-timezone': TimezoneSiteMap,
  149. }
  150. generic_sitemaps_lastmod = {
  151. 'generic': GenericSitemap({
  152. 'queryset': TestModel.objects.order_by('pk').all(),
  153. 'date_field': 'lastmod',
  154. }),
  155. }
  156. callable_lastmod_partial_sitemap = {
  157. 'callable-lastmod': CallableLastmodPartialSitemap,
  158. }
  159. callable_lastmod_full_sitemap = {
  160. 'callable-lastmod': CallableLastmodFullSitemap,
  161. }
  162. urlpatterns = [
  163. path('simple/index.xml', views.index, {'sitemaps': simple_sitemaps}),
  164. path('simple-paged/index.xml', views.index, {'sitemaps': simple_sitemaps_paged}),
  165. path('simple-not-callable/index.xml', views.index, {'sitemaps': simple_sitemaps_not_callable}),
  166. path(
  167. 'simple/custom-index.xml', views.index,
  168. {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
  169. path(
  170. 'simple/custom-lastmod-index.xml', views.index,
  171. {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_lastmod_index.xml'},
  172. ),
  173. path(
  174. 'simple/sitemap-<section>.xml', views.sitemap,
  175. {'sitemaps': simple_sitemaps},
  176. name='django.contrib.sitemaps.views.sitemap'),
  177. path(
  178. 'simple/sitemap.xml', views.sitemap,
  179. {'sitemaps': simple_sitemaps},
  180. name='django.contrib.sitemaps.views.sitemap'),
  181. path(
  182. 'simple/i18n.xml', views.sitemap,
  183. {'sitemaps': simple_i18n_sitemaps},
  184. name='django.contrib.sitemaps.views.sitemap'),
  185. path(
  186. 'alternates/i18n.xml', views.sitemap,
  187. {'sitemaps': alternates_i18n_sitemaps},
  188. name='django.contrib.sitemaps.views.sitemap'),
  189. path(
  190. 'limited/i18n.xml', views.sitemap,
  191. {'sitemaps': limited_i18n_sitemaps},
  192. name='django.contrib.sitemaps.views.sitemap'),
  193. path(
  194. 'x-default/i18n.xml', views.sitemap,
  195. {'sitemaps': xdefault_i18n_sitemaps},
  196. name='django.contrib.sitemaps.views.sitemap'),
  197. path(
  198. 'simple/custom-sitemap.xml', views.sitemap,
  199. {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'},
  200. name='django.contrib.sitemaps.views.sitemap'),
  201. path(
  202. 'empty/sitemap.xml', views.sitemap,
  203. {'sitemaps': empty_sitemaps},
  204. name='django.contrib.sitemaps.views.sitemap'),
  205. path(
  206. 'lastmod/sitemap.xml', views.sitemap,
  207. {'sitemaps': fixed_lastmod_sitemaps},
  208. name='django.contrib.sitemaps.views.sitemap'),
  209. path(
  210. 'lastmod-mixed/sitemap.xml', views.sitemap,
  211. {'sitemaps': fixed_lastmod_mixed_sitemaps},
  212. name='django.contrib.sitemaps.views.sitemap'),
  213. path(
  214. 'lastmod/date-sitemap.xml', views.sitemap,
  215. {'sitemaps': {'date-sitemap': DateSiteMap}},
  216. name='django.contrib.sitemaps.views.sitemap'),
  217. path(
  218. 'lastmod/tz-sitemap.xml', views.sitemap,
  219. {'sitemaps': {'tz-sitemap': TimezoneSiteMap}},
  220. name='django.contrib.sitemaps.views.sitemap'),
  221. path(
  222. 'lastmod-sitemaps/mixed-ascending.xml', views.sitemap,
  223. {'sitemaps': sitemaps_lastmod_mixed_ascending},
  224. name='django.contrib.sitemaps.views.sitemap'),
  225. path(
  226. 'lastmod-sitemaps/mixed-descending.xml', views.sitemap,
  227. {'sitemaps': sitemaps_lastmod_mixed_descending},
  228. name='django.contrib.sitemaps.views.sitemap'),
  229. path(
  230. 'lastmod-sitemaps/ascending.xml', views.sitemap,
  231. {'sitemaps': sitemaps_lastmod_ascending},
  232. name='django.contrib.sitemaps.views.sitemap'),
  233. path(
  234. 'lastmod-sitemaps/descending.xml', views.sitemap,
  235. {'sitemaps': sitemaps_lastmod_descending},
  236. name='django.contrib.sitemaps.views.sitemap'),
  237. path(
  238. 'lastmod/get-latest-lastmod-none-sitemap.xml', views.index,
  239. {'sitemaps': get_latest_lastmod_none_sitemaps},
  240. name='django.contrib.sitemaps.views.index',
  241. ),
  242. path(
  243. 'lastmod/get-latest-lastmod-sitemap.xml', views.index,
  244. {'sitemaps': get_latest_lastmod_sitemaps},
  245. name='django.contrib.sitemaps.views.index',
  246. ),
  247. path(
  248. 'lastmod/latest-lastmod-timezone-sitemap.xml', views.index,
  249. {'sitemaps': latest_lastmod_timezone_sitemaps},
  250. name='django.contrib.sitemaps.views.index',
  251. ),
  252. path(
  253. 'generic/sitemap.xml', views.sitemap,
  254. {'sitemaps': generic_sitemaps},
  255. name='django.contrib.sitemaps.views.sitemap'),
  256. path(
  257. 'generic-lastmod/sitemap.xml', views.sitemap,
  258. {'sitemaps': generic_sitemaps_lastmod},
  259. name='django.contrib.sitemaps.views.sitemap'),
  260. path(
  261. 'cached/index.xml', cache_page(1)(views.index),
  262. {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}),
  263. path(
  264. 'cached/sitemap-<section>.xml', cache_page(1)(views.sitemap),
  265. {'sitemaps': simple_sitemaps}, name='cached_sitemap'),
  266. path(
  267. 'sitemap-without-entries/sitemap.xml', views.sitemap,
  268. {'sitemaps': {}}, name='django.contrib.sitemaps.views.sitemap'),
  269. path('callable-lastmod-partial/index.xml', views.index, {'sitemaps': callable_lastmod_partial_sitemap}),
  270. path('callable-lastmod-partial/sitemap.xml', views.sitemap, {'sitemaps': callable_lastmod_partial_sitemap}),
  271. path('callable-lastmod-full/index.xml', views.index, {'sitemaps': callable_lastmod_full_sitemap}),
  272. path('callable-lastmod-full/sitemap.xml', views.sitemap, {'sitemaps': callable_lastmod_full_sitemap}),
  273. path(
  274. 'generic-lastmod/index.xml', views.index,
  275. {'sitemaps': generic_sitemaps_lastmod},
  276. name='django.contrib.sitemaps.views.index',
  277. ),
  278. ]
  279. urlpatterns += i18n_patterns(
  280. path('i18n/testmodel/<int:id>/', testmodelview, name='i18n_testmodel'),
  281. )