From 350e177cdfc60dcd18a29706c54fb38b1b7e3149 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 12 Aug 2022 15:35:24 +0200
Subject: [PATCH 5/5] wcs: snapshot loading with old card cells (#68140)

---
 combo/data/models.py   |  10 ++
 tests/wcs/test_card.py | 204 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 213 insertions(+), 1 deletion(-)

diff --git a/combo/data/models.py b/combo/data/models.py
index f65d3e83..327b2117 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -605,6 +605,16 @@ class Page(models.Model):
     def load_serialized_cells(cls, cells):
         # load new cells
         for cell_data in cells:
+            # legacy card cells, for snapshots
+            if cell_data['model'] == 'wcs.wcscardinfoscell':
+                cell_data['model'] = 'wcs.wcscardcell'
+            elif cell_data['model'] == 'wcs.wcscardscell':
+                cell_data['model'] = 'wcs.wcscardcell'
+                cell_data['fields']['related_card_path'] = '__all__'
+                cell_data['fields']['display_mode'] = 'table'
+                cell_data['fields']['title_type'] = 'auto'
+                if cell_data['fields'].get('custom_title'):
+                    cell_data['fields']['title_type'] = 'manual'
             model = apps.get_model(cell_data['model'])
             cell_data = model.prepare_serialized_data(cell_data)
             cell = list(serializers.deserialize('json', json.dumps([cell_data]), ignorenonexistent=True))[0]
diff --git a/tests/wcs/test_card.py b/tests/wcs/test_card.py
index 6dc79936..18ef2000 100644
--- a/tests/wcs/test_card.py
+++ b/tests/wcs/test_card.py
@@ -14,7 +14,7 @@ from requests.models import Response
 
 from combo.apps.wcs.forms import WcsCardCellDisplayForm
 from combo.apps.wcs.models import WcsCardCell
-from combo.data.models import Page, TextCell, ValidityInfo
+from combo.data.models import Page, PageSnapshot, TextCell, ValidityInfo
 from tests.test_manager import login
 from tests.utils import manager_submit_cell
 
@@ -2511,3 +2511,205 @@ def test_card_cell_assets(mock_send, settings, app, admin_user):
     resp = app.get('/manage/assets/')
     assert 'Picture — %s' % cell1.get_label_for_asset() in resp.text
     assert 'Logo — %s' % cell2.get_label_for_asset() in resp.text
+
+
+def test_page_snapshot_with_old_card_cells():
+    """
+    page_serialization = {
+        'cells': [{
+            'model': 'data.parentcontentcell',
+            'fields': {
+                'slug': '',
+                'order': 0,
+                'groups': [],
+                'public': True,
+                'condition': None,
+                'placeholder': 'footer',
+                'template_name': None,
+                'extra_css_class': '',
+                'last_update_timestamp': '2022-08-08T13:55:29.599Z',
+                'restricted_to_unlogged': False,
+                'page': 14
+            }
+        }, {
+            'model': 'wcs.wcscardinfoscell',
+            'fields': {
+                'slug': 'enfant',
+                'limit': None,
+                'order': 1,
+                'groups': [],
+                'public': True,
+                'card_ids': '',
+                'condition': '',
+                'title_type': 'auto',
+                'placeholder': 'content',
+                'custom_title': '',
+                'display_mode': 'card',
+                'without_user': False,
+                'custom_schema': {},
+                'only_for_user': False,
+                'template_name': None,
+                'extra_css_class': '',
+                'carddef_reference': 'eservices:test-related-enfant',
+                'related_card_path': '',
+                'last_update_timestamp': '2022-08-11T13:57:43.362Z',
+                'restricted_to_unlogged': False,
+                'page': 14
+            }
+        }, {
+            'model': 'wcs.wcscardinfoscell',
+            'fields': {
+                'slug': '',
+                'limit': None,
+                'order': 2,
+                'groups': [],
+                'public': True,
+                'card_ids': '',
+                'condition': '',
+                'title_type': 'auto',
+                'placeholder': 'content',
+                'custom_title': '',
+                'display_mode': 'card',
+                'without_user': False,
+                'custom_schema': {},
+                'only_for_user': False,
+                'template_name': None,
+                'extra_css_class': '',
+                'carddef_reference': 'eservices:test-related-adulte:par-defaut',
+                'related_card_path': 'enfant/block_parents_parent',
+                'last_update_timestamp': '2022-08-08T19:53:02.097Z',
+                'restricted_to_unlogged': False,
+                'page': 14
+            }
+        }, {
+            'model': 'wcs.wcscardinfoscell',
+            'fields': {
+                'slug': '',
+                'limit': None,
+                'order': 3,
+                'groups': [],
+                'public': True,
+                'card_ids': '',
+                'condition': '',
+                'title_type': 'empty',
+                'placeholder': 'content',
+                'custom_title': 'Les parents !',
+                'display_mode': 'table',
+                'without_user': False,
+                'custom_schema': {},
+                'only_for_user': False,
+                'template_name': None,
+                'extra_css_class': '',
+                'carddef_reference': 'eservices:test-related-adulte:par-defaut',
+                'related_card_path': 'enfant/block_parents_parent',
+                'last_update_timestamp': '2022-08-11T13:07:35.757Z',
+                'restricted_to_unlogged': False,
+                'page': 14
+            }
+        }],
+        'fields': {
+            'slug': 'related-enfant',
+            'order': 6,
+            'title': 'Related enfant',
+            'groups': [],
+            'parent': ['search-related'],
+            'public': True,
+            'picture': '',
+            'snapshot': None,
+            'sub_slug': 'test-related-enfant_id',
+            'edit_role': None,
+            'description': '',
+            'redirect_url': '',
+            'template_name': 'standard',
+            'extra_variables': {},
+            'creation_timestamp': '2022-08-08T13:55:29.502Z',
+            'subpages_edit_role': None,
+            'placeholder_options': {},
+            'last_update_timestamp': '2022-08-11T13:57:43.369Z',
+            'exclude_from_navigation': True
+        },
+        'model': 'data.page',
+        'pk': 14
+    }
+    """
+    page = Page.objects.create(title='xxx', slug='test_snapshots', template_name='standard')
+    PageSnapshot.take(page)
+    snapshot = page.pagesnapshot_set.first()
+    snapshot.serialization['cells'].append(
+        {
+            'model': 'wcs.wcscardinfoscell',
+            'fields': {
+                'slug': 'my-card',
+                'limit': 42,
+                'order': 1,
+                'groups': [],
+                'public': True,
+                'card_ids': '42,35',
+                'condition': 'my-condition',
+                'title_type': 'manual',
+                'placeholder': 'content',
+                'custom_title': 'my-title',
+                'display_mode': 'card',
+                'without_user': True,
+                'custom_schema': {},
+                'only_for_user': True,
+                'template_name': None,
+                'extra_css_class': '',
+                'carddef_reference': 'default:card_model_1',
+                'related_card_path': '',
+                'last_update_timestamp': '2022-08-11T13:57:43.362Z',
+                'restricted_to_unlogged': False,
+                'page': page.pk,
+            },
+        }
+    )
+    snapshot.serialization['cells'].append(
+        {
+            'model': 'wcs.wcscardscell',
+            'fields': {
+                'slug': 'my-other-card',
+                'limit': 35,
+                'order': 2,
+                'groups': [],
+                'public': True,
+                'condition': '',
+                'placeholder': 'content',
+                'custom_title': 'my-other-title',
+                'without_user': False,
+                'only_for_user': False,
+                'template_name': None,
+                'extra_css_class': '',
+                'carddef_reference': 'default:card_model_1',
+                'last_update_timestamp': '2022-08-12T07:19:18.541Z',
+                'restricted_to_unlogged': False,
+                'page': page.pk,
+            },
+        }
+    )
+    old_page = snapshot.get_page()
+    cell1 = old_page.get_cells()[0]
+    assert isinstance(cell1, WcsCardCell)
+    assert cell1.slug == 'my-card'
+    assert cell1.limit == 42
+    assert cell1.card_ids == '42,35'
+    assert cell1.title_type == 'manual'
+    assert cell1.custom_title == 'my-title'
+    assert cell1.display_mode == 'card'
+    assert cell1.without_user is True
+    assert cell1.custom_schema == {}
+    assert cell1.only_for_user is True
+    assert cell1.carddef_reference == 'default:card_model_1'
+    assert cell1.related_card_path == ''
+    cell2 = old_page.get_cells()[1]
+    assert isinstance(cell1, WcsCardCell)
+    assert cell2.slug == 'my-other-card'
+    assert cell2.limit == 35
+    assert cell2.card_ids == ''
+    assert cell2.title_type == 'manual'
+    assert cell2.custom_title == 'my-other-title'
+    assert cell2.display_mode == 'table'
+    assert cell2.without_user is False
+    assert cell2.custom_schema == {}
+    assert cell2.only_for_user is False
+    assert cell2.carddef_reference == 'default:card_model_1'
+    assert cell2.related_card_path == '__all__'
-- 
2.35.1

