web/formfields.py
changeset 4388 15c6607c4bda
parent 4386 cf8842b69379
child 4392 91a56a30141e
equal deleted inserted replaced
4387:4aacd6492ef4 4388:15c6607c4bda
   249         renderer
   249         renderer
   250         """
   250         """
   251         widget = self.get_widget(form)
   251         widget = self.get_widget(form)
   252         return widget.render(form, self, renderer)
   252         return widget.render(form, self, renderer)
   253 
   253 
   254     def vocabulary(self, form):
   254     def vocabulary(self, form, **kwargs):
   255         """return vocabulary for this field. This method will be called by
   255         """return vocabulary for this field. This method will be called by
   256         widgets which requires a vocabulary.
   256         widgets which requires a vocabulary.
   257         """
   257         """
   258         assert self.choices is not None
   258         assert self.choices is not None
   259         if callable(self.choices):
   259         if callable(self.choices):
   260             try:
   260             try:
   261                 if getattr(self.choices, 'im_self', None) is self:
   261                 if getattr(self.choices, 'im_self', None) is self:
   262                     vocab = self.choices(form=form)
   262                     vocab = self.choices(form=form, **kwargs)
   263                 else:
   263                 else:
   264                     vocab = self.choices(form=form, field=self)
   264                     vocab = self.choices(form=form, field=self, **kwargs)
   265             except TypeError:
   265             except TypeError:
   266                 warn('[3.6]  %s: choices should now take '
   266                 warn('[3.6]  %s: choices should now take '
   267                      'the form and field as named arguments' % self,
   267                      'the form and field as named arguments' % self,
   268                      DeprecationWarning)
   268                      DeprecationWarning)
   269                 try:
   269                 try:
   270                     vocab = self.choices(form=form)
   270                     vocab = self.choices(form=form, **kwargs)
   271                 except TypeError:
   271                 except TypeError:
   272                     warn('[3.3]  %s: choices should now take '
   272                     warn('[3.3]  %s: choices should now take '
   273                          'the form and field as named arguments' % self,
   273                          'the form and field as named arguments' % self,
   274                          DeprecationWarning)
   274                          DeprecationWarning)
   275                     vocab = self.choices(req=form._cw)
   275                     vocab = self.choices(req=form._cw, **kwargs)
   276         else:
   276         else:
   277             vocab = self.choices
   277             vocab = self.choices
   278         if vocab and not isinstance(vocab[0], (list, tuple)):
   278         if vocab and not isinstance(vocab[0], (list, tuple)):
   279             vocab = [(x, x) for x in vocab]
   279             vocab = [(x, x) for x in vocab]
   280         if self.internationalizable:
   280         if self.internationalizable:
   758     def fromcardinality(card, **kwargs):
   758     def fromcardinality(card, **kwargs):
   759         kwargs.setdefault('widget', fw.Select(multiple=card in '*+'))
   759         kwargs.setdefault('widget', fw.Select(multiple=card in '*+'))
   760         return RelationField(**kwargs)
   760         return RelationField(**kwargs)
   761 
   761 
   762     def choices(self, form, limit=None):
   762     def choices(self, form, limit=None):
       
   763         """Take care, choices function for relation field instance should take
       
   764         an extra 'limit' argument, with default to None.
       
   765 
       
   766         This argument is used by the 'unrelateddivs' view (see in autoform) and
       
   767         when it's specified (eg not None), vocabulary returned should:
       
   768         * not include already related entities
       
   769         * have a max size of `limit` entities
       
   770         """
   763         entity = form.edited_entity
   771         entity = form.edited_entity
   764         # first see if its specified by __linkto form parameters
   772         # first see if its specified by __linkto form parameters
   765         linkedto = relvoc_linkedto(entity, self.name, self.role)
   773         if limit is None:
   766         if linkedto:
   774             linkedto = relvoc_linkedto(entity, self.name, self.role)
   767             return linkedto
   775             if linkedto:
       
   776                 return linkedto
       
   777             vocab = relvoc_init(entity, self.name, self.role, self.required)
       
   778         else:
       
   779             vocab = []
   768         # it isn't, check if the entity provides a method to get correct values
   780         # it isn't, check if the entity provides a method to get correct values
   769         vocab = relvoc_init(entity, self.name, self.role, self.required)
       
   770         method = '%s_%s_vocabulary' % (self.role, self.name)
   781         method = '%s_%s_vocabulary' % (self.role, self.name)
   771         try:
   782         try:
   772             vocab += getattr(form, method)(self.name, limit)
   783             vocab += getattr(form, method)(self.name, limit)
   773             warn('[3.6] found %s on %s, should override field.choices instead (need tweaks)'
   784             warn('[3.6] found %s on %s, should override field.choices instead (need tweaks)'
   774                  % (method, form), DeprecationWarning)
   785                  % (method, form), DeprecationWarning)