Skip to content

Commit

Permalink
Ensure imported pages are added to revision history
Browse files Browse the repository at this point in the 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.
  • Loading branch information
gasman committed Jan 13, 2020
1 parent 89a7dac commit 4a2471c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tests/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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",
Expand All @@ -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 = """{
Expand Down
13 changes: 13 additions & 0 deletions wagtail_transfer/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 4a2471c

Please sign in to comment.