0001-forms-only-display-selected-option-for-readonly-sele.patch
tests/test_widgets.py | ||
---|---|---|
464 | 464 |
('peach', 'Peach', 'peach', {'disabled': True})]) |
465 | 465 |
assert not widget.has_valid_options() |
466 | 466 | |
467 |
# test readonly only includes the selected value |
|
468 |
req.form = {} |
|
469 |
widget = SingleSelectHintWidget('test', |
|
470 |
options=[ |
|
471 |
('apple', 'Apple', 'apple'), |
|
472 |
('pear', 'Pear', 'pear'), |
|
473 |
('peach', 'Peach', 'peach')]) |
|
474 |
widget.readonly = 'readonly' |
|
475 |
widget.attrs['readonly'] = 'readonly' |
|
476 |
assert 'apple' in str(widget.render()) and 'pear' in str(widget.render()) |
|
477 |
widget.set_value('pear') |
|
478 |
assert 'apple' not in str(widget.render()) and 'pear' in str(widget.render()) |
|
479 |
assert 'readonly="readonly"' not in str(widget.render()) |
|
480 | ||
467 | 481 | |
468 | 482 |
def test_select_or_other_widget(): |
469 | 483 |
widget = SingleSelectWidgetWithOther('test', |
wcs/qommon/form.py | ||
---|---|---|
1772 | 1772 | |
1773 | 1773 |
class SingleSelectHintWidget(SingleSelectWidget): |
1774 | 1774 |
template_name = 'qommon/forms/widgets/select.html' |
1775 |
readonly = None |
|
1775 | 1776 | |
1776 | 1777 |
def __init__(self, name, value=None, **kwargs): |
1777 | 1778 |
self.options_with_attributes = kwargs.pop('options_with_attributes', None) |
... | ... | |
1794 | 1795 |
if options[0][0] is None: |
1795 | 1796 |
options = self.options[1:] |
1796 | 1797 | |
1797 |
tags = [] |
|
1798 | 1798 |
for option in options: |
1799 | 1799 |
object, description, key = option[:3] |
1800 | 1800 |
html_attrs = {} |
1801 | 1801 |
html_attrs['value'] = key |
1802 | 1802 |
if self.is_selected(object): |
1803 | 1803 |
html_attrs['selected'] = 'selected' |
1804 |
elif self.readonly and self.value: |
|
1805 |
# if readonly only include the selected option |
|
1806 |
continue |
|
1804 | 1807 |
if self.options_with_attributes and option[-1].get('disabled'): |
1805 | 1808 |
html_attrs['disabled'] = 'disabled' |
1806 | 1809 |
if description is None: |
wcs/qommon/templates/qommon/forms/widgets/select.html | ||
---|---|---|
3 | 3 |
<select id="form_{{widget.name}}" name="{{widget.name}}" |
4 | 4 |
{% if widget.select2 %}data-autocomplete="true"{% endif %} |
5 | 5 |
{% if widget.required %}data-required="true"{% endif %} |
6 |
{% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}>
|
|
6 |
{% for attr in widget.attrs.items %}{% if attr.0 != 'readonly' %}{{attr.0}}="{{attr.1}}"{% endif %}{% endfor %}>
|
|
7 | 7 |
{% if not widget.separate_hint and widget.hint %} |
8 | 8 |
<option value="" data-hint="{{ widget.hint }}">{% if not widget.select2 %}{{ widget.hint }}{% endif %}</option> |
9 | 9 |
{% endif %} |
10 |
- |