Use last_published_at for lastmod in sitemaps - fixes #3565

This commit is contained in:
Matt Westcott 2017-05-23 12:47:57 +01:00
parent 21dc3e2fac
commit 7f34adda0a
3 changed files with 42 additions and 2 deletions

View file

@ -12,7 +12,11 @@ class Sitemap(DjangoSitemap):
return obj.specific.url
def lastmod(self, obj):
return obj.specific.latest_revision_created_at
obj = obj.specific
# fall back on latest_revision_created_at if last_published_at is null
# (for backwards compatibility from before last_published_at was added)
return (obj.last_published_at or obj.latest_revision_created_at)
def items(self):
return (

View file

@ -1,5 +1,8 @@
from __future__ import absolute_import, unicode_literals
import datetime
import pytz
from django.contrib.sites.shortcuts import get_current_site
from django.test import RequestFactory, TestCase
@ -18,6 +21,8 @@ class TestSitemapGenerator(TestCase):
slug='hello-world',
content="hello",
live=True,
last_published_at=datetime.datetime(2017, 1, 1, 12, 0, 0, tzinfo=pytz.utc),
latest_revision_created_at=datetime.datetime(2017, 2, 1, 12, 0, 0, tzinfo=pytz.utc)
))
self.unpublished_child_page = self.home_page.add_child(instance=SimplePage(
@ -35,6 +40,14 @@ class TestSitemapGenerator(TestCase):
))
PageViewRestriction.objects.create(page=self.protected_child_page, password='hello')
self.page_with_no_last_publish_date = self.home_page.add_child(instance=SimplePage(
title="I have no last publish date :-(",
slug='no-last-publish-date',
content="hello",
live=True,
latest_revision_created_at=datetime.datetime(2017, 2, 1, 12, 0, 0, tzinfo=pytz.utc)
))
self.site = Site.objects.get(is_default_site=True)
def test_items(self):
@ -74,6 +87,27 @@ class TestSitemapGenerator(TestCase):
self.assertIn('http://localhost/events/', urls) # Main view
self.assertIn('http://localhost/events/past/', urls) # Sub view
def test_lastmod_uses_last_published_date(self):
request = RequestFactory().get('/sitemap.xml')
req_protocol = request.scheme
req_site = get_current_site(request)
sitemap = Sitemap(self.site)
urls = sitemap.get_urls(1, req_site, req_protocol)
child_page_lastmod = [
url['lastmod'] for url in urls
if url['location'] == 'http://localhost/hello-world/'
][0]
self.assertEqual(child_page_lastmod, datetime.datetime(2017, 1, 1, 12, 0, 0, tzinfo=pytz.utc))
# if no last_publish_date is defined, use latest revision date
child_page_lastmod = [
url['lastmod'] for url in urls
if url['location'] == 'http://localhost/no-last-publish-date/'
][0]
self.assertEqual(child_page_lastmod, datetime.datetime(2017, 2, 1, 12, 0, 0, tzinfo=pytz.utc))
class TestIndexView(TestCase):
def test_index_view(self):

View file

@ -1350,7 +1350,9 @@ class Page(six.with_metaclass(PageBase, AbstractPage, index.Indexed, Clusterable
return [
{
'location': self.full_url,
'lastmod': self.latest_revision_created_at
# fall back on latest_revision_created_at if last_published_at is null
# (for backwards compatibility from before last_published_at was added)
'lastmod': (self.last_published_at or self.latest_revision_created_at),
}
]