From 8706cc6c69122997cd3a03ac4de71a33706b86ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 25 Jul 2017 11:16:39 +0200 Subject: [PATCH 2/2] forms: use a template to render select widgets (#17964) --- wcs/qommon/form.py | 32 ++++++++-------------- .../templates/qommon/forms/widgets/select.html | 16 +++++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 wcs/qommon/templates/qommon/forms/widgets/select.html diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 3608f6c4..602adcd7 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1483,6 +1483,8 @@ class CheckboxesTableWidget(TableWidget): class SingleSelectHintWidget(SingleSelectWidget): + template_name = 'qommon/forms/widgets/select.html' + def __init__(self, name, value=None, **kwargs): self.options_with_attributes = kwargs.pop('options_with_attributes', None) super(SingleSelectHintWidget, self).__init__(name, value=value, **kwargs) @@ -1490,23 +1492,15 @@ class SingleSelectHintWidget(SingleSelectWidget): def separate_hint(self): return (self.hint and len(self.hint) > 80) - def render_content(self): - attrs = {'id': 'form_' + self.name} - if self.attrs: - attrs.update(self.attrs) - tags = [htmltag('select', name=self.name, **attrs)] - options = self.options[:] - include_disabled = False + def get_options(self): if self.options_with_attributes: - options = self.options_with_attributes - include_disabled = True - if not self.separate_hint() and self.hint: - r = htmltag('option', value='', selected=None) - tags.append(r + htmlescape(self.hint) + htmltext('')) - if self.options[0][0] is None: - # hint has been put as first element, skip the default empty - # value. - options = self.options[1:] + options = self.options_with_attributes[:] + else: + options = self.options[:] + if options[0][0] is None: + options = self.options[1:] + + tags = [] for option in options: object, description, key = option[:3] html_attrs = {} @@ -1517,10 +1511,8 @@ class SingleSelectHintWidget(SingleSelectWidget): html_attrs['disabled'] = 'disabled' if description is None: description = '' - r = htmltag('option', **html_attrs) - tags.append(r + htmlescape(description) + htmltext('')) - tags.append(htmltext('')) - return htmltext('\n').join(tags) + yield {'description': description, 'attrs': html_attrs, + 'options': option[-1] if self.options_with_attributes else None} def get_hint(self): if self.separate_hint(): diff --git a/wcs/qommon/templates/qommon/forms/widgets/select.html b/wcs/qommon/templates/qommon/forms/widgets/select.html new file mode 100644 index 00000000..cfe86188 --- /dev/null +++ b/wcs/qommon/templates/qommon/forms/widgets/select.html @@ -0,0 +1,16 @@ +{% extends "qommon/forms/widget.html" %} +{% block widget-control %} + +{% endblock %} -- 2.14.1