From 4a2471c8aa4316126e5aaf19a8e9b3b9cadb29a1 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 9 Jan 2020 17:32:01 +0000 Subject: [PATCH] Ensure imported pages are added to revision history If we don't do this, and the page has draft edits at the destination, then the 'edit page' view will continue to treat those draft edits as more recent than the imported (published) version of the page. Additionally, saving a revision has the side effect of updating the Page.draft_title field with the new title, so we don't end up with an outdated title being shown in listings in the admin. --- tests/tests/test_import.py | 18 +++++++++++++++++- wagtail_transfer/operations.py | 13 +++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/tests/test_import.py b/tests/tests/test_import.py index 986e930..db03280 100644 --- a/tests/tests/test_import.py +++ b/tests/tests/test_import.py @@ -32,6 +32,11 @@ def tearDown(self): shutil.rmtree(TEST_MEDIA_DIR, ignore_errors=True) def test_import_pages(self): + # make a draft edit to the homepage + home = SimplePage.objects.get(slug='home') + home.title = "Draft home" + home.save_revision() + data = """{ "ids_for_import": [ ["wagtailcore.page", 12], @@ -59,7 +64,7 @@ def test_import_pages(self): "pk": 12, "parent_id": 1, "fields": { - "title": "Home", + "title": "New home", "show_in_menus": false, "live": true, "slug": "home", @@ -75,9 +80,20 @@ def test_import_pages(self): updated_page = SimplePage.objects.get(url_path='/home/') self.assertEqual(updated_page.intro, "This is the updated homepage") + self.assertEqual(updated_page.title, "New home") + self.assertEqual(updated_page.draft_title, "New home") + + # get_latest_revision (as used in the edit-page view) should also reflect the imported content + updated_page_revision = updated_page.get_latest_revision_as_page() + self.assertEqual(updated_page_revision.intro, "This is the updated homepage") + self.assertEqual(updated_page_revision.title, "New home") created_page = SimplePage.objects.get(url_path='/home/imported-child-page/') self.assertEqual(created_page.intro, "This page is imported from the source site") + # An initial page revision should also be created + self.assertTrue(created_page.get_latest_revision()) + created_page_revision = created_page.get_latest_revision_as_page() + self.assertEqual(created_page_revision.intro, "This page is imported from the source site") def test_import_pages_with_fk(self): data = """{ diff --git a/wagtail_transfer/operations.py b/wagtail_transfer/operations.py index 68e1086..32cc9f5 100644 --- a/wagtail_transfer/operations.py +++ b/wagtail_transfer/operations.py @@ -670,6 +670,10 @@ def _save(self, context): # Add the page to the database as a child of parent parent.add_child(instance=self.instance) + if isinstance(self.instance, Page): + # Also save this as a revision, so that it exists in revision history + self.instance.save_revision(changed=False) + class UpdateModel(SaveOperationMixin, Operation): def __init__(self, instance, object_data): @@ -682,6 +686,15 @@ def run(self, context): self._save(context) self._populate_many_to_many_fields(context) + def _save(self, context): + super()._save(context) + if isinstance(self.instance, Page): + # Also save this as a revision, so that: + # * the edit-page view will pick up this imported version rather than any currently-existing drafts + # * it exists in revision history + # * the Page.draft_title field (as used in page listings in the admin) is updated to match the real title + self.instance.save_revision(changed=False) + class DeleteModel(Operation): def __init__(self, instance):