Projet

Général

Profil

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

Lauréline Guérin, 31 mai 2021 15:03

Télécharger (4,53 ko)

Voir les différences:

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
combo/apps/wcs/forms.py
68 68

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

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

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

  
84 91
    def save(self, *args, **kwargs):
85 92
        super().save(*args, **kwargs)
combo/apps/wcs/migrations/0029_card_custom_schema.py
1
from __future__ import unicode_literals
2

  
3
import jsonfield.fields
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.fields.JSONField(blank=True, default=dict),
18
        ),
19
    ]
combo/apps/wcs/models.py
887 887
    carddef_reference = models.CharField(_('Card Model'), max_length=150)
888 888
    card_id = models.CharField(_('Card Identifier'), max_length=150, blank=True)
889 889
    without_user = models.BooleanField(_('Ignore the logged-in user'), default=False)
890
    custom_schema = JSONField(blank=True)
890 891

  
891 892
    cached_title = models.CharField(_('Title'), max_length=150)
892 893
    cached_json = JSONField(blank=True)
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
1555 1568

  
1556 1569

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