web/formfields.py
changeset 5908 91c61feb5bc8
parent 5816 5d72fbba92e9
child 5911 47216d37bb3d
equal deleted inserted replaced
5907:5eba1248e12b 5908:91c61feb5bc8
   359         """
   359         """
   360         widget = self.get_widget(form)
   360         widget = self.get_widget(form)
   361         return widget.render(form, self, renderer)
   361         return widget.render(form, self, renderer)
   362 
   362 
   363     def vocabulary(self, form, **kwargs):
   363     def vocabulary(self, form, **kwargs):
   364         """return vocabulary for this field. This method will be called by
   364         """return vocabulary for this field. This method will be
   365         widgets which requires a vocabulary.
   365         called by widgets which requires a vocabulary.
       
   366 
       
   367         It should return a list of tuple (label, value), where value
       
   368         *must be an unicode string*, not a typed value.
   366         """
   369         """
   367         assert self.choices is not None
   370         assert self.choices is not None
   368         if callable(self.choices):
   371         if callable(self.choices):
   369             try:
   372             try:
   370                 if getattr(self.choices, 'im_self', None) is self:
   373                 if getattr(self.choices, 'im_self', None) is self:
   385         else:
   388         else:
   386             vocab = self.choices
   389             vocab = self.choices
   387         if vocab and not isinstance(vocab[0], (list, tuple)):
   390         if vocab and not isinstance(vocab[0], (list, tuple)):
   388             vocab = [(x, x) for x in vocab]
   391             vocab = [(x, x) for x in vocab]
   389         if self.internationalizable:
   392         if self.internationalizable:
   390             # the short-cirtcuit 'and' boolean operator is used here to permit
   393             # the short-cirtcuit 'and' boolean operator is used here
   391             # a valid empty string in vocabulary without attempting to translate
   394             # to permit a valid empty string in vocabulary without
   392             # it by gettext (which can lead to weird strings display)
   395             # attempting to translate it by gettext (which can lead to
   393             vocab = [(label and form._cw._(label), value) for label, value in vocab]
   396             # weird strings display)
       
   397             vocab = [(label and form._cw._(label), value)
       
   398                      for label, value in vocab]
   394         if self.sort:
   399         if self.sort:
   395             vocab = vocab_sort(vocab)
   400             vocab = vocab_sort(vocab)
       
   401         # XXX pre 3.9 bw compat
       
   402         for i, (label, value) in enumerate(vocab):
       
   403             if not isinstance(value, basestring):
       
   404                 warn('[3.9] %s: vocabulary value should be an unicode string'
       
   405                      % self, DeprecationWarning)
       
   406                 vocab[i] = (label, unicode(value))
   396         return vocab
   407         return vocab
   397 
   408 
   398     def format(self, form):
   409     def format(self, form):
   399         """return MIME type used for the given (text or bytes) field"""
   410         """return MIME type used for the given (text or bytes) field"""
   400         if self.eidparam and self.role == 'subject':
   411         if self.eidparam and self.role == 'subject':