web/formfields.py
changeset 7990 a673d1d9a738
parent 7973 64639bc94e25
child 8238 087bb529035c
equal deleted inserted replaced
7989:db76e8aaec29 7990:a673d1d9a738
   201     fallback_on_none_attribute = False
   201     fallback_on_none_attribute = False
   202     ignore_req_params = False
   202     ignore_req_params = False
   203 
   203 
   204     def __init__(self, name=None, label=_MARKER, widget=None, **kwargs):
   204     def __init__(self, name=None, label=_MARKER, widget=None, **kwargs):
   205         for key, val in kwargs.items():
   205         for key, val in kwargs.items():
   206             if key == 'initial':
       
   207                 warn('[3.6] use value instead of initial', DeprecationWarning,
       
   208                      stacklevel=3)
       
   209                 key = 'value'
       
   210             assert hasattr(self.__class__, key) and not key[0] == '_', key
   206             assert hasattr(self.__class__, key) and not key[0] == '_', key
   211             setattr(self, key, val)
   207             setattr(self, key, val)
   212         self.name = name
   208         self.name = name
   213         if label is _MARKER:
   209         if label is _MARKER:
   214             label = name or _MARKER
   210             label = name or _MARKER
   356                     warn("[3.10] field's value callback must now take form and "
   352                     warn("[3.10] field's value callback must now take form and "
   357                          "field as argument (%s)" % self, DeprecationWarning)
   353                          "field as argument (%s)" % self, DeprecationWarning)
   358                     return self.value(form)
   354                     return self.value(form)
   359             return self.value
   355             return self.value
   360         formattr = '%s_%s_default' % (self.role, self.name)
   356         formattr = '%s_%s_default' % (self.role, self.name)
   361         if hasattr(form, formattr):
       
   362             warn('[3.6] %s.%s deprecated, use field.value' % (
       
   363                 form.__class__.__name__, formattr), DeprecationWarning)
       
   364             return getattr(form, formattr)()
       
   365         if self.eidparam and self.role is not None:
   357         if self.eidparam and self.role is not None:
   366             if form._cw.vreg.schema.rschema(self.name).final:
   358             if form._cw.vreg.schema.rschema(self.name).final:
   367                 return form.edited_entity.e_schema.default(self.name)
   359                 return form.edited_entity.e_schema.default(self.name)
   368             return ()
   360             return ()
   369         return None
   361         return None
   391         assert self.choices is not None
   383         assert self.choices is not None
   392         if callable(self.choices):
   384         if callable(self.choices):
   393             # pylint: disable=E1102
   385             # pylint: disable=E1102
   394             if getattr(self.choices, 'im_self', None) is self:
   386             if getattr(self.choices, 'im_self', None) is self:
   395                 vocab = self.choices(form=form, **kwargs)
   387                 vocab = self.choices(form=form, **kwargs)
   396             elif support_args(self.choices, 'form', 'field'):
   388             else:
   397                 vocab = self.choices(form=form, field=self, **kwargs)
   389                 vocab = self.choices(form=form, field=self, **kwargs)
   398             else:
       
   399                 try:
       
   400                     vocab = self.choices(form=form, **kwargs)
       
   401                     warn('[3.6] %s: choices should now take '
       
   402                          'the form and field as named arguments' % self,
       
   403                          DeprecationWarning)
       
   404                 except TypeError:
       
   405                     warn('[3.3] %s: choices should now take '
       
   406                          'the form and field as named arguments' % self,
       
   407                          DeprecationWarning)
       
   408                     vocab = self.choices(req=form._cw, **kwargs)
       
   409         else:
   390         else:
   410             vocab = self.choices
   391             vocab = self.choices
   411         if vocab and not isinstance(vocab[0], (list, tuple)):
   392         if vocab and not isinstance(vocab[0], (list, tuple)):
   412             vocab = [(x, x) for x in vocab]
   393             vocab = [(x, x) for x in vocab]
   413         if self.internationalizable:
   394         if self.internationalizable:
  1045         # first see if its specified by __linkto form parameters
  1026         # first see if its specified by __linkto form parameters
  1046         if limit is None:
  1027         if limit is None:
  1047             linkedto = self.relvoc_linkedto(form)
  1028             linkedto = self.relvoc_linkedto(form)
  1048             if linkedto:
  1029             if linkedto:
  1049                 return linkedto
  1030                 return linkedto
       
  1031             # it isn't, search more vocabulary
  1050             vocab = self.relvoc_init(form)
  1032             vocab = self.relvoc_init(form)
  1051         else:
  1033         else:
  1052             vocab = []
  1034             vocab = []
  1053         # it isn't, check if the entity provides a method to get correct values
  1035         vocab += self.relvoc_unrelated(form, limit)
  1054         method = '%s_%s_vocabulary' % (self.role, self.name)
       
  1055         try:
       
  1056             vocab += getattr(form, method)(self.name, limit)
       
  1057             warn('[3.6] found %s on %s, should override field.choices instead (need tweaks)'
       
  1058                  % (method, form), DeprecationWarning)
       
  1059         except AttributeError:
       
  1060             vocab += self.relvoc_unrelated(form, limit)
       
  1061         if self.sort:
  1036         if self.sort:
  1062             vocab = vocab_sort(vocab)
  1037             vocab = vocab_sort(vocab)
  1063         return vocab
  1038         return vocab
  1064 
  1039 
  1065     def relvoc_linkedto(self, form):
  1040     def relvoc_linkedto(self, form):