From 2c2cf65735a58d97701eed1997d5b71ad5c9472f Mon Sep 17 00:00:00 2001 From: Elias Showk Date: Wed, 25 Apr 2018 16:26:34 +0200 Subject: [PATCH] add CheckboxesWidget.get_options and tests (#23403) --- tests/test_widgets.py | 13 ++++++++++++- wcs/qommon/form.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/test_widgets.py b/tests/test_widgets.py index b58e6a32..a4f2b62d 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 @@ -407,9 +408,19 @@ def test_select_or_other_widget(): def test_checkboxes_widget(): widget = CheckboxesWidget('test', - options=[('apple', 'Apple', 'apple'), ('pear', 'Pear', 'pear'), ('peach', 'Peach', 'peach')]) + options=[('apple', 'Apple', 'apple', {'datetime': datetime.datetime.now()}), ('pear', 'Pear', 'pear'), ('peach', 'Peach', 'peach')], + options_with_attributes=[('apple', 'Apple', 'apple', {'datetime': datetime.datetime.now()}), ('pear', 'Pear', 'pear', {'datetime': datetime.datetime.now()}), ('peach', 'Peach', 'peach', {'datetime': datetime.datetime.now()})]) mock_form_submission(req, widget, {'test$elementpeach': ['yes'], 'test$elementpear': ['yes']}) assert widget.parse() == ['pear', 'peach'] + assert isinstance(widget.get_options(), collections.Iterator) + iter_options = list(widget.get_options()) + assert len(iter_options) > 0 + for option in iter_options: + assert option.get('description', False) + assert option.get('key', False) + assert option.get('attrs', False) + assert option.get('options', False) + assert isinstance(option['options']['datetime'], datetime.datetime) def test_composite_widget(): diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 4e02888e..f9bd9ce4 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: + object, description, key = option[:3] + html_attrs = {} + html_attrs['value'] = key + 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'] = 'true' + if description is None: + description = '' + + yield { + 'description': description, + 'key': key, + 'attrs': html_attrs, + 'options': option[-1] if self.options_with_attributes else None + } + class ValidatedStringWidget(StringWidget): '''StringWidget which checks the value entered is correct according to a regex''' -- 2.17.0