Projet

Général

Profil

0002-forms-use-a-template-to-render-select-widgets-17964.patch

Frédéric Péters, 11 août 2017 10:53

Télécharger (3,81 ko)

Voir les différences:

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
wcs/qommon/form.py
1483 1483

  
1484 1484

  
1485 1485
class SingleSelectHintWidget(SingleSelectWidget):
1486
    template_name = 'qommon/forms/widgets/select.html'
1487

  
1486 1488
    def __init__(self, name, value=None, **kwargs):
1487 1489
        self.options_with_attributes = kwargs.pop('options_with_attributes', None)
1488 1490
        super(SingleSelectHintWidget, self).__init__(name, value=value, **kwargs)
......
1490 1492
    def separate_hint(self):
1491 1493
        return (self.hint and len(self.hint) > 80)
1492 1494

  
1493
    def render_content(self):
1494
        attrs = {'id': 'form_' + self.name}
1495
        if self.attrs:
1496
            attrs.update(self.attrs)
1497
        tags = [htmltag('select', name=self.name, **attrs)]
1498
        options = self.options[:]
1499
        include_disabled = False
1495
    def get_options(self):
1500 1496
        if self.options_with_attributes:
1501
            options = self.options_with_attributes
1502
            include_disabled = True
1503
        if not self.separate_hint() and self.hint:
1504
            r = htmltag('option', value='', selected=None)
1505
            tags.append(r + htmlescape(self.hint) + htmltext('</option>'))
1506
            if self.options[0][0] is None:
1507
                # hint has been put as first element, skip the default empty
1508
                # value.
1509
                options = self.options[1:]
1497
            options = self.options_with_attributes[:]
1498
        else:
1499
            options = self.options[:]
1500
        if options[0][0] is None:
1501
            options = self.options[1:]
1502

  
1503
        tags = []
1510 1504
        for option in options:
1511 1505
            object, description, key = option[:3]
1512 1506
            html_attrs = {}
......
1517 1511
                html_attrs['disabled'] = 'disabled'
1518 1512
            if description is None:
1519 1513
                description = ''
1520
            r = htmltag('option', **html_attrs)
1521
            tags.append(r + htmlescape(description) + htmltext('</option>'))
1522
        tags.append(htmltext('</select>'))
1523
        return htmltext('\n').join(tags)
1514
            yield {'description': description, 'attrs': html_attrs,
1515
                    'options': option[-1] if self.options_with_attributes else None}
1524 1516

  
1525 1517
    def get_hint(self):
1526 1518
        if self.separate_hint():
wcs/qommon/templates/qommon/forms/widgets/select.html
1
{% extends "qommon/forms/widget.html" %}
2
{% block widget-control %}
3
<select id="form_{{widget.name}}" name="{{widget.name}}"
4
    {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}>
5
  {% if not widget.separate_hint and widget.hint %}
6
  <option value="">{{ widget.hint }}</option>
7
  {% endif %}
8
  {% for option in widget.get_options %}
9
  <option{% for attr in option.attrs.items %} {{attr.0}}="{{attr.1}}"{% endfor %}>{{ option.description }}</option>
10
  {% empty %}
11
    {% if widget.separate_hint or not widget.hint %}
12
    <option value="">---</option>
13
    {% endif %}
14
  {% endfor %}
15
</select>
16
{% endblock %}
0
-