Rename `Page.get_latest_revision_as_page` to `Page.get_latest_revision_as_object`

pull/8720/head
Sage Abdullah 2022-05-25 21:58:15 +07:00 zatwierdzone przez Karl Hobley
rodzic 9dd4d6c6c4
commit 3215bebddf
9 zmienionych plików z 23 dodań i 9 usunięć

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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