data: handle duplicated page slugs at import (#59509)

This commit is contained in:
Valentin Deniaud 2021-12-22 16:54:36 +01:00
parent 4a8f18d89b
commit 6370164fc8
2 changed files with 49 additions and 1 deletions

View File

@ -624,7 +624,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].split('/')[-1]
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

View File

@ -546,3 +546,45 @@ def test_import_export_linkcell_to_url(app, admin_user):
cell = LinkCell.objects.get()
assert cell.url == 'https://example.com'
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