From 628a36fb44c6b22642d505736626a808decec674 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 13 Jan 2022 11:59:09 +0100 Subject: [PATCH] data: allow missing link_page on LinkCell import (#60320) --- combo/data/models.py | 13 +++++++++++++ tests/test_import_export.py | 17 ++++++++++++++++- tests/test_manager.py | 4 ++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/combo/data/models.py b/combo/data/models.py index 1822e8fd..684c91b9 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -1461,6 +1461,19 @@ class LinkCell(CellBase): self.check_validity() return result + @staticmethod + @utils.cache_during_request + def get_page_slugs(): + return {page.slug for page in Page.objects.all()} + + @classmethod + def prepare_serialized_data(cls, cell_data): + if 'link_page' in cell_data['fields']: + link_page_slug = cell_data['fields']['link_page'][0].strip('/').split('/')[-1] + if link_page_slug not in cls.get_page_slugs(): + del cell_data['fields']['link_page'] + return cell_data + def get_additional_label(self): title = self.title if not title and self.link_page: diff --git a/tests/test_import_export.py b/tests/test_import_export.py index 27d40776..a53d9452 100644 --- a/tests/test_import_export.py +++ b/tests/test_import_export.py @@ -21,7 +21,7 @@ from combo.apps.gallery.models import GalleryCell, Image from combo.apps.lingo.models import PaymentBackend, Regie from combo.apps.maps.models import Map, MapLayer, MapLayerOptions from combo.apps.pwa.models import PwaNavigationEntry, PwaSettings -from combo.data.models import Page, SiteSettings, TextCell +from combo.data.models import LinkCell, Page, SiteSettings, TextCell from combo.data.utils import MissingGroups, export_site, import_site pytestmark = pytest.mark.django_db @@ -519,3 +519,18 @@ 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_linkcell_to_missing_page(app, admin_user): + page1 = Page.objects.create(title='One', slug='one') + page2 = Page.objects.create(title='Two', slug='two') + link_cell = LinkCell.objects.create(page=page1, link_page=page2, placeholder='content', order=0) + + output = get_output_of_command('export_site') + payload = json.loads(output) + del payload['pages'][1] + import_site(data=payload, clean=True) + + cell = LinkCell.objects.get() + assert cell.link_page == None + assert cell.get_validity_info().invalid_reason_code == 'data_url_not_defined' diff --git a/tests/test_manager.py b/tests/test_manager.py index 5d05f35f..9b9829b3 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -926,7 +926,7 @@ def test_site_export_import_json(app, admin_user): resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json') with CaptureQueriesContext(connection) as ctx: resp = resp.form.submit() - assert len(ctx.captured_queries) in [303, 304] + assert len(ctx.captured_queries) in [304, 305] assert Page.objects.count() == 4 assert PageSnapshot.objects.all().count() == 4 @@ -937,7 +937,7 @@ def test_site_export_import_json(app, admin_user): resp.form['site_file'] = Upload('site-export.json', site_export, 'application/json') with CaptureQueriesContext(connection) as ctx: resp = resp.form.submit() - assert len(ctx.captured_queries) == 273 + assert len(ctx.captured_queries) == 274 assert set(Page.objects.get(slug='one').related_cells['cell_types']) == {'data_textcell', 'data_linkcell'} assert Page.objects.count() == 4 assert LinkCell.objects.count() == 2 -- 2.30.2