From f4a1d9e8aa07c3ff2c0f5adc8df22eddfe0a9c7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 4 Jan 2022 16:03:49 +0100
Subject: [PATCH 1/3] wcs: method to get migrated custom_schema (#58800)

---
 combo/apps/wcs/forms.py  |  1 +
 combo/apps/wcs/models.py | 18 +++++++++++++++
 tests/test_wcs.py        | 50 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py
index 1aefeb32..8a0eb713 100644
--- a/combo/apps/wcs/forms.py
+++ b/combo/apps/wcs/forms.py
@@ -85,6 +85,7 @@ class WcsCardInfoCellForm(forms.ModelForm):
         self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
         if self.instance.custom_schema:
             self.initial['customize_display'] = True
+            self.initial['custom_schema'] = self.instance.get_custom_schema()
         if not self.instance.cached_json:
             del self.fields['customize_display']
             del self.fields['custom_schema']
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 09e74778..d4de946e 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -1076,6 +1076,24 @@ class WcsCardInfosCell(CardMixin, CellBase):
 
         return WcsCardInfoCellForm
 
+    def get_custom_schema(self):
+        custom_schema = self.custom_schema or {}
+
+        # migrate old formats
+        for cell in custom_schema.get('cells') or []:
+            if cell.get('varname') == '@custom@':
+                if cell.get('display_mode') == 'value':
+                    cell['display_mode'] = 'text'
+            elif not cell.get('field_content'):
+                if cell.get('display_mode') == 'title':
+                    cell['field_content'] = 'value'
+                else:
+                    cell['field_content'] = cell.get('display_mode') or 'value'
+                if cell.get('display_mode') in ['label', 'value', 'label-and-value']:
+                    cell['display_mode'] = 'text'
+
+        return custom_schema
+
 
 @register_cell_class
 class TrackingCodeInputCell(CellBase):
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index dbbf9952..7f058c15 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1705,13 +1705,15 @@ def test_card_cell_setup(mock_send, app, admin_user):
     assert 'customize_display' not in form.initial
     assert form.initial['custom_schema'] == {}
 
-    cell.custom_schema = {'foo': 'bar'}
+    cell.custom_schema = {'cells': [{'varname': 'foo', 'display_mode': 'value'}]}
     cell.save()
     form = form_class(instance=cell)
     assert 'customize_display' in form.fields
     assert 'custom_schema' in form.fields
     assert form.initial['customize_display'] is True
-    assert form.initial['custom_schema'] == {'foo': 'bar'}
+    assert form.initial['custom_schema'] == {
+        'cells': [{'varname': 'foo', 'field_content': 'value', 'display_mode': 'text'}]
+    }
 
     app = login(app)
     resp = app.get('/manage/pages/%s/' % page.pk)
@@ -1722,6 +1724,50 @@ def test_card_cell_setup(mock_send, app, admin_user):
     assert cell.custom_schema == {}
 
 
+def test_card_cell_custom_schema_migration():
+    cell = WcsCardInfosCell()
+
+    cell.custom_schema = {
+        'cells': [{'varname': 'some-field', 'display_mode': 'label', 'cell_size': 'foobar'}]
+    }
+    assert cell.get_custom_schema() == {
+        'cells': [
+            {'varname': 'some-field', 'field_content': 'label', 'display_mode': 'text', 'cell_size': 'foobar'}
+        ]
+    }
+    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'value'}]}
+    assert cell.get_custom_schema() == {
+        'cells': [{'varname': 'some-field', 'field_content': 'value', 'display_mode': 'text'}]
+    }
+    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'label-and-value'}]}
+    assert cell.get_custom_schema() == {
+        'cells': [{'varname': 'some-field', 'field_content': 'label-and-value', 'display_mode': 'text'}]
+    }
+    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'title'}]}
+    assert cell.get_custom_schema() == {
+        'cells': [{'varname': 'some-field', 'field_content': 'value', 'display_mode': 'title'}]
+    }
+
+    cell.custom_schema = {
+        'cells': [
+            {'varname': '@custom@', 'template': 'foobar', 'display_mode': 'label', 'cell_size': 'foobar'}
+        ]
+    }
+    assert cell.get_custom_schema() == {
+        'cells': [
+            {'varname': '@custom@', 'template': 'foobar', 'display_mode': 'label', 'cell_size': 'foobar'}
+        ]
+    }
+    cell.custom_schema = {'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'value'}]}
+    assert cell.get_custom_schema() == {
+        'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'text'}]
+    }
+    cell.custom_schema = {'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'title'}]}
+    assert cell.get_custom_schema() == {
+        'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'title'}]
+    }
+
+
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
 def test_card_cell_save_cache(mock_send):
     page = Page.objects.create(title='xxx', slug='test_card_cell_save_cache', template_name='standard')
-- 
2.34.1

