backoffice: only allow python as prefilling source for items field (#22146)

This commit is contained in:
Frédéric Péters 2018-02-28 10:42:00 +01:00
parent e3fa75cadc
commit 44080b2fae
2 changed files with 58 additions and 8 deletions

View File

@ -1132,6 +1132,52 @@ def test_form_edit_item_field(pub):
assert resp.location == 'http://example.net/backoffice/forms/1/fields/#itemId_1'
assert FormDef.get(1).fields[0].items == ['XXX']
def test_form_edit_items_field(pub):
create_superuser(pub)
create_role()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [fields.ItemsField(id='1', label='1st field', type='items')]
formdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/1/')
resp = resp.click(href='fields/')
assert '1st field' in resp.body
resp = resp.click('Edit', href='1/')
assert resp.forms[0]['label'].value == '1st field'
resp.forms[0]['label'] = 'changed field'
resp.forms[0]['required'] = False
resp = resp.forms[0].submit('items$add_element')
# this adds a second field
assert 'items$element0' in resp.form.fields
assert 'items$element1' in resp.form.fields
# but don't fill anything
resp = resp.forms[0].submit('submit')
assert resp.location == 'http://example.net/backoffice/forms/1/fields/#itemId_1'
resp = resp.follow()
assert FormDef.get(1).fields[0].label == 'changed field'
assert FormDef.get(1).fields[0].required == False
assert FormDef.get(1).fields[0].items is None
# edit and fill with one item
resp = resp.click('Edit', href='1/')
assert resp.forms[0]['label'].value == 'changed field'
resp.forms[0]['items$element0'] = 'XXX'
resp = resp.forms[0].submit('submit')
assert resp.location == 'http://example.net/backoffice/forms/1/fields/#itemId_1'
assert FormDef.get(1).fields[0].items == ['XXX']
# check prefilling is only possible with Python
resp = resp.follow()
resp = resp.click('Edit', href='1/')
assert resp.forms[0]['prefill$type'].options == [
(u'None', True, u'None'), (u'Formula (Python)', False, u'Formula (Python)')]
def test_form_edit_page_field(pub):
create_superuser(pub)
create_role()

View File

@ -37,7 +37,7 @@ import portfolio
class PrefillSelectionWidget(CompositeWidget):
def __init__(self, name, value = None, **kwargs):
def __init__(self, name, value=None, field=None, **kwargs):
CompositeWidget.__init__(self, name, value, **kwargs)
if not value:
@ -49,8 +49,13 @@ class PrefillSelectionWidget(CompositeWidget):
('user', _('User Field')),
('geolocation', _('Geolocation')),]
if kwargs.get('map'):
options = [('none', _('None')), ('geolocation', _('Geolocation')),]
if field and field.type == 'items':
# limit choices to python as items field are prefilled with list
# of strings
options = [x for x in options if x[0] in ('none', 'formula')]
elif field and field.type == 'map':
# limit choices to geolocation
options = [x for x in options if x[0] in ('none', 'geolocation')]
self.add(SingleSelectWidget, 'type', options=options, value=value.get('type'),
attrs={'data-dynamic-display-parent': 'true'})
@ -88,7 +93,8 @@ class PrefillSelectionWidget(CompositeWidget):
attrs={'data-dynamic-display-child-of': 'prefill$type',
'data-dynamic-display-value': prefill_types.get('user')})
if kwargs.get('map'):
if field and field.type == 'map':
# different prefilling sources on map fields
geoloc_fields = [('position', _('Position'))]
else:
geoloc_fields = [
@ -333,7 +339,6 @@ class WidgetField(Field):
in_listing = True
extra_attributes = []
prefill = {}
prefill_kwargs = {}
widget_class = None
@ -408,9 +413,9 @@ class WidgetField(Field):
# check if the apply button has been clicked, and get the field out
# of the "advanced parameters" section if that's the case.
prefill_in_advanced = False
form.add(PrefillSelectionWidget, 'prefill', title = _('Prefill'),
form.add(PrefillSelectionWidget, 'prefill', title=_('Prefill'),
value=self.prefill, advanced=prefill_in_advanced,
**self.prefill_kwargs)
field=self)
if 'anonymise' in self.get_admin_attributes():
# override anonymise flag default value
form.add(CheckboxWidget, 'anonymise', title=_('Anonymise'),
@ -1851,7 +1856,6 @@ class MapField(WidgetField):
widget_class = MapWidget
extra_attributes = ['initial_zoom', 'min_zoom', 'max_zoom',
'default_position', 'init_with_geoloc']
prefill_kwargs = {'map': True}
def fill_admin_form(self, form):
WidgetField.fill_admin_form(self, form)