From 3f67445e41475fb8657e3bae638cfd3a6b544fc9 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 22 Dec 2021 16:54:36 +0100 Subject: [PATCH] data: handle duplicated page slugs at import (#59509) --- combo/data/models.py | 8 ++++++- tests/test_import_export.py | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/combo/data/models.py b/combo/data/models.py index 1822e8fd..5c4ce26e 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -603,7 +603,13 @@ class Page(models.Model): for json_page in json_site: # pre-create pages - page, created = Page.objects.get_or_create(slug=json_page['fields']['slug']) + parent = None + if json_page['fields'].get('parent'): + parent = json_page['fields']['parent'][0] + + page, created = Page.objects.get_or_create( + slug=json_page['fields']['slug'], parent__slug=parent + ) to_load.append((page, created, json_page)) # delete cells of already existing pages diff --git a/tests/test_import_export.py b/tests/test_import_export.py index 27d40776..ea825a82 100644 --- a/tests/test_import_export.py +++ b/tests/test_import_export.py @@ -519,3 +519,45 @@ def test_import_export_settings(app): site_settings.refresh_from_db() assert site_settings.initial_login_page_path == '' assert site_settings.welcome_page_path == '' + + +def test_import_export_duplicated_slugs(): + first_page = Page.objects.create(title='Title', slug='title', description='1') + output = get_output_of_command('export_site') + + # create real situation where a subpage has the same slug as a top-level page + second_page = Page.objects.create(title='Test', slug='test') + third_page = Page.objects.create(title='Title', slug='title', description='test', parent=second_page) + first_page.description = '2' + first_page.save() + + import_site(data=json.loads(output)) + assert Page.objects.count() == 3 + + # top-level page has been updated + first_page.refresh_from_db() + assert first_page.description == '1' + + # same slug subpage was left untouched + third_page.refresh_from_db() + assert third_page.description == 'test' + + output = get_output_of_command('export_site') + third_page.description = '' + third_page.save() + + import_site(data=json.loads(output)) + assert Page.objects.count() == 3 + + # top level page was left untouched + first_page.refresh_from_db() + assert first_page.description == '1' + + # same slug subpage has been updated + third_page.refresh_from_db() + assert third_page.description == 'test' + + Page.objects.all().delete() + import_site(data=json.loads(output)) + assert Page.objects.count() == 3 + assert Page.objects.filter(parent__isnull=True).count() == 2 -- 2.30.2