From 4bee785ba559418d568676088338d7d50195b0c7 Mon Sep 17 00:00:00 2001 From: Elias Showk Date: Fri, 4 May 2018 18:58:01 +0200 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 diff --git a/tests/test_widgets.py b/tests/test_widgets.py index b58e6a32..ab7dd999 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 @@ -412,6 +413,35 @@ def test_checkboxes_widget(): assert widget.parse() == ['pear', 'peach'] +def test_checkboxes_widget_submission_with_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})]) + mock_form_submission(req, widget, {'test$elementpeach': ['yes'], 'test$elementpear': ['yes']}) + assert widget.parse() == ['pear', 'peach'] + + +def test_checkboxes_get_options(): + test_dt = datetime.datetime.now() + any_info = 'any extra info' + widget = CheckboxesWidget('test', + options_with_attributes=[ + ('apple', 'Apple', 'apple', {'datetime': test_dt, 'any_info': any_info}), + ('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) == 3 + for option in iter_options: + assert isinstance(option, dict) + assert isinstance(option['options'], dict) + assert option['options']['datetime'] == test_dt + + assert iter_options[0]['options']['any_info'] == any_info + def test_composite_widget(): widget = CompositeWidget('compotest') widget.add(StringWidget, name='str1') diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 49df467e..45176c59 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1109,28 +1109,31 @@ class CheckboxesWidget(CompositeWidget): def has_error(self, request=None): return Widget.has_error(self, request=request) - def render_content(self): - r = TemplateIO(html=True) - if self.inline: - r += htmltext('