From c4148d906eb1f90bb07252150ccd8f97924d357b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 27 May 2021 10:44:51 +0200
Subject: [PATCH] wcs: add fields to customize Card display (#54259)

---
 combo/apps/wcs/forms.py                       |  9 ++++++++-
 .../wcs/migrations/0029_card_custom_schema.py | 19 +++++++++++++++++++
 combo/apps/wcs/models.py                      |  1 +
 tests/test_wcs.py                             | 15 ++++++++++++++-
 4 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 combo/apps/wcs/migrations/0029_card_custom_schema.py

diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py
index d6f75df1..85abdfe1 100644
--- a/combo/apps/wcs/forms.py
+++ b/combo/apps/wcs/forms.py
@@ -68,10 +68,14 @@ class WcsCardsCellForm(forms.ModelForm):
 
 class WcsCardInfoCellForm(forms.ModelForm):
     with_user = forms.BooleanField(label=_('Restrict to cards accessible to the user'), required=False)
+    customize_display = forms.BooleanField(label=_('Customize display'), required=False)
 
     class Meta:
         model = WcsCardInfosCell
-        fields = ('carddef_reference', 'card_id')
+        fields = ('carddef_reference', 'card_id', 'custom_schema')
+        widgets = {
+            'custom_schema': forms.HiddenInput(),
+        }
 
     def __init__(self, *args, **kwargs):
         instance = kwargs['instance']
@@ -80,6 +84,9 @@ class WcsCardInfoCellForm(forms.ModelForm):
         super().__init__(initial=initial, *args, **kwargs)
         card_models = get_wcs_options('/api/cards/@list')
         self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
+        if not self.instance.cached_json:
+            del self.fields['customize_display']
+            del self.fields['custom_schema']
 
     def save(self, *args, **kwargs):
         super().save(*args, **kwargs)
diff --git a/combo/apps/wcs/migrations/0029_card_custom_schema.py b/combo/apps/wcs/migrations/0029_card_custom_schema.py
new file mode 100644
index 00000000..9a93b075
--- /dev/null
+++ b/combo/apps/wcs/migrations/0029_card_custom_schema.py
@@ -0,0 +1,19 @@
+from __future__ import unicode_literals
+
+import jsonfield.fields
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('wcs', '0028_wcscardscell_without_user'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='wcscardinfoscell',
+            name='custom_schema',
+            field=jsonfield.fields.JSONField(blank=True, default=dict),
+        ),
+    ]
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 60f15353..86259c4c 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -887,6 +887,7 @@ class WcsCardInfosCell(CardMixin, CellBase):
     carddef_reference = models.CharField(_('Card Model'), max_length=150)
     card_id = models.CharField(_('Card Identifier'), max_length=150, blank=True)
     without_user = models.BooleanField(_('Ignore the logged-in user'), default=False)
+    custom_schema = JSONField(blank=True)
 
     cached_title = models.CharField(_('Title'), max_length=150)
     cached_json = JSONField(blank=True)
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 4fbc49df..916f2422 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1541,7 +1541,8 @@ def test_manager_card_cell(mock_send, app, admin_user):
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
 def test_card_cell_setup(mock_send):
-    cell = WcsCardInfosCell()
+    page = Page.objects.create(title='xxx', slug='test_card_cell_save_cache', template_name='standard')
+    cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
     form_class = cell.get_default_form_class()
     form = form_class(instance=cell)
     assert form.fields['carddef_reference'].widget.choices == [
@@ -1552,6 +1553,18 @@ def test_card_cell_setup(mock_send):
         ('other:card_model_2', 'test2 : Card Model 2'),
         ('other:card_model_3', 'test2 : Card Model 3'),
     ]
+    assert 'customize_display' not in form.fields
+    assert 'custom_schema' not in form.fields
+
+    cell.save()
+    assert 'customize_display' not in form.fields
+    assert 'custom_schema' not in form.fields
+
+    cell.carddef_reference = 'default:card_model_1'
+    cell.save()
+    form = form_class(instance=cell)
+    assert 'customize_display' in form.fields
+    assert 'custom_schema' in form.fields
 
 
 @mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
-- 
2.30.2

