From cdbae1f575ccc6cec4c4659d9da35fe13548e8ce Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 12 Mar 2015 10:57:54 +0000 Subject: [PATCH 1/5] Added first_published_at field to Page --- .../0011_page_first_published_at.py | 20 +++++++++++++++++++ wagtail/wagtailcore/models.py | 1 + 2 files changed, 21 insertions(+) create mode 100644 wagtail/wagtailcore/migrations/0011_page_first_published_at.py diff --git a/wagtail/wagtailcore/migrations/0011_page_first_published_at.py b/wagtail/wagtailcore/migrations/0011_page_first_published_at.py new file mode 100644 index 0000000000..c1466240a4 --- /dev/null +++ b/wagtail/wagtailcore/migrations/0011_page_first_published_at.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('wagtailcore', '0010_change_page_owner_to_null_on_delete'), + ] + + operations = [ + migrations.AddField( + model_name='page', + name='first_published_at', + field=models.DateTimeField(editable=False, null=True), + preserve_default=True, + ), + ] diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 2688f4b1e6..2268a9e87e 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -284,6 +284,7 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed locked = models.BooleanField(default=False, editable=False) + first_published_at = models.DateTimeField(null=True, editable=False) latest_revision_created_at = models.DateTimeField(null=True, editable=False) search_fields = ( From 0bbd8edf68ff27cc86b6cc4ce012a37478b8dd8d Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 12 Mar 2015 13:54:23 +0000 Subject: [PATCH 2/5] Set first_published_at on first publish --- wagtail/wagtailcore/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 2268a9e87e..35122105ac 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -1140,6 +1140,11 @@ class PageRevision(models.Model): # If page goes live clear the approved_go_live_at of all revisions page.revisions.update(approved_go_live_at=None) page.expired = False # When a page is published it can't be expired + + # Set first_published_at if the page is being published now + if page.live and page.first_published_at is None: + page.first_published_at = timezone.now() + page.save() self.submitted_for_moderation = False page.revisions.update(submitted_for_moderation=False) From 00e5138885cd5702deeac8af9119d8a41d4c5fa5 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 12 Mar 2015 13:55:06 +0000 Subject: [PATCH 3/5] Some tests for first_published_at --- wagtail/wagtailadmin/tests/test_pages_views.py | 4 ++++ wagtail/wagtailcore/tests/test_management_commands.py | 1 + 2 files changed, 5 insertions(+) diff --git a/wagtail/wagtailadmin/tests/test_pages_views.py b/wagtail/wagtailadmin/tests/test_pages_views.py index 0d4b5e93c5..0824b9d775 100644 --- a/wagtail/wagtailadmin/tests/test_pages_views.py +++ b/wagtail/wagtailadmin/tests/test_pages_views.py @@ -212,6 +212,7 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertEqual(page.title, post_data['title']) self.assertIsInstance(page, SimplePage) self.assertFalse(page.live) + self.assertFalse(page.first_published_at) # treebeard should report no consistency problems with the tree self.assertFalse(any(Page.find_problems()), 'treebeard found consistency problems') @@ -298,6 +299,7 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertEqual(page.title, post_data['title']) self.assertIsInstance(page, SimplePage) self.assertTrue(page.live) + self.assertTrue(page.first_published_at) # Check that the page_published signal was fired self.assertTrue(signal_fired[0]) @@ -333,6 +335,7 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertTrue(PageRevision.objects.filter(page=page).exclude(approved_go_live_at__isnull=True).exists()) # But Page won't be live self.assertFalse(page.live) + self.assertFalse(page.first_published_at) self.assertTrue(page.status_string, "scheduled") def test_create_simplepage_post_submit(self): @@ -357,6 +360,7 @@ class TestPageCreation(TestCase, WagtailTestUtils): self.assertEqual(page.title, post_data['title']) self.assertIsInstance(page, SimplePage) self.assertFalse(page.live) + self.assertFalse(page.first_published_at) # The latest revision for the page should now be in moderation self.assertTrue(page.get_latest_revision().submitted_for_moderation) diff --git a/wagtail/wagtailcore/tests/test_management_commands.py b/wagtail/wagtailcore/tests/test_management_commands.py index d631617b36..91fc727dcc 100644 --- a/wagtail/wagtailcore/tests/test_management_commands.py +++ b/wagtail/wagtailcore/tests/test_management_commands.py @@ -188,6 +188,7 @@ class TestPublishScheduledPagesCommand(TestCase): p = Page.objects.get(slug='hello-world') self.assertTrue(p.live) + self.assertTrue(p.first_published_at) self.assertFalse(p.has_unpublished_changes) self.assertFalse(PageRevision.objects.filter(page=p).exclude(approved_go_live_at__isnull=True).exists()) From 6f916a611b9ff8867f9c24d68fa795ec3c996cd9 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 17 Mar 2015 11:49:42 +0000 Subject: [PATCH 4/5] Make sure first_published_at is using live copy When publishing, the first_published at gets set on the live copy, not in the revision. This caused it to be set again on next publish because it is reading from the revision. --- wagtail/wagtailcore/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 35122105ac..2753842b70 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -1100,6 +1100,7 @@ class PageRevision(models.Model): obj.owner = self.page.owner obj.locked = self.page.locked obj.latest_revision_created_at = self.page.latest_revision_created_at + obj.first_published_at = self.page.first_published_at return obj From 24e633e8e7b7cc7887228c2ad076257908a97f23 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 17 Mar 2015 13:51:17 +0000 Subject: [PATCH 5/5] Test republishing a page wont set first_published --- .../wagtailadmin/tests/test_pages_views.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/wagtail/wagtailadmin/tests/test_pages_views.py b/wagtail/wagtailadmin/tests/test_pages_views.py index 0824b9d775..2a28ca4201 100644 --- a/wagtail/wagtailadmin/tests/test_pages_views.py +++ b/wagtail/wagtailadmin/tests/test_pages_views.py @@ -442,12 +442,13 @@ class TestPageEdit(TestCase, WagtailTestUtils): self.root_page = Page.objects.get(id=2) # Add child page - self.child_page = SimplePage() - self.child_page.title = "Hello world!" - self.child_page.slug = "hello-world" - self.child_page.live = True - self.root_page.add_child(instance=self.child_page) - self.child_page.save_revision() + child_page = SimplePage( + title="Hello world!", + slug="hello-world", + ) + self.root_page.add_child(instance=child_page) + child_page.save_revision().publish() + self.child_page = SimplePage.objects.get(id=child_page.id) # Add event page (to test edit handlers) self.event_page = EventPage() @@ -588,6 +589,9 @@ class TestPageEdit(TestCase, WagtailTestUtils): self.child_page.has_unpublished_changes = True self.child_page.save() + # Save current value of first_published_at so we can check that it doesn't change + first_published_at = SimplePage.objects.get(id=self.child_page.id).first_published_at + # Tests publish from edit page post_data = { 'title': "I've been edited!", @@ -612,6 +616,9 @@ class TestPageEdit(TestCase, WagtailTestUtils): # The page shouldn't have "has_unpublished_changes" flag set self.assertFalse(child_page_new.has_unpublished_changes) + # first_published_at should not change as it was already set + self.assertEqual(first_published_at, child_page_new.first_published_at) + def test_edit_post_publish_scheduled(self): go_live_at = timezone.now() + timedelta(days=1) expire_at = timezone.now() + timedelta(days=2)