diff --git a/wagtail/contrib/wagtailsitemaps/sitemap_generator.py b/wagtail/contrib/wagtailsitemaps/sitemap_generator.py index ac169694b..f903a1aa8 100644 --- a/wagtail/contrib/wagtailsitemaps/sitemap_generator.py +++ b/wagtail/contrib/wagtailsitemaps/sitemap_generator.py @@ -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 ( diff --git a/wagtail/contrib/wagtailsitemaps/tests.py b/wagtail/contrib/wagtailsitemaps/tests.py index f740254be..9fa2147b8 100644 --- a/wagtail/contrib/wagtailsitemaps/tests.py +++ b/wagtail/contrib/wagtailsitemaps/tests.py @@ -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): diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index e560edfac..18d2bac10 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -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), } ]