Projet

Général

Profil

0001-wcs-method-to-get-migrated-custom_schema-58800.patch

Lauréline Guérin, 06 janvier 2022 16:46

Télécharger (5,6 ko)

Voir les différences:

Subject: [PATCH 1/3] wcs: method to get migrated custom_schema (#58800)

 combo/apps/wcs/forms.py  |  1 +
 combo/apps/wcs/models.py | 19 ++++++++++++++
 tests/test_wcs.py        | 57 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 75 insertions(+), 2 deletions(-)
combo/apps/wcs/forms.py
85 85
        self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
86 86
        if self.instance.custom_schema:
87 87
            self.initial['customize_display'] = True
88
            self.initial['custom_schema'] = self.instance.get_custom_schema()
88 89
        if not self.instance.cached_json:
89 90
            del self.fields['customize_display']
90 91
            del self.fields['custom_schema']
combo/apps/wcs/models.py
1076 1076

  
1077 1077
        return WcsCardInfoCellForm
1078 1078

  
1079
    def get_custom_schema(self):
1080
        custom_schema = self.custom_schema or {}
1081
        custom_schema['cells'] = custom_schema.get('cells') or []
1082

  
1083
        # migrate old formats
1084
        for cell in custom_schema.get('cells') or []:
1085
            if cell.get('varname') == '@custom@':
1086
                if cell.get('display_mode') == 'value':
1087
                    cell['display_mode'] = 'text'
1088
            elif not cell.get('field_content'):
1089
                if cell.get('display_mode') == 'title':
1090
                    cell['field_content'] = 'value'
1091
                else:
1092
                    cell['field_content'] = cell.get('display_mode') or 'value'
1093
                if cell.get('display_mode') in ['label', 'value', 'label-and-value']:
1094
                    cell['display_mode'] = 'text'
1095

  
1096
        return custom_schema
1097

  
1079 1098

  
1080 1099
@register_cell_class
1081 1100
class TrackingCodeInputCell(CellBase):
tests/test_wcs.py
1705 1705
    assert 'customize_display' not in form.initial
1706 1706
    assert form.initial['custom_schema'] == {}
1707 1707

  
1708
    cell.custom_schema = {'foo': 'bar'}
1708
    cell.custom_schema = {'cells': [{'varname': 'foo', 'display_mode': 'value'}]}
1709 1709
    cell.save()
1710 1710
    form = form_class(instance=cell)
1711 1711
    assert 'customize_display' in form.fields
1712 1712
    assert 'custom_schema' in form.fields
1713 1713
    assert form.initial['customize_display'] is True
1714
    assert form.initial['custom_schema'] == {'foo': 'bar'}
1714
    assert form.initial['custom_schema'] == {
1715
        'cells': [{'varname': 'foo', 'field_content': 'value', 'display_mode': 'text'}]
1716
    }
1715 1717

  
1716 1718
    app = login(app)
1717 1719
    resp = app.get('/manage/pages/%s/' % page.pk)
......
1722 1724
    assert cell.custom_schema == {}
1723 1725

  
1724 1726

  
1727
def test_card_cell_custom_schema_migration():
1728
    cell = WcsCardInfosCell()
1729

  
1730
    cell.custom_schema = {'grid_class': 'foobar', 'grid_layout_label': 'barfoo'}
1731
    assert cell.get_custom_schema() == {
1732
        'cells': [],
1733
        'grid_class': 'foobar',
1734
        'grid_layout_label': 'barfoo',
1735
    }
1736

  
1737
    cell.custom_schema = {
1738
        'cells': [{'varname': 'some-field', 'display_mode': 'label', 'cell_size': 'foobar'}]
1739
    }
1740
    assert cell.get_custom_schema() == {
1741
        'cells': [
1742
            {'varname': 'some-field', 'field_content': 'label', 'display_mode': 'text', 'cell_size': 'foobar'}
1743
        ]
1744
    }
1745
    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'value'}]}
1746
    assert cell.get_custom_schema() == {
1747
        'cells': [{'varname': 'some-field', 'field_content': 'value', 'display_mode': 'text'}]
1748
    }
1749
    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'label-and-value'}]}
1750
    assert cell.get_custom_schema() == {
1751
        'cells': [{'varname': 'some-field', 'field_content': 'label-and-value', 'display_mode': 'text'}]
1752
    }
1753
    cell.custom_schema = {'cells': [{'varname': 'some-field', 'display_mode': 'title'}]}
1754
    assert cell.get_custom_schema() == {
1755
        'cells': [{'varname': 'some-field', 'field_content': 'value', 'display_mode': 'title'}]
1756
    }
1757

  
1758
    cell.custom_schema = {
1759
        'cells': [
1760
            {'varname': '@custom@', 'template': 'foobar', 'display_mode': 'label', 'cell_size': 'foobar'}
1761
        ]
1762
    }
1763
    assert cell.get_custom_schema() == {
1764
        'cells': [
1765
            {'varname': '@custom@', 'template': 'foobar', 'display_mode': 'label', 'cell_size': 'foobar'}
1766
        ]
1767
    }
1768
    cell.custom_schema = {'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'value'}]}
1769
    assert cell.get_custom_schema() == {
1770
        'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'text'}]
1771
    }
1772
    cell.custom_schema = {'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'title'}]}
1773
    assert cell.get_custom_schema() == {
1774
        'cells': [{'varname': '@custom@', 'template': 'foobar', 'display_mode': 'title'}]
1775
    }
1776

  
1777

  
1725 1778
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
1726 1779
def test_card_cell_save_cache(mock_send):
1727 1780
    page = Page.objects.create(title='xxx', slug='test_card_cell_save_cache', template_name='standard')
1728
-