web/formwidgets.py
branchtls-sprint
changeset 1771 bb9538d91465
parent 1768 b59b7c422a8a
child 1832 3384264d25cc
equal deleted inserted replaced
1768:b59b7c422a8a 1771:bb9538d91465
   171     def render(self, form, field):
   171     def render(self, form, field):
   172         name, curvalues, attrs = self._render_attrs(form, field)
   172         name, curvalues, attrs = self._render_attrs(form, field)
   173         if not 'size' in attrs and self._multiple:
   173         if not 'size' in attrs and self._multiple:
   174             attrs['size'] = '5'
   174             attrs['size'] = '5'
   175         options = []
   175         options = []
       
   176         optgroup_opened = False
   176         for label, value in field.vocabulary(form):
   177         for label, value in field.vocabulary(form):
   177             if value is None:
   178             if value is None:
   178                 # handle separator
   179                 # handle separator
   179                 options.append(u'<optgroup label="%s"/>' % (label or ''))
   180                 if optgroup_opened:
       
   181                     options.append(u'</optgroup>')
       
   182                 options.append(u'<optgroup label="%s">' % (label or ''))
       
   183                 optgroup_opened = True
   180             elif value in curvalues:
   184             elif value in curvalues:
   181                 options.append(tags.option(label, value=value, selected='selected'))
   185                 options.append(tags.option(label, value=value, selected='selected'))
   182             else:
   186             else:
   183                 options.append(tags.option(label, value=value))
   187                 options.append(tags.option(label, value=value))
       
   188         if optgroup_opened:
       
   189             options.append(u'</optgroup>')
   184         return tags.select(name=name, multiple=self._multiple,
   190         return tags.select(name=name, multiple=self._multiple,
   185                            options=options, **attrs)
   191                            options=options, **attrs)
   186 
   192 
   187 
   193 
   188 class CheckBox(Input):
   194 class CheckBox(Input):