From 00adeb8cf29cd22d83e75aee425e9d47bf62659d Mon Sep 17 00:00:00 2001 From: Andy Chosak Date: Thu, 31 Mar 2022 14:47:35 -0400 Subject: [PATCH] Fix bulk publishing of pages without revisions Currently bulk publishing raises an error if you include pages that exist in the database without any revisions. This commit ensures that a revision exists before the page is published. Fixes issue 8187. --- CHANGELOG.txt | 1 + docs/releases/3.0.md | 1 + .../test_bulk_actions/test_bulk_publish.py | 35 +++++++++++++++++-- .../admin/views/pages/bulk_actions/publish.py | 12 +++++-- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index a8a8f7efe9..7f8990f0c5 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -46,6 +46,7 @@ Changelog * Fix: Use a button element instead of a link for page explorer menu item, for the correct semantics and behavior (LB (Ben Johnston)) * Fix: Make sure the “Title” column label can be translated in the page chooser and page move UI (Stephanie Cheng Smith) * Fix: Remove redundant `role="main"` attributes on `
` elements causing HTML validation issues (Luis Espinoza) + * Fix: Allow bulk publishing of pages without revisions (Andy Chosak) 2.16.2 (xx.xx.xxxx) - IN DEVELOPMENT diff --git a/docs/releases/3.0.md b/docs/releases/3.0.md index 3f198c7555..2749fc08b2 100644 --- a/docs/releases/3.0.md +++ b/docs/releases/3.0.md @@ -73,6 +73,7 @@ The panel types `StreamFieldPanel`, `RichTextFieldPanel`, `ImageChooserPanel`, ` * Use a button element instead of a link for page explorer menu item, for the correct semantics and behavior (LB (Ben Johnston)) * Make sure the “Title” column label can be translated in the page chooser and page move UI (Stephanie Cheng Smith) * Remove redundant `role="main"` attributes on `
` elements causing HTML validation issues (Luis Espinoza) + * Allow bulk publishing of pages without revisions (Andy Chosak) ## Upgrade considerations diff --git a/wagtail/admin/tests/pages/test_bulk_actions/test_bulk_publish.py b/wagtail/admin/tests/pages/test_bulk_actions/test_bulk_publish.py index ddaaa9a46b..95762a8cb7 100644 --- a/wagtail/admin/tests/pages/test_bulk_actions/test_bulk_publish.py +++ b/wagtail/admin/tests/pages/test_bulk_actions/test_bulk_publish.py @@ -17,7 +17,8 @@ class TestBulkPublish(TestCase, WagtailTestUtils): def setUp(self): self.root_page = Page.objects.get(id=2) - # Add child pages + # Add child pages which will have already been published before we + # bulk publish them. self.child_pages = [ SimplePage( title=f"Hello world!-{i}", @@ -27,6 +28,7 @@ class TestBulkPublish(TestCase, WagtailTestUtils): ) for i in range(1, 5) ] + self.pages_to_be_published = self.child_pages[:3] self.pages_not_to_be_published = self.child_pages[3:] @@ -34,9 +36,22 @@ class TestBulkPublish(TestCase, WagtailTestUtils): self.root_page.add_child(instance=child_page) for i, child_page in enumerate(self.child_pages): - child_page.content = f"Hello updated world {i}!" + child_page.content = f"Hello published world {i}!" child_page.save_revision() + # Add an additional child page which will be bulk published from a + # draft-only state. + draft_page = SimplePage( + title="Hello world!-5", + slug="hello-world-5", + content="Hello published world 5!", + live=False, + ) + + self.root_page.add_child(instance=draft_page) + self.child_pages.append(draft_page) + self.pages_to_be_published.append(draft_page) + self.url = ( reverse( "wagtail_bulk_action", @@ -137,7 +152,7 @@ class TestBulkPublish(TestCase, WagtailTestUtils): for child_page in self.pages_to_be_published: published_page = SimplePage.objects.get(id=child_page.id) self.assertTrue(published_page.live) - self.assertIn("Hello updated", published_page.content) + self.assertIn("Hello published", published_page.content) # Check that the child pages not to be published remain for child_page in self.pages_not_to_be_published: @@ -231,6 +246,8 @@ class TestBulkPublishIncludingDescendants(TestCase, WagtailTestUtils): child_page.content = f"Hello updated world {i}!" child_page.save_revision() + # Add grandchild pages which will have already been published before + # we bulk publish them. # map of the form { page: [child_pages] } to be added self.grandchildren_pages = { self.pages_to_be_published[0]: [ @@ -267,6 +284,18 @@ class TestBulkPublishIncludingDescendants(TestCase, WagtailTestUtils): ) grandchild_page.save_revision() + # Add an additional grandchild page which will be bulk published from + # a draft-only state. + draft_page = SimplePage( + title="Hello world!-d", + slug="hello-world-d", + content="Hello grandchild d!", + live=False, + ) + + self.pages_to_be_published[1].add_child(instance=draft_page) + self.grandchildren_pages[self.pages_to_be_published[1]].append(draft_page) + self.url = ( reverse( "wagtail_bulk_action", diff --git a/wagtail/admin/views/pages/bulk_actions/publish.py b/wagtail/admin/views/pages/bulk_actions/publish.py index 783affdfe1..4d4422a447 100644 --- a/wagtail/admin/views/pages/bulk_actions/publish.py +++ b/wagtail/admin/views/pages/bulk_actions/publish.py @@ -40,7 +40,10 @@ class PublishBulkAction(PageBulkAction): def execute_action(cls, objects, include_descendants=False, user=None, **kwargs): num_parent_objects, num_child_objects = 0, 0 for page in objects: - page.get_latest_revision().publish(user=user) + revision = page.get_latest_revision() or page.specific.save_revision( + user=user + ) + revision.publish(user=user) num_parent_objects += 1 if include_descendants: @@ -53,8 +56,13 @@ class PublishBulkAction(PageBulkAction): user ).can_publish() ): - draft_descendant_page.get_latest_revision().publish(user=user) + draft_descendant_revision = ( + draft_descendant_page.get_latest_revision() + or draft_descendant_page.save_revision(user=user) + ) + draft_descendant_revision.publish(user=user) num_child_objects += 1 + return num_parent_objects, num_child_objects def get_success_message(self, num_parent_objects, num_child_objects):