web/formwidgets.py
branchstable
changeset 7450 c21d845836e4
parent 7411 238da9684f99
child 7453 84046395d2cd
equal deleted inserted replaced
7449:5e45e32071aa 7450:c21d845836e4
   433 class Select(FieldWidget):
   433 class Select(FieldWidget):
   434     """Simple <select>, for field having a specific vocabulary. Will return
   434     """Simple <select>, for field having a specific vocabulary. Will return
   435     an unicode string, or a list of unicode strings.
   435     an unicode string, or a list of unicode strings.
   436     """
   436     """
   437     vocabulary_widget = True
   437     vocabulary_widget = True
       
   438     default_size = 5
   438 
   439 
   439     def __init__(self, attrs=None, multiple=False, **kwargs):
   440     def __init__(self, attrs=None, multiple=False, **kwargs):
   440         super(Select, self).__init__(attrs, **kwargs)
   441         super(Select, self).__init__(attrs, **kwargs)
   441         self._multiple = multiple
   442         self._multiple = multiple
   442 
   443 
   443     def _render(self, form, field, renderer):
   444     def _render(self, form, field, renderer):
   444         curvalues, attrs = self.values_and_attributes(form, field)
   445         curvalues, attrs = self.values_and_attributes(form, field)
   445         if not 'size' in attrs:
       
   446             attrs['size'] = self._multiple and '5' or '1'
       
   447         options = []
   446         options = []
   448         optgroup_opened = False
   447         optgroup_opened = False
   449         for option in field.vocabulary(form):
   448         for option in field.vocabulary(form):
   450             try:
   449             try:
   451                 label, value, oattrs = option
   450                 label, value, oattrs = option
   464                                            selected='selected', **oattrs))
   463                                            selected='selected', **oattrs))
   465             else:
   464             else:
   466                 options.append(tags.option(label, value=value, **oattrs))
   465                 options.append(tags.option(label, value=value, **oattrs))
   467         if optgroup_opened:
   466         if optgroup_opened:
   468             options.append(u'</optgroup>')
   467             options.append(u'</optgroup>')
       
   468         if not 'size' in attrs:
       
   469             if self._multiple:
       
   470                 size = unicode(min(self.default_size, len(vocab) or 1))
       
   471             else:
       
   472                 size = u'1'
       
   473             attrs['size'] = size
   469         return tags.select(name=field.input_name(form, self.suffix),
   474         return tags.select(name=field.input_name(form, self.suffix),
   470                            multiple=self._multiple, options=options, **attrs)
   475                            multiple=self._multiple, options=options, **attrs)
   471 
   476 
   472 
   477 
   473 class CheckBox(Input):
   478 class CheckBox(Input):