From e1131e667e29588bb5355735e49ed4deb441d163 Mon Sep 17 00:00:00 2001 From: Elias Showk Date: Wed, 2 May 2018 11:10:07 +0200 Subject: [PATCH] add Checkboxeswidget.get_options and tests (#23413) --- tests/test_widgets.py | 17 +++++++++++++++++ wcs/qommon/form.py | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/tests/test_widgets.py b/tests/test_widgets.py index b58e6a32..1e560fb9 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -2,6 +2,7 @@ import datetime import sys import shutil import copy +import collections from quixote import cleanup from quixote.http_request import parse_query @@ -411,6 +412,22 @@ def test_checkboxes_widget(): mock_form_submission(req, widget, {'test$elementpeach': ['yes'], 'test$elementpear': ['yes']}) assert widget.parse() == ['pear', 'peach'] +def test_checkboxes_widget_options(): + test_dt = datetime.datetime.now() + widget = CheckboxesWidget('test', + options_with_attributes=[('apple', 'Apple', 'apple', {'datetime': test_dt}), ('pear', 'Pear', 'pear', {'datetime': test_dt}), ('peach', 'Peach', 'peach', {'datetime': test_dt})]) + assert isinstance(widget.get_options(), collections.Iterator) + iter_options = list(widget.get_options()) + assert len(iter_options) > 0 + for option in iter_options: + attributes = option['attrs'] + assert attributes.get('value', False) + assert attributes.get('name', False) + assert option.get('description', False) + assert option.get('attrs', False) + assert option.get('options', False) + assert option['options']['datetime'] == test_dt + def test_composite_widget(): widget = CompositeWidget('compotest') diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 49df467e..4981bf55 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1109,6 +1109,31 @@ class CheckboxesWidget(CompositeWidget): def has_error(self, request=None): return Widget.has_error(self, request=request) + def get_options(self): + options = [] + tags = [] + + if self.options_with_attributes: + options = self.options_with_attributes[:] + + for option in options: + name, description, key = option[:3] + html_attrs = {} + html_attrs['value'] = key + html_attrs['name'] = name + if self.options_with_attributes and option[-1].get('disabled'): + html_attrs['disabled'] = 'disabled' + if self.options_with_attributes and option[-1].get('checked'): + html_attrs['checked'] = 'checked' + if description is None: + description = '' + + yield { + 'description': description, + 'attrs': html_attrs, + 'options': option[-1] if self.options_with_attributes else None + } + def render_content(self): r = TemplateIO(html=True) if self.inline: -- 2.17.0