From d005f5fe7a68597e826604ceb978540ecc058d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 12 Jul 2018 14:02:52 +0200 Subject: [PATCH] forms: mark unset radio options as disabled in readonly mode (#25246) --- tests/test_form_pages.py | 35 +++++++++++++++++++++++++++++++++++ wcs/qommon/form.py | 2 ++ 2 files changed, 37 insertions(+) diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py index 68ba22a5..cdb95c35 100644 --- a/tests/test_form_pages.py +++ b/tests/test_form_pages.py @@ -4356,6 +4356,41 @@ def test_form_page_profile_verified_prefill(pub): assert not 'Check values then click submit.' in resp.body assert resp.form['f0'].value == 'foo@localhost' +def test_form_page_profile_verified_radio_item_prefill(pub): + user = create_user(pub) + formdef = create_formdef() + formdef.data_class().wipe() + formdef.fields = [fields.ItemField(id='0', label='item', type='item', + items=['bar@localhost', 'foo@localhost', 'baz@localhost'], + show_as_radio=True, + prefill={'type': 'user', 'value': 'email'})] + formdef.store() + + resp = get_app(pub).get('/test/') + assert resp.form['f0'].value is None + + user.verified_fields = ['email'] + user.store() + + resp = login(get_app(pub), username='foo', password='foo').get('/test/') + assert resp.form['f0'].value == 'foo@localhost' + assert 'readonly' in resp.form['f0'].attrs + for radio in resp.html.findAll('input'): + if radio['name'] == 'f0': + if radio['value'] == 'foo@localhost': + assert radio.attrs.get('checked') + assert radio.attrs.get('readonly') + assert not radio.attrs.get('disabled') + else: + assert not radio.attrs.get('checked') + assert radio.attrs.get('readonly') + assert radio.attrs.get('disabled') + + resp.form['f0'].value = 'baz@localhost' # try changing the value + resp = resp.form.submit('submit') + assert 'Check values then click submit.' in resp.body + assert resp.form['f0'].value == 'foo@localhost' # it is reverted + def test_item_field_with_disabled_items(http_requests, pub): user = create_user(pub) formdef = create_formdef() diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 44859dda..43a95da2 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -207,6 +207,8 @@ class RadiobuttonsWidget(quixote.form.RadiobuttonsWidget): html_attrs = self.attrs.copy() if self.is_selected(object): html_attrs['checked'] = 'checked' + elif self.attrs.get('readonly'): + html_attrs['disabled'] = 'disabled' if self.options_with_attributes and option[-1].get('disabled'): html_attrs['disabled'] = 'disabled' label_tag = htmltext('