summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLauréline Guérin <zebuline@entrouvert.com>2021-10-15 09:10:56 (GMT)
committerLauréline Guérin <zebuline@entrouvert.com>2021-11-26 15:02:20 (GMT)
commitaf5b8e59f901c831d995c235a94b451a13853bcc (patch)
tree2e3af163e82769884b04b9082f551adb2fc98657
parent9af4b3952fea2b2234e49e6d57ec502a42116c5a (diff)
downloadwcs-main.zip
wcs-main.tar.gz
wcs-main.tar.bz2
fields: fix live prefill for item fields with carddef datasource (#57877)HEADwip/57877-prefill-itemmain
-rw-r--r--tests/form_pages/test_live.py84
-rw-r--r--wcs/forms/common.py6
-rw-r--r--wcs/qommon/static/js/qommon.forms.js5
3 files changed, 95 insertions, 0 deletions
diff --git a/tests/form_pages/test_live.py b/tests/form_pages/test_live.py
index ed69240..3c1c543 100644
--- a/tests/form_pages/test_live.py
+++ b/tests/form_pages/test_live.py
@@ -926,6 +926,90 @@ def test_field_live_date_prefill(pub, http_requests):
assert live_resp.json['result']['2'] == {'visible': True}
+def test_field_live_item_datasource_carddef_prefill(pub, http_requests):
+ CardDef.wipe()
+
+ carddef_related = CardDef()
+ carddef_related.name = 'bar'
+ carddef_related.digest_templates = {'default': '{{ form_var_bar }}'}
+ carddef_related.fields = [
+ fields.StringField(id='1', type='string', label='string', varname='bar'),
+ ]
+ carddef_related.store()
+ carddef_related.data_class().wipe()
+ for value in ['A', 'B', 'C']:
+ carddata = carddef_related.data_class()()
+ carddata.data = {
+ '1': value,
+ }
+ carddata.just_created()
+ carddata.store()
+
+ carddef = CardDef()
+ carddef.name = 'foo'
+ carddef.digest_templates = {'default': '{{ form_var_foo }}'}
+ carddef.fields = [
+ fields.StringField(id='1', type='string', label='string', varname='foo'),
+ fields.ItemField(
+ type='item',
+ id='2',
+ varname='item',
+ data_source={'type': 'carddef:bar'},
+ ),
+ ]
+ carddef.store()
+ carddef.data_class().wipe()
+ carddata1 = carddef.data_class()()
+ carddata1.data = {
+ '1': 'bar',
+ '2': '1',
+ '2_display': 'A',
+ }
+ carddata1.just_created()
+ carddata1.store()
+ carddata2 = carddef.data_class()()
+ carddata2.data = {
+ '1': 'baz',
+ '2': '2',
+ '2_display': 'B',
+ }
+ carddata2.just_created()
+ carddata2.store()
+
+ FormDef.wipe()
+ formdef = FormDef()
+ formdef.name = 'Foo'
+ formdef.fields = [
+ fields.ItemField(
+ type='item', id='1', label='foo', varname='foo', data_source={'type': 'carddef:foo'}
+ ),
+ fields.ItemField(
+ type='item',
+ id='2',
+ label='item',
+ varname='item',
+ prefill={'type': 'string', 'value': '{{ form_var_foo_live_var_item }}'},
+ data_source={'type': 'carddef:bar'},
+ ),
+ ]
+ formdef.store()
+ formdef.data_class().wipe()
+
+ app = get_app(pub)
+ resp = app.get('/foo/')
+ assert resp.html.find('div', {'data-field-id': '1'}).attrs['data-live-source'] == 'true'
+ assert resp.pyquery('#var_item.widget-prefilled') # second field is marked as prefilled
+ assert resp.form['f2'].value == '1'
+ resp.form['f1'] = str(carddata2.id)
+ live_resp = app.post('/foo/live?modified_field_id=1&prefilled_2=on', params=resp.form.submit_fields())
+ assert live_resp.json['result']['2'] == {'visible': True, 'content': '2'}
+
+ resp.form['f2'] = '3' # manually changed -> widget-prefilled class will be removed
+ resp.form['f1'] = str(carddata1.id)
+ live_resp = app.post('/foo/live?modified_field_id=1', params=resp.form.submit_fields())
+ assert live_resp.json['result']['2'] == {'visible': True}
+
+
def test_field_live_block_string_prefill(pub, http_requests):
FormDef.wipe()
BlockDef.wipe()
diff --git a/wcs/forms/common.py b/wcs/forms/common.py
index 74281d6..1071caf 100644
--- a/wcs/forms/common.py
+++ b/wcs/forms/common.py
@@ -807,6 +807,12 @@ class FormStatusPage(Directory, FormTemplateMixin):
value = field.get_json_value(value)
except ValueError:
pass
+ elif field.key == 'item':
+ for option in field.get_options():
+ # get raw value from display value
+ if option[1] == value:
+ value = option[0]
+ break
entry['content'] = value
elif field.prefill and field.prefill.get('type') == 'user':
update_prefill = bool(get_request().form.get('modified_field_id') == 'user')
diff --git a/wcs/qommon/static/js/qommon.forms.js b/wcs/qommon/static/js/qommon.forms.js
index 991ff0d..4c3aad1 100644
--- a/wcs/qommon/static/js/qommon.forms.js
+++ b/wcs/qommon/static/js/qommon.forms.js
@@ -484,6 +484,11 @@ $(function() {
// replace checkbox input value
$widget.find('input[type=checkbox]').prop('checked', value.content);
}
+ // replace select value
+ $(widget).find('select').val(value.content);
+ // replace radio value
+ $(widget).find('input[type=radio]').prop('checked', false);
+ $(widget).find('input[type=radio][value="'+value.content+'"]').prop('checked', true);
}
}
});