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.
pull/7953/head
Andy Chosak 2022-03-31 14:47:35 -04:00
rodzic 30ad2ee217
commit 00adeb8cf2
4 zmienionych plików z 44 dodań i 5 usunięć

Wyświetl plik

@ -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 `<main>` 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

Wyświetl plik

@ -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 `<main>` elements causing HTML validation issues (Luis Espinoza)
* Allow bulk publishing of pages without revisions (Andy Chosak)
## Upgrade considerations

Wyświetl plik

@ -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",

Wyświetl plik

@ -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):