diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 6bd41e7321..515c0c680c 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -37,6 +37,7 @@ Changelog * Added multi-site support to the API (Sævar Öfjörð Magnússon) * Add `add_to_admin_menu` option for ModelAdmin (Oliver Parker) * Implement Fuzzy matching for Elasticsearch (Nick Smith) + * Rename `Page.get_latest_revision_as_page` to `Page.get_latest_revision_as_object` (Sage Abdullah) * Fix: Typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer) * Fix: Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke) * Fix: Remove extra padding for headers with breadcrumbs on mobile viewport (Steven Steinwand) diff --git a/docs/releases/4.0.md b/docs/releases/4.0.md index 6280b6558e..859cda195d 100644 --- a/docs/releases/4.0.md +++ b/docs/releases/4.0.md @@ -106,6 +106,9 @@ class MyModel(models.Model): revision = models.ForeignKey(get_revision_model(), on_delete=models.CASCADE) ``` +### `Page.get_latest_revision_as_page` renamed to `Page.get_latest_revision_as_object` + +The `Page.get_latest_revision_as_page` method has been renamed to `Page.get_latest_revision_as_object`. The old name still exists for backwards-compatibility, but calling it will raise a `RemovedInWagtail50Warning`. ### `AdminChooser` replaced with `BaseChooser` diff --git a/wagtail/actions/copy_page.py b/wagtail/actions/copy_page.py index 115c483ea2..f6c6646b73 100644 --- a/wagtail/actions/copy_page.py +++ b/wagtail/actions/copy_page.py @@ -236,7 +236,7 @@ class CopyPageAction: # * It makes sure update_attrs gets applied to the latest revision # * It bumps the last_revision_created_at value so the new page gets ordered as if it was just created # * It sets the user of the new revision so it's possible to see who copied the page by looking at its history - latest_revision = page_copy.get_latest_revision_as_page() + latest_revision = page_copy.get_latest_revision_as_object() if update_attrs: for field, value in update_attrs.items(): diff --git a/wagtail/admin/tests/pages/test_edit_page.py b/wagtail/admin/tests/pages/test_edit_page.py index 9c3e188c5f..cd5e985f0a 100644 --- a/wagtail/admin/tests/pages/test_edit_page.py +++ b/wagtail/admin/tests/pages/test_edit_page.py @@ -1864,7 +1864,7 @@ class TestParentalM2M(TestCase, WagtailTestUtils): self.assertRedirects( response, reverse("wagtailadmin_pages:edit", args=(created_page.id,)) ) - created_revision = created_page.get_latest_revision_as_page() + created_revision = created_page.get_latest_revision_as_object() self.assertIn(self.holiday_category, created_revision.categories.all()) self.assertIn(self.men_with_beards_category, created_revision.categories.all()) @@ -1945,7 +1945,7 @@ class TestParentalM2M(TestCase, WagtailTestUtils): response, reverse("wagtailadmin_pages:edit", args=(self.christmas_page.id,)) ) updated_page = EventPage.objects.get(id=self.christmas_page.id) - created_revision = updated_page.get_latest_revision_as_page() + created_revision = updated_page.get_latest_revision_as_object() self.assertIn(self.holiday_category, created_revision.categories.all()) self.assertIn(self.men_with_beards_category, created_revision.categories.all()) diff --git a/wagtail/admin/tests/test_workflows.py b/wagtail/admin/tests/test_workflows.py index a4d4289640..25c4891202 100644 --- a/wagtail/admin/tests/test_workflows.py +++ b/wagtail/admin/tests/test_workflows.py @@ -1452,7 +1452,7 @@ class TestApproveRejectWorkflow(TestCase, WagtailTestUtils): # Check that page edits made at the same time as the action have been saved page = Page.objects.get(id=self.page.id) self.assertEqual( - page.get_latest_revision_as_page().title, + page.get_latest_revision_as_object().title, "This title was edited while approving", ) diff --git a/wagtail/admin/views/pages/edit.py b/wagtail/admin/views/pages/edit.py index b40b67c0f3..14addcb18d 100644 --- a/wagtail/admin/views/pages/edit.py +++ b/wagtail/admin/views/pages/edit.py @@ -328,7 +328,7 @@ class EditView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View): "back to a branch where the model class is still present." ) - self.page = self.real_page_record.get_latest_revision_as_page() + self.page = self.real_page_record.get_latest_revision_as_object() self.parent = self.page.get_parent() self.page_perms = self.page.permissions_for_user(self.request.user) diff --git a/wagtail/admin/views/pages/preview.py b/wagtail/admin/views/pages/preview.py index 7123a46aa8..3ce6d3b8bb 100644 --- a/wagtail/admin/views/pages/preview.py +++ b/wagtail/admin/views/pages/preview.py @@ -12,7 +12,7 @@ from wagtail.models import Page def view_draft(request, page_id): - page = get_object_or_404(Page, id=page_id).get_latest_revision_as_page() + page = get_object_or_404(Page, id=page_id).get_latest_revision_as_object() perms = page.permissions_for_user(request.user) if not (perms.can_publish() or perms.can_edit()): raise PermissionDenied @@ -48,7 +48,7 @@ class PreviewOnEdit(View): def get_page(self): return get_object_or_404( Page, id=self.kwargs["page_id"] - ).get_latest_revision_as_page() + ).get_latest_revision_as_object() def get_form(self, page, query_dict): form_class = page.get_edit_handler().get_form_class() diff --git a/wagtail/models/__init__.py b/wagtail/models/__init__.py index 290bcfe1dc..c4f5418be7 100644 --- a/wagtail/models/__init__.py +++ b/wagtail/models/__init__.py @@ -1005,6 +1005,16 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): return self.revisions.order_by("-created_at", "-id").first() def get_latest_revision_as_page(self): + warnings.warn( + "Pages should use .get_latest_revision_as_object() instead of " + ".get_latest_revision_as_page() to retrieve the latest revision as a " + "Page instance.", + category=RemovedInWagtail50Warning, + stacklevel=2, + ) + return self.get_latest_revision_as_object() + + def get_latest_revision_as_object(self): if not self.has_unpublished_changes: # Use the live database copy in preference to the revision record, as: # 1) this will pick up any changes that have been made directly to the model, diff --git a/wagtail/tests/test_page_model.py b/wagtail/tests/test_page_model.py index b020f9f4dd..83cda16483 100644 --- a/wagtail/tests/test_page_model.py +++ b/wagtail/tests/test_page_model.py @@ -1497,11 +1497,11 @@ class TestCopyPage(TestCase): ) # Check that the attributes were updated in the latest revision - latest_revision = new_christmas_event.get_latest_revision_as_page() + latest_revision = new_christmas_event.get_latest_revision_as_object() self.assertEqual(latest_revision.title, "New christmas event") self.assertEqual(latest_revision.slug, "new-christmas-event") - # get_latest_revision_as_page might bypass the revisions table if it determines + # get_latest_revision_as_object might bypass the revisions table if it determines # that there are no draft edits since publish - so retrieve it explicitly from the # revision data, to ensure it's been updated there too latest_revision = new_christmas_event.get_latest_revision().as_object()