From 9e4e0f9692283e3f8e6627929f98f7afac4023a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 9 Sep 2015 13:40:52 +0200 Subject: [PATCH] forms: always add attributes to prefilled fields (#8223) --- tests/test_form_pages.py | 41 +++++++++++++++++++++++++++++++++++++++++ wcs/forms/root.py | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py index 6f21db6..04662cf 100644 --- a/tests/test_form_pages.py +++ b/tests/test_form_pages.py @@ -1085,3 +1085,44 @@ def test_formdata_form_file_download(pub): # go back to the status page, this will exercise the substitution variables # codepath. resp = resp.follow() + +def test_form_map_field_back_and_submit(pub): + formdef = create_formdef() + formdef.fields = [ + fields.MapField(id='0', label='map'), + fields.StringField(id='1', label='street', required=False, + prefill={'type': 'geolocation', 'value': 'road'}), + ] + formdef.store() + resp = get_app(pub).get('/test/') + formdef.data_class().wipe() + assert 'qommon.map.js' in resp.body + assert 'qommon.geolocation.js' in resp.body + # with a real user interaction this would get set by javascript + resp.forms[0]['f0$latlng'] = '1.234;-1.234' + assert 'data-geolocation="road"' in resp.body + + # check summary page + resp = resp.forms[0].submit('submit') + assert 'Check values then click submit.' in resp.body + assert 'data-init-lng="-1.234"' in resp.body + assert 'data-init-lat="1.234"' in resp.body + + # get back to the map field + resp = resp.forms[0].submit('previous') + # check the field is still marked as holding the road + assert 'data-geolocation="road"' in resp.body + assert resp.forms[0]['f0$latlng'].value == '1.234;-1.234' + + # back to summary page + resp = resp.forms[0].submit('submit') + + # and submitting the form + resp = resp.forms[0].submit('submit') + assert resp.status_int == 302 + resp = resp.follow() + assert 'The form has been recorded' in resp.body + assert formdef.data_class().count() == 1 + data_id = formdef.data_class().select()[0].id + data = formdef.data_class().get(data_id) + assert data.data == {'1': None, '0': '1.234;-1.234'} diff --git a/wcs/forms/root.py b/wcs/forms/root.py index 913cd75..7f34109 100644 --- a/wcs/forms/root.py +++ b/wcs/forms/root.py @@ -388,6 +388,10 @@ class FormPage(Directory): form.get_widget('f%s' % k).set_message( _('Value has been automatically prefilled.')) form.get_widget('f%s' % k).prefilled = True + + if field.prefill: + # always set additional attributes as they will be used for + # "live prefill", regardless of existing data. form.get_widget('f%s' % k).prefill_attributes = field.get_prefill_attributes() if not prefilled and form.get_widget('f%s' % k): -- 2.5.1