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

This commit is contained in:
Frédéric Péters 2020-11-09 15:27:49 +01:00
parent 9fd6500aea
commit 6238ac9b76
5 changed files with 47 additions and 21 deletions

View File

@ -1521,6 +1521,7 @@ def test_form_edit_item_field_data_source(pub):
(u'jsonp', False, u'JSONP URL'),
(u'python', False, u'Python Expression')
]
resp.form['data_mode'].value = 'data-source'
resp.form['data_source$type'].value = 'foobar'
resp = resp.form.submit('submit').follow()
assert FormDef.get(formdef.id).fields[0].data_source == {'type': 'foobar'}

View File

@ -74,13 +74,14 @@ class FieldDefPage(Directory):
form.clear_errors()
redo = True
try:
self.field.check_admin_form(form)
except AttributeError:
# informational fields don't have that method
pass
if form.has_errors():
redo = True
if form.is_submitted():
try:
self.field.check_admin_form(form)
except AttributeError:
# informational fields don't have that method
pass
if form.has_errors():
redo = True
if redo or not form.get_submit() == 'submit':
self.html_top(self.objectdef.name)

View File

@ -1636,16 +1636,29 @@ class ItemField(WidgetField):
title=_('Display Mode'),
options=options,
value=self.display_mode)
form.add(WidgetList, 'items', title = _('Items'), element_type = StringWidget,
value = self.items, required = False,
element_kwargs = {'render_br': False, 'size': 50},
add_element_label = _('Add item'))
real_data_source = data_sources.get_real(self.data_source)
form.add(RadiobuttonsWidget, 'data_mode',
title=_('Data'),
options=[('simple-list', _('Simple List'), 'simple-list'),
('data-source', _('Data Source'), 'data-source')],
value='data-source' if real_data_source else 'simple-list',
attrs={'data-dynamic-display-parent': 'true'},
extra_css_class='no-bottom-margin')
form.add(WidgetList, 'items', element_type=StringWidget,
value=self.items,
required=False,
element_kwargs={'render_br': False, 'size': 50},
add_element_label=_('Add item'),
attrs={'data-dynamic-display-child-of': 'data_mode',
'data-dynamic-display-value': 'simple-list'}
)
form.add(data_sources.DataSourceSelectionWidget, 'data_source',
value=self.data_source,
title=_('Data Source'),
hint=_('This will get the available items from an external source.'),
required=False,
advanced=is_datasource_advanced(self.data_source))
hint=_('This will get the available items from an external source.'),
attrs={'data-dynamic-display-child-of': 'data_mode',
'data-dynamic-display-value': 'data-source'}
)
form.add(CheckboxWidget, 'display_disabled_items',
title=_('Display disabled items'),
value=self.display_disabled_items,
@ -1657,13 +1670,19 @@ class ItemField(WidgetField):
'display_disabled_items']
def check_admin_form(self, form):
items = form.get_widget('items').parse()
d = {}
for v in (items or []):
if v in d:
form.set_error('items', _('Duplicated Items'))
return
d[v] = None
data_mode = form.get_widget('data_mode').parse()
if data_mode == 'simple-list':
items = form.get_widget('items').parse()
d = {}
for v in (items or []):
if v in d:
form.set_error('items', _('Duplicated Items'))
return
d[v] = None
data_source_type = form.get_widget('data_source').get_widget('type')
data_source_type.set_value(None)
data_source_type.transfer_form_value(get_request())
def stats(self, values):
return item_items_stats(self, values)

View File

@ -256,6 +256,7 @@ class RadiobuttonsWidget(quixote.form.RadiobuttonsWidget):
content_extra_attributes = {'role': 'radiogroup'}
def __init__(self, name, value=None, **kwargs):
self.extra_css_class = kwargs.pop('extra_css_class', None)
self.options_with_attributes = kwargs.pop('options_with_attributes', None)
super(RadiobuttonsWidget, self).__init__(name, value=value, **kwargs)

View File

@ -660,6 +660,10 @@ div.widget input[type="text"]:focus {
box-shadow: 0 0 0px 1px #1999cd;
}
div.widget.no-bottom-margin {
margin-bottom: 0.2rem;
}
div.DataSourceSelectionWidget select,
div.PrefillSelectionWidget select,
div.SingleSelectWidget select,