Projet

Général

Profil

0001-add-CheckboxesWidget.get_options-and-checkboxes.html.patch

Anonyme, 03 mai 2018 15:11

Télécharger (4,99 ko)

Voir les différences:

Subject: [PATCH] add CheckboxesWidget.get_options and checkboxes.html (#23413)

 tests/test_widgets.py                         | 26 ++++++++++++++++
 wcs/qommon/form.py                            | 30 +++++--------------
 .../qommon/forms/widgets/checkboxes.html      | 18 +++++++++++
 3 files changed, 52 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
......
411 412
    mock_form_submission(req, widget, {'test$elementpeach': ['yes'], 'test$elementpear': ['yes']})
412 413
    assert widget.parse() == ['pear', 'peach']
413 414

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

  
425
def test_checkboxes_widget_get_options():
426
    test_dt = datetime.datetime.now()
427
    widget = CheckboxesWidget('test',
428
        options_with_attributes=[
429
            ('apple', 'Apple', 'apple', {'datetime': test_dt}),
430
            ('pear', 'Pear', 'pear', {'datetime': test_dt}),
431
            ('peach', 'Peach', 'peach', {'datetime': test_dt})])
432
    assert isinstance(widget.get_options(), collections.Iterator)
433
    iter_options = list(widget.get_options())
434
    assert len(iter_options) == 3
435
    for widget in iter_options:
436
        assert isinstance(widget, CheckboxWidget)
437
        assert isinstance(widget.extra_options, dict)
438
        assert widget.extra_options['datetime'] == test_dt
439

  
414 440

  
415 441
def test_composite_widget():
416 442
    widget = CompositeWidget('compotest')
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 every widget with extra information
1114
        '''
1115
        tags = []
1116
        for idx, widget in enumerate(self.get_widgets()):
1117
            widget.extra_options = self.options_with_attributes[idx][-1] if self.options_with_attributes else None
1118
            yield widget
1119

  
1134 1120

  
1135 1121
class ValidatedStringWidget(StringWidget):
1136 1122
    '''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 w in widget.get_options %}
5
    <li {% if "disabled" in w %}class="disabled"{% endif %}>
6
      <label>
7
        {% if widget.readonly or "disabled" in w %}
8
        <input type="hidden" name="{{w.name}}" value="yes" >
9
        {% endif %}
10
        <input type="checkbox" value="yes" name="{{w.name}}"
11
           {% if widget.readonly or "disabled" in w %}disabled{% endif %}
12
           {% if w.value %}checked="checked"{% endif %}
13
           {% for key, value in w.attrs.items %}{{key}}="{{value}}"{% endfor %}/>
14
        <span>{{ w.title }}</span></label>
15
    </li>
16
  {% endfor %}
17
</ul>
18
{% endblock %}
0
-