Projet

Général

Profil

0001-forms-use-a-template-to-render-radio-buttons-27996.patch

Frédéric Péters, 14 novembre 2018 15:23

Télécharger (4,54 ko)

Voir les différences:

Subject: [PATCH] forms: use a template to render radio buttons (#27996)

 tests/test_form_pages.py                      |  4 +-
 wcs/qommon/form.py                            | 37 +++++--------------
 .../qommon/forms/widgets/radiobuttons.html    | 12 ++++++
 3 files changed, 23 insertions(+), 30 deletions(-)
 create mode 100644 wcs/qommon/templates/qommon/forms/widgets/radiobuttons.html
tests/test_form_pages.py
4489 4489

  
4490 4490
    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
4491 4491
    assert resp.form['f0'].value == 'foo@localhost'
4492
    assert 'readonly' in resp.form['f0'].attrs
4492
    assert 'disabled' in resp.form['f0'].attrs
4493 4493
    for radio in resp.html.findAll('input'):
4494 4494
        if radio['name'] == 'f0':
4495 4495
            if radio['value'] == 'foo@localhost':
4496 4496
                assert radio.attrs.get('checked')
4497
                assert radio.attrs.get('readonly')
4498 4497
                assert not radio.attrs.get('disabled')
4499 4498
            else:
4500 4499
                assert not radio.attrs.get('checked')
4501
                assert radio.attrs.get('readonly')
4502 4500
                assert radio.attrs.get('disabled')
4503 4501

  
4504 4502
    resp.form['f0'].value = 'baz@localhost' # try changing the value
wcs/qommon/form.py
198 198

  
199 199

  
200 200
class RadiobuttonsWidget(quixote.form.RadiobuttonsWidget):
201
    template_name = 'qommon/forms/widgets/radiobuttons.html'
202

  
201 203
    def __init__(self, name, value=None, **kwargs):
202 204
        self.options_with_attributes = kwargs.pop('options_with_attributes', None)
203 205
        super(RadiobuttonsWidget, self).__init__(name, value=value, **kwargs)
204 206

  
205
    def render_content(self):
206
        include_disabled = False
207
        options = self.options[:]
208
        if self.options_with_attributes:
209
            options = self.options_with_attributes
210
            include_disabled = True
211

  
212
        r = TemplateIO(html=True)
207
    def get_options(self):
208
        options = self.options_with_attributes or self.options
213 209
        for i, option in enumerate(options):
214 210
            object, description, key = option[:3]
215
            r += htmltext('<label>')
216
            html_attrs = self.attrs.copy()
217
            if self.is_selected(object):
218
                html_attrs['checked'] = 'checked'
219
            elif self.attrs.get('readonly'):
220
                html_attrs['disabled'] = 'disabled'
221
            if self.options_with_attributes and option[-1].get('disabled'):
222
                html_attrs['disabled'] = 'disabled'
223
                label_tag = htmltext('<label class="disabled">')
224
            r += htmltag("input", xml_end=True,
225
                        type="radio",
226
                        name=self.name,
227
                        value=key,
228
                        **html_attrs)
229
            r += htmltext('<span>%s</span>') % description
230
            r += htmltext('</label>')
231
            if i != len(options) - 1:
232
                r += htmltext(self.delim)
233
        return r.getvalue()
211
            yield {
212
                'value': key,
213
                'label': description,
214
                'disabled': bool(self.options_with_attributes and option[-1].get('disabled')),
215
                'selected': self.is_selected(object)
216
            }
234 217

  
235 218
def checkbox_render_content(self, standalone=True):
236 219
    attrs = {'id': 'form_' + self.name}
wcs/qommon/templates/qommon/forms/widgets/radiobuttons.html
1
{% extends "qommon/forms/widget.html" %}
2

  
3
{% block widget-control %}
4
{% for option in widget.get_options %}
5
 <label {% if option.disabled %}class="disabled"{% endif %}><input
6
   {% if option.selected %}checked="checked"
7
   {% elif widget.readonly or option.disabled %}disabled="disabled"{% endif %}
8
   type="radio" name="{{ widget.name }}"
9
   value="{{ option.value }}"><span>{{ option.label }}</span></label>
10
   {% if not forloop.last %}{{ widget.delim|safe }}{% endif %}
11
{% endfor %}
12
{% endblock %}
0
-