web/formfields.py
changeset 3890 d7a270f50f54
parent 3720 5376aaadd16b
parent 3874 7d0d4a6be046
child 4014 24f7d7eb4c23
equal deleted inserted replaced
3810:5b75fd66c80e 3890:d7a270f50f54
    79     :role:
    79     :role:
    80        when the field is linked to an entity attribute or relation, tells the
    80        when the field is linked to an entity attribute or relation, tells the
    81        role of the entity in the relation (eg 'subject' or 'object')
    81        role of the entity in the relation (eg 'subject' or 'object')
    82     :fieldset:
    82     :fieldset:
    83        optional fieldset to which this field belongs to
    83        optional fieldset to which this field belongs to
       
    84     :order:
       
    85        key used by automatic forms to sort fields
    84 
    86 
    85     """
    87     """
    86     # default widget associated to this class of fields. May be overriden per
    88     # default widget associated to this class of fields. May be overriden per
    87     # instance
    89     # instance
    88     widget = TextInput
    90     widget = TextInput
    92     __creation_rank = 0
    94     __creation_rank = 0
    93 
    95 
    94     def __init__(self, name=None, id=None, label=None, help=None,
    96     def __init__(self, name=None, id=None, label=None, help=None,
    95                  widget=None, required=False, initial=None,
    97                  widget=None, required=False, initial=None,
    96                  choices=None, sort=True, internationalizable=False,
    98                  choices=None, sort=True, internationalizable=False,
    97                  eidparam=False, role='subject', fieldset=None):
    99                  eidparam=False, role='subject', fieldset=None, order=None):
    98         self.name = name
   100         self.name = name
    99         self.id = id or name
   101         self.id = id or name
   100         self.label = label or name
   102         self.label = label or name
   101         self.help = help
   103         self.help = help
   102         self.required = required
   104         self.required = required
   106         self.internationalizable = internationalizable
   108         self.internationalizable = internationalizable
   107         self.eidparam = eidparam
   109         self.eidparam = eidparam
   108         self.role = role
   110         self.role = role
   109         self.fieldset = fieldset
   111         self.fieldset = fieldset
   110         self.init_widget(widget)
   112         self.init_widget(widget)
       
   113         self.order = order
   111         # ordering number for this field instance
   114         # ordering number for this field instance
   112         self.creation_rank = Field.__creation_rank
   115         self.creation_rank = Field.__creation_rank
   113         Field.__creation_rank += 1
   116         Field.__creation_rank += 1
   114 
   117 
   115     def __unicode__(self):
   118     def __unicode__(self):
   176     def render(self, form, renderer):
   179     def render(self, form, renderer):
   177         """render this field, which is part of form, using the given form
   180         """render this field, which is part of form, using the given form
   178         renderer
   181         renderer
   179         """
   182         """
   180         widget = self.get_widget(form)
   183         widget = self.get_widget(form)
   181         try:
   184         return widget.render(form, self, renderer)
   182             return widget.render(form, self, renderer)
       
   183         except TypeError:
       
   184             warn('[3.3] %s: widget.render now take the renderer as third argument, '
       
   185                  'please update implementation' % widget, DeprecationWarning)
       
   186             return widget.render(form, self)
       
   187 
   185 
   188     def vocabulary(self, form):
   186     def vocabulary(self, form):
   189         """return vocabulary for this field. This method will be called by
   187         """return vocabulary for this field. This method will be called by
   190         widgets which desire it."""
   188         widgets which desire it."""
   191         if self.choices is not None:
   189         if self.choices is not None:
   201             if vocab and not isinstance(vocab[0], (list, tuple)):
   199             if vocab and not isinstance(vocab[0], (list, tuple)):
   202                 vocab = [(x, x) for x in vocab]
   200                 vocab = [(x, x) for x in vocab]
   203         else:
   201         else:
   204             vocab = form.form_field_vocabulary(self)
   202             vocab = form.form_field_vocabulary(self)
   205         if self.internationalizable:
   203         if self.internationalizable:
   206             vocab = [(form._cw._(label), value) for label, value in vocab]
   204             # the short-cirtcuit 'and' boolean operator is used here to permit
       
   205             # a valid empty string in vocabulary without attempting to translate
       
   206             # it by gettext (which can lead to weird strings display)
       
   207             vocab = [(label and form._cw._(label), value) for label, value in vocab]
   207         if self.sort:
   208         if self.sort:
   208             vocab = vocab_sort(vocab)
   209             vocab = vocab_sort(vocab)
   209         return vocab
   210         return vocab
   210 
   211 
   211     def form_init(self, form):
   212     def form_init(self, form):