[4.1.x] Fixed #34088 -- Fixed Sitemap.get_latest_lastmod() crash with empty items.
Bug in 480191244d12fefbf95854b2b117c71ffe44749a. Thanks Michal Čihař for the report. Backport of 5eab4d1924613a5506e517f157054b4852ae7dc2 from main
This commit is contained in:
parent
84a2b2e7a7
commit
eca526eab0
@ -167,7 +167,7 @@ class Sitemap:
|
|||||||
return None
|
return None
|
||||||
if callable(self.lastmod):
|
if callable(self.lastmod):
|
||||||
try:
|
try:
|
||||||
return max([self.lastmod(item) for item in self.items()])
|
return max([self.lastmod(item) for item in self.items()], default=None)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
@ -11,3 +11,8 @@ Bugfixes
|
|||||||
|
|
||||||
* Fixed a regression in Django 4.1 that caused an unnecessary table rebuilt
|
* Fixed a regression in Django 4.1 that caused an unnecessary table rebuilt
|
||||||
when adding ``ManyToManyField`` on SQLite (:ticket:`34138`).
|
when adding ``ManyToManyField`` on SQLite (:ticket:`34138`).
|
||||||
|
|
||||||
|
* Fixed a bug in Django 4.1 that caused a crash of the sitemap index view with
|
||||||
|
an empty :meth:`Sitemap.items() <django.contrib.sitemaps.Sitemap.items>` and
|
||||||
|
a callable :attr:`~django.contrib.sitemaps.Sitemap.lastmod`
|
||||||
|
(:ticket:`34088`).
|
||||||
|
@ -507,6 +507,16 @@ class HTTPSitemapTests(SitemapTestsBase):
|
|||||||
self.assertXMLEqual(index_response.content.decode(), expected_content_index)
|
self.assertXMLEqual(index_response.content.decode(), expected_content_index)
|
||||||
self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap)
|
self.assertXMLEqual(sitemap_response.content.decode(), expected_content_sitemap)
|
||||||
|
|
||||||
|
def test_callable_sitemod_no_items(self):
|
||||||
|
index_response = self.client.get("/callable-lastmod-no-items/index.xml")
|
||||||
|
self.assertNotIn("Last-Modified", index_response)
|
||||||
|
expected_content_index = """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
|
<sitemap><loc>http://example.com/simple/sitemap-callable-lastmod.xml</loc></sitemap>
|
||||||
|
</sitemapindex>
|
||||||
|
"""
|
||||||
|
self.assertXMLEqual(index_response.content.decode(), expected_content_index)
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango50Warning
|
# RemovedInDjango50Warning
|
||||||
class DeprecatedTests(SitemapTestsBase):
|
class DeprecatedTests(SitemapTestsBase):
|
||||||
|
@ -114,6 +114,16 @@ class CallableLastmodFullSitemap(Sitemap):
|
|||||||
return obj.lastmod
|
return obj.lastmod
|
||||||
|
|
||||||
|
|
||||||
|
class CallableLastmodNoItemsSitemap(Sitemap):
|
||||||
|
location = "/location/"
|
||||||
|
|
||||||
|
def items(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
def lastmod(self, obj):
|
||||||
|
return obj.lastmod
|
||||||
|
|
||||||
|
|
||||||
class GetLatestLastmodNoneSiteMap(Sitemap):
|
class GetLatestLastmodNoneSiteMap(Sitemap):
|
||||||
changefreq = "never"
|
changefreq = "never"
|
||||||
priority = 0.5
|
priority = 0.5
|
||||||
@ -233,6 +243,10 @@ callable_lastmod_full_sitemap = {
|
|||||||
"callable-lastmod": CallableLastmodFullSitemap,
|
"callable-lastmod": CallableLastmodFullSitemap,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callable_lastmod_no_items_sitemap = {
|
||||||
|
"callable-lastmod": CallableLastmodNoItemsSitemap,
|
||||||
|
}
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("simple/index.xml", views.index, {"sitemaps": simple_sitemaps}),
|
path("simple/index.xml", views.index, {"sitemaps": simple_sitemaps}),
|
||||||
path("simple-paged/index.xml", views.index, {"sitemaps": simple_sitemaps_paged}),
|
path("simple-paged/index.xml", views.index, {"sitemaps": simple_sitemaps_paged}),
|
||||||
@ -417,6 +431,11 @@ urlpatterns = [
|
|||||||
views.sitemap,
|
views.sitemap,
|
||||||
{"sitemaps": callable_lastmod_full_sitemap},
|
{"sitemaps": callable_lastmod_full_sitemap},
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
"callable-lastmod-no-items/index.xml",
|
||||||
|
views.index,
|
||||||
|
{"sitemaps": callable_lastmod_no_items_sitemap},
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
"generic-lastmod/index.xml",
|
"generic-lastmod/index.xml",
|
||||||
views.index,
|
views.index,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user