Project

General

Profile

0001-fields-merge-simple-string-list-and-data-sources-in-.patch

Frédéric Péters, 24 Nov 2020 08:47 AM

Download (6.17 KB)

View differences:

Subject: [PATCH] fields: "merge" simple string list and data sources in UI
 (#48627)

 tests/admin_pages/test_form.py      |  1 +
 wcs/admin/fields.py                 | 15 ++++-----
 wcs/fields.py                       | 47 ++++++++++++++++++++---------
 wcs/qommon/form.py                  |  1 +
 wcs/qommon/static/css/dc2/admin.css |  4 +++
 5 files changed, 47 insertions(+), 21 deletions(-)
tests/admin_pages/test_form.py
1521 1521
        (u'jsonp', False, u'JSONP URL'),
1522 1522
        (u'python', False, u'Python Expression')
1523 1523
    ]
1524
    resp.form['data_mode'].value = 'data-source'
1524 1525
    resp.form['data_source$type'].value = 'foobar'
1525 1526
    resp = resp.form.submit('submit').follow()
1526 1527
    assert FormDef.get(formdef.id).fields[0].data_source == {'type': 'foobar'}
wcs/admin/fields.py
74 74
            form.clear_errors()
75 75
            redo = True
76 76

  
77
        try:
78
            self.field.check_admin_form(form)
79
        except AttributeError:
80
            # informational fields don't have that method
81
            pass
82
        if form.has_errors():
83
            redo = True
77
        if form.is_submitted():
78
            try:
79
                self.field.check_admin_form(form)
80
            except AttributeError:
81
                # informational fields don't have that method
82
                pass
83
            if form.has_errors():
84
                redo = True
84 85

  
85 86
        if redo or not form.get_submit() == 'submit':
86 87
            self.html_top(self.objectdef.name)
wcs/fields.py
1636 1636
                title=_('Display Mode'),
1637 1637
                options=options,
1638 1638
                value=self.display_mode)
1639
        form.add(WidgetList, 'items', title = _('Items'), element_type = StringWidget,
1640
                value = self.items, required = False,
1641
                element_kwargs = {'render_br': False, 'size': 50},
1642
                add_element_label = _('Add item'))
1639
        real_data_source = data_sources.get_real(self.data_source)
1640
        form.add(RadiobuttonsWidget, 'data_mode',
1641
                title=_('Data'),
1642
                options=[('simple-list', _('Simple List'), 'simple-list'),
1643
                         ('data-source', _('Data Source'), 'data-source')],
1644
                value='data-source' if real_data_source else 'simple-list',
1645
                attrs={'data-dynamic-display-parent': 'true'},
1646
                extra_css_class='no-bottom-margin')
1647
        form.add(WidgetList, 'items', element_type=StringWidget,
1648
                value=self.items,
1649
                required=False,
1650
                element_kwargs={'render_br': False, 'size': 50},
1651
                add_element_label=_('Add item'),
1652
                attrs={'data-dynamic-display-child-of': 'data_mode',
1653
                       'data-dynamic-display-value': 'simple-list'}
1654
                )
1643 1655
        form.add(data_sources.DataSourceSelectionWidget, 'data_source',
1644 1656
                 value=self.data_source,
1645
                 title=_('Data Source'),
1646
                 hint=_('This will get the available items from an external source.'),
1647 1657
                 required=False,
1648
                 advanced=is_datasource_advanced(self.data_source))
1658
                 hint=_('This will get the available items from an external source.'),
1659
                 attrs={'data-dynamic-display-child-of': 'data_mode',
1660
                        'data-dynamic-display-value': 'data-source'}
1661
                 )
1649 1662
        form.add(CheckboxWidget, 'display_disabled_items',
1650 1663
                title=_('Display disabled items'),
1651 1664
                value=self.display_disabled_items,
......
1657 1670
                'display_disabled_items']
1658 1671

  
1659 1672
    def check_admin_form(self, form):
1660
        items = form.get_widget('items').parse()
1661
        d = {}
1662
        for v in (items or []):
1663
            if v in d:
1664
                form.set_error('items', _('Duplicated Items'))
1665
                return
1666
            d[v] = None
1673
        data_mode = form.get_widget('data_mode').parse()
1674
        if data_mode == 'simple-list':
1675
            items = form.get_widget('items').parse()
1676
            d = {}
1677
            for v in (items or []):
1678
                if v in d:
1679
                    form.set_error('items', _('Duplicated Items'))
1680
                    return
1681
                d[v] = None
1682

  
1683
            data_source_type = form.get_widget('data_source').get_widget('type')
1684
            data_source_type.set_value(None)
1685
            data_source_type.transfer_form_value(get_request())
1667 1686

  
1668 1687
    def stats(self, values):
1669 1688
        return item_items_stats(self, values)
wcs/qommon/form.py
256 256
    content_extra_attributes = {'role': 'radiogroup'}
257 257

  
258 258
    def __init__(self, name, value=None, **kwargs):
259
        self.extra_css_class = kwargs.pop('extra_css_class', None)
259 260
        self.options_with_attributes = kwargs.pop('options_with_attributes', None)
260 261
        super(RadiobuttonsWidget, self).__init__(name, value=value, **kwargs)
261 262

  
wcs/qommon/static/css/dc2/admin.css
660 660
	box-shadow: 0 0 0px 1px #1999cd;
661 661
}
662 662

  
663
div.widget.no-bottom-margin {
664
	margin-bottom: 0.2rem;
665
}
666

  
663 667
div.DataSourceSelectionWidget select,
664 668
div.PrefillSelectionWidget select,
665 669
div.SingleSelectWidget select,
666
-