Projet

Général

Profil

0001-forms-use-a-template-to-render-checkboxes-widgets-23.patch

Anonyme, 07 mai 2018 20:32

Télécharger (5,65 ko)

Voir les différences:

Subject: [PATCH] forms: use a template to render checkboxes widgets (#23413)

 tests/test_widgets.py                         | 30 ++++++++++++
 wcs/qommon/form.py                            | 47 ++++++++++---------
 .../qommon/forms/widgets/checkboxes.html      | 18 +++++++
 3 files changed, 73 insertions(+), 22 deletions(-)
 create mode 100644 wcs/qommon/templates/qommon/forms/widgets/checkboxes.html
tests/test_widgets.py
2 2
import sys
3 3
import shutil
4 4
import copy
5
import collections
5 6

  
6 7
from quixote import cleanup
7 8
from quixote.http_request import parse_query
......
412 413
    assert widget.parse() == ['pear', 'peach']
413 414

  
414 415

  
416
def test_checkboxes_widget_submission_with_options():
417
    test_dt = datetime.datetime.now()
418
    widget = CheckboxesWidget('test',
419
        options_with_attributes=[
420
            ('apple', 'Apple', 'apple', {'datetime': test_dt}),
421
            ('pear', 'Pear', 'pear', {'datetime': test_dt}),
422
            ('peach', 'Peach', 'peach', {'datetime': test_dt})])
423
    mock_form_submission(req, widget, {'test$elementpeach': ['yes'], 'test$elementpear': ['yes']})
424
    assert widget.parse() == ['pear', 'peach']
425

  
426

  
427
def test_checkboxes_get_options():
428
    test_dt = datetime.datetime.now()
429
    any_info = 'any extra info'
430
    widget = CheckboxesWidget('test',
431
        options_with_attributes=[
432
            ('apple', 'Apple', 'apple', {'datetime': test_dt, 'any_info': any_info}),
433
            ('pear', 'Pear', 'pear', {'datetime': test_dt}),
434
            ('peach', 'Peach', 'peach', {'datetime': test_dt})])
435
    assert isinstance(widget.get_options(), collections.Iterator)
436
    iter_options = list(widget.get_options())
437
    assert len(iter_options) == 3
438
    for option in iter_options:
439
        assert isinstance(option, dict)
440
        assert isinstance(option['options'], dict)
441
        assert option['options']['datetime'] == test_dt
442

  
443
    assert iter_options[0]['options']['any_info'] == any_info
444

  
415 445
def test_composite_widget():
416 446
    widget = CompositeWidget('compotest')
417 447
    widget.add(StringWidget, name='str1')
wcs/qommon/form.py
1109 1109
    def has_error(self, request=None):
1110 1110
        return Widget.has_error(self, request=request)
1111 1111

  
1112
    def render_content(self):
1113
        r = TemplateIO(html=True)
1114
        if self.inline:
1115
            r += htmltext('<ul class="inline">')
1116
        else:
1117
            r += htmltext('<ul>')
1118
        for widget in self.get_widgets():
1119
            if widget.attrs and 'disabled' in widget.attrs:
1120
                r += htmltext('<li class="disabled"><label>')
1121
            else:
1122
                r += htmltext('<li><label>')
1123
            if self.readonly:
1124
                widget.attrs['disabled'] = 'disabled'
1125
                if widget.value:
1126
                    r += htmltext('<input type="hidden" name="%s" value="yes" >') % widget.name
1127
                widget.name = widget.name + 'xx'
1128
            r += widget.render_content()
1129
            r += htmltext('<span>%s</span>') % widget.title
1130
            r += htmltext('</label>')
1131
            r += htmltext('</li>')
1132
        r += htmltext('</ul>')
1133
        return r.getvalue()
1112
    def get_options(self):
1113
        '''yield dict with options containing widget data and extra informations
1114
        '''
1115
        for idx, widget in enumerate(self.get_widgets()):
1116
            attrs = {'id': 'form_' + widget.name}
1117
            if widget.required:
1118
                attrs['aria-required'] = 'true'
1119

  
1120
            if widget.attrs:
1121
                attrs.update(widget.attrs)
1122

  
1123
            option = {
1124
                'name': widget.name,
1125
                'value': 'yes',
1126
                'title': widget.title,
1127
                'attrs': attrs,
1128
                'checked': True if widget.value else False,
1129
            }
1130
            if widget.name in self.disabled_options:
1131
                option['disabled'] = 'disabled'
1132
            if self.options_with_attributes:
1133
                option['options'] = self.options_with_attributes[idx][-1]
1134

  
1135
            yield option
1136

  
1134 1137

  
1135 1138
class ValidatedStringWidget(StringWidget):
1136 1139
    '''StringWidget which checks the value entered is correct according to a regex'''
wcs/qommon/templates/qommon/forms/widgets/checkboxes.html
1
{% extends "qommon/forms/widget.html" %}
2
{% block widget-control %}
3
<ul {% if widget.inline %}class="inline"{% endif %}>
4
  {% for option in widget.get_options %}
5
    <li {% if "disabled" in option %}class="disabled"{% endif %}>
6
      <label>
7
        {% if widget.readonly or "disabled" in option %}
8
        <input type="hidden" name="{{option.name}}" value="yes" >
9
        {% endif %}
10
        <input type="checkbox" value="yes" name="{{option.name}}{% if widget.readonly %}xx{% endif %}"
11
        {% if widget.readonly or "disabled" in option %}disabled{% endif %}
12
        {% if option.checked %}checked="checked"{% endif %}
13
        {% for key, value in option.attrs.items %}{{key}}="{{value}}"{% endfor %}
14
        ><span>{{ option.title }}</span></label>
15
    </li>
16
  {% endfor %}
17
</ul>
18
{% endblock %}
0
-