Projet

Général

Profil

0001-wcs-add-fields-to-customize-Card-display-54259.patch

Lauréline Guérin, 03 juin 2021 11:23

Télécharger (5,05 ko)

Voir les différences:

Subject: [PATCH] wcs: add fields to customize Card display (#54259)

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

  
71 71
class WcsCardInfoCellForm(forms.ModelForm):
72 72
    with_user = forms.BooleanField(label=_('Restrict to cards accessible to the user'), required=False)
73
    customize_display = forms.BooleanField(label=_('Customize display'), required=False)
73 74

  
74 75
    class Meta:
75 76
        model = WcsCardInfosCell
76
        fields = ('carddef_reference', 'card_id')
77
        fields = ('carddef_reference', 'card_id', 'custom_schema')
78
        widgets = {
79
            'custom_schema': forms.HiddenInput(),
80
        }
77 81

  
78 82
    def __init__(self, *args, **kwargs):
79 83
        instance = kwargs['instance']
......
82 86
        super().__init__(initial=initial, *args, **kwargs)
83 87
        card_models = get_wcs_options('/api/cards/@list')
84 88
        self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
89
        if self.instance.custom_schema:
90
            self.initial['customize_display'] = True
91
        if not self.instance.cached_json:
92
            del self.fields['customize_display']
93
            del self.fields['custom_schema']
85 94

  
86 95
    def save(self, *args, **kwargs):
87 96
        super().save(*args, **kwargs)
combo/apps/wcs/migrations/0029_card_custom_schema.py
1
from __future__ import unicode_literals
2

  
3
from django.contrib.postgres.fields.jsonb import JSONField
4
from django.db import migrations
5

  
6

  
7
class Migration(migrations.Migration):
8

  
9
    dependencies = [
10
        ('wcs', '0028_wcscardscell_without_user'),
11
    ]
12

  
13
    operations = [
14
        migrations.AddField(
15
            model_name='wcscardinfoscell',
16
            name='custom_schema',
17
            field=JSONField(blank=True, default=dict),
18
        ),
19
    ]
combo/apps/wcs/models.py
890 890
    carddef_reference = models.CharField(_('Card Model'), max_length=150)
891 891
    card_id = models.CharField(_('Card Identifier'), max_length=150, blank=True)
892 892
    without_user = models.BooleanField(_('Ignore the logged-in user'), default=False)
893
    custom_schema = JSONField(blank=True, default=dict)
893 894

  
894 895
    cached_title = models.CharField(_('Title'), max_length=150)
895 896
    cached_json = JSONField(blank=True, default=dict)
tests/test_wcs.py
1541 1541

  
1542 1542
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1543 1543
def test_card_cell_setup(mock_send):
1544
    cell = WcsCardInfosCell()
1544
    page = Page.objects.create(title='xxx', slug='test_card_cell_save_cache', template_name='standard')
1545
    cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
1545 1546
    form_class = cell.get_default_form_class()
1546 1547
    form = form_class(instance=cell)
1547 1548
    assert form.fields['carddef_reference'].widget.choices == [
......
1552 1553
        ('other:card_model_2', 'test2 : Card Model 2'),
1553 1554
        ('other:card_model_3', 'test2 : Card Model 3'),
1554 1555
    ]
1556
    assert 'customize_display' not in form.fields
1557
    assert 'custom_schema' not in form.fields
1558

  
1559
    cell.save()
1560
    assert 'customize_display' not in form.fields
1561
    assert 'custom_schema' not in form.fields
1562

  
1563
    cell.carddef_reference = 'default:card_model_1'
1564
    cell.save()
1565
    form = form_class(instance=cell)
1566
    assert 'customize_display' in form.fields
1567
    assert 'custom_schema' in form.fields
1568
    assert 'customize_display' not in form.initial
1569
    assert form.initial['custom_schema'] == {}
1570

  
1571
    cell.custom_schema = {'foo': 'bar'}
1572
    cell.save()
1573
    form = form_class(instance=cell)
1574
    assert 'customize_display' in form.fields
1575
    assert 'custom_schema' in form.fields
1576
    assert form.initial['customize_display'] is True
1577
    assert form.initial['custom_schema'] == {'foo': 'bar'}
1555 1578

  
1556 1579

  
1557 1580
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1558
-