misc: add optgroup in SingleSelectWidget (#48283)

This commit is contained in:
Lauréline Guérin 2020-11-13 17:29:38 +01:00
parent a371c57f4b
commit d01b3c2ce9
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 73 additions and 5 deletions

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import datetime
import sys
import shutil
import copy
@ -11,7 +10,6 @@ import mechanize
import pytest
from wcs import publisher
from wcs.qommon import sessions
from wcs.qommon.form import *
from wcs.qommon.http_request import HTTPRequest
@ -438,7 +436,7 @@ def test_wysiwygwidget():
wcs.qommon.form._sanitizeHTML = sanitize_html
def test_select_widget():
def test_select_hint_widget():
widget = SingleSelectHintWidget('test',
options=[
('apple', 'Apple', 'apple'),
@ -492,6 +490,59 @@ def test_select_widget():
assert 'readonly="readonly"' not in str(widget.render())
def test_select_widget():
# test with optgroups
widget = SingleSelectWidget(
'test',
options=[
OptGroup('foo'),
('apple', 'Apple', 'apple'),
OptGroup('bar'),
('pear', 'Pear', 'pear'),
('peach', 'Peach', 'peach')
]
)
assert widget.get_allowed_values() == ['apple', 'pear', 'peach']
assert (
'<optgroup label="foo">'
'<option value="apple">Apple</option>'
'</optgroup>'
'<optgroup label="bar">'
'<option value="pear">Pear</option>'
'<option value="peach">Peach</option>'
'</optgroup>') in ''.join(p.strip() for p in str(widget.render()).split('\n'))
# first option is not optgroup
widget = SingleSelectWidget(
'test',
options=[
('apple', 'Apple', 'apple'),
OptGroup('bar'),
('pear', 'Pear', 'pear'),
('peach', 'Peach', 'peach')
]
)
assert widget.get_allowed_values() == ['apple', 'pear', 'peach']
assert (
'<option value="apple">Apple</option>'
'<optgroup label="bar">'
'<option value="pear">Pear</option>'
'<option value="peach">Peach</option>'
'</optgroup>') in ''.join(p.strip() for p in str(widget.render()).split('\n'))
# only one optgroup and no options
widget = SingleSelectWidget(
'test',
options=[
OptGroup('bar'),
]
)
assert widget.get_allowed_values() == []
assert (
'<optgroup label="bar">'
'</optgroup>') in ''.join(p.strip() for p in str(widget.render()).split('\n'))
def test_select_or_other_widget():
widget = SingleSelectWidgetWithOther('test',
options=[('apple', 'Apple'), ('pear', 'Pear'), ('peach', 'Peach')])

View File

@ -937,7 +937,15 @@ class EmailWidget(StringWidget):
self.error = _('invalid address domain')
class OptGroup(object):
def __init__(self, title):
self.title = title
class SingleSelectWidget(quixote.form.widget.SingleSelectWidget):
def get_allowed_values(self):
return [item[0] for item in self.options if not isinstance(item[0], OptGroup)]
def set_options(self, options, *args, **kwargs):
# options can be,
# - [objects:any, ...]
@ -967,8 +975,15 @@ class SingleSelectWidget(quixote.form.widget.SingleSelectWidget):
if self.attrs:
attrs.update(self.attrs)
tags = [htmltag("select", name=self.name, **attrs)]
for object, description, key, attrs in self.full_options:
if self.is_selected(object):
opened_optgroup = False
for obj, description, key, attrs in self.full_options:
if isinstance(obj, OptGroup):
if opened_optgroup:
tags.append(htmltext("</optgroup>"))
tags.append(htmltag("optgroup", label=obj.title))
opened_optgroup = True
continue
if self.is_selected(obj):
selected = 'selected'
else:
selected = None
@ -976,6 +991,8 @@ class SingleSelectWidget(quixote.form.widget.SingleSelectWidget):
description = ""
r = htmltag("option", value=key, selected=selected, **attrs)
tags.append(r + htmlescape(description) + htmltext('</option>'))
if opened_optgroup:
tags.append(htmltext("</optgroup>"))
tags.append(htmltext("</select>"))
return htmltext("\n").join(tags)