From 0ae72dbbb993338a68d9e1e6be7a5f8f296f038b Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 9 May 2019 18:51:56 +0200 Subject: [PATCH] forms: replaces RadioSelect by Select of locked attributes (#32954) --- src/authentic2/forms/profile.py | 13 ++++++++++++- tests/test_profile.py | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/authentic2/forms/profile.py b/src/authentic2/forms/profile.py index 5dd81a0d..cc2e00cb 100644 --- a/src/authentic2/forms/profile.py +++ b/src/authentic2/forms/profile.py @@ -81,7 +81,18 @@ class BaseUserForm(forms.ModelForm): for av in models.AttributeValue.objects.with_owner(instance): if av.attribute.name in self.declared_fields: if av.verified: - self.declared_fields[av.attribute.name].widget.attrs['readonly'] = 'readonly' + widget = self.declared_fields[av.attribute.name].widget + # RadioSelect are a special case, the HTML readonly attribute does not work on them + # we must replace them by a combo box, see #32954. + if isinstance(widget, forms.RadioSelect): + new_widget = self.declared_fields[av.attribute.name].widget = forms.Select() + try: + new_widget._choices = widget._choices + except AttributeError: + # dj18 compatibility + new_widget.choices = widget.choices + widget = new_widget + widget.attrs['readonly'] = 'readonly' initial[av.attribute.name] = av.to_python() super(BaseUserForm, self).__init__(*args, **kwargs) diff --git a/tests/test_profile.py b/tests/test_profile.py index 8482fa0a..1df28088 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -135,3 +135,23 @@ def test_account_edit_scopes(app, simple_user): resp = app.get(reverse('profile_edit_with_scope', kwargs={'scope': 'address'}), status=200) assert get_fields(resp) == set(['city', 'zipcode', 'next_url']) + + +def test_account_edit_locked_title(app, simple_user): + Attribute.objects.create( + name='title', label='title', + kind='title', user_visible=True, user_editable=True) + simple_user.attributes.title = 'Monsieur' + + utils.login(app, simple_user) + url = reverse('profile_edit') + response = app.get(url, status=200) + assert len(response.pyquery('input[type="radio"][name="edit-profile-title"]')) == 2 + assert len(response.pyquery('input[type="radio"][name="edit-profile-title"][readonly="true"]')) == 0 + assert len(response.pyquery('select[name="edit-profile-title"]')) == 0 + + simple_user.verified_attributes.title = 'Monsieur' + + response = app.get(url, status=200) + assert len(response.pyquery('input[type="radio"][name="edit-profile-title"][readonly=true]')) == 0 + assert len(response.pyquery('select[name="edit-profile-title"]')) == 1 -- 2.20.1