1
0
mirror of https://github.com/django/django.git synced 2025-10-26 23:26:08 +00:00

Fixed #25989 -- Corrected sitemap's Last-Modified header to use the latest lastmod of all sitemaps.

Previously, the lastmod of the last sitemap was always used.
All sitemaps are required to have a lastmod.
This commit is contained in:
Alexander Rudakov
2015-12-26 21:01:25 +03:00
committed by Tim Graham
parent 104eddbdf6
commit 002a4f72c4
3 changed files with 86 additions and 9 deletions

View File

@@ -57,6 +57,8 @@ def sitemap(request, sitemaps, section=None,
maps = sitemaps.values()
page = request.GET.get("p", 1)
lastmod = None
all_sites_lastmod = True
urls = []
for site in maps:
try:
@@ -64,20 +66,24 @@ def sitemap(request, sitemaps, section=None,
site = site()
urls.extend(site.get_urls(page=page, site=req_site,
protocol=req_protocol))
if all_sites_lastmod:
site_lastmod = getattr(site, 'latest_lastmod', None)
if site_lastmod is not None:
site_lastmod = (
site_lastmod.utctimetuple() if isinstance(site_lastmod, datetime.datetime)
else site_lastmod.timetuple()
)
lastmod = site_lastmod if lastmod is None else max(lastmod, site_lastmod)
else:
all_sites_lastmod = False
except EmptyPage:
raise Http404("Page %s empty" % page)
except PageNotAnInteger:
raise Http404("No page '%s'" % page)
response = TemplateResponse(request, template_name, {'urlset': urls},
content_type=content_type)
if hasattr(site, 'latest_lastmod'):
# if latest_lastmod is defined for site, set header so as
if all_sites_lastmod and lastmod is not None:
# if lastmod is defined for all sites, set header so as
# ConditionalGetMiddleware is able to send 304 NOT MODIFIED
lastmod = site.latest_lastmod
response['Last-Modified'] = http_date(
timegm(
lastmod.utctimetuple() if isinstance(lastmod, datetime.datetime)
else lastmod.timetuple()
)
)
response['Last-Modified'] = http_date(timegm(lastmod))
return response