web/formfields.py
branchtls-sprint
changeset 1181 620ec8e6ae19
parent 1147 402e8a8b1d6a
child 1265 e5cdd5c0dce3
equal deleted inserted replaced
1180:5536d4ee2bc3 1181:620ec8e6ae19
    87     def render(self, form, renderer):
    87     def render(self, form, renderer):
    88         return self.get_widget(form).render(form, self)
    88         return self.get_widget(form).render(form, self)
    89 
    89 
    90     def vocabulary(self, form):
    90     def vocabulary(self, form):
    91         if self.choices is not None:
    91         if self.choices is not None:
    92             return self.choices
    92             if callable(self.choices):
       
    93                 vocab = self.choices(req=form.req)
       
    94             else:
       
    95                 vocab = self.choices
       
    96             if vocab and not isinstance(vocab[0], (list, tuple)):
       
    97                 vocab = [(x, x) for x in vocab]
       
    98             return vocab
    93         return form.form_field_vocabulary(self)
    99         return form.form_field_vocabulary(self)
    94 
   100 
    95     
   101     
    96 class StringField(Field):
   102 class StringField(Field):
    97     def __init__(self, max_length=None, **kwargs):
   103     def __init__(self, max_length=None, **kwargs):
   332 
   338 
   333 def stringfield_from_constraints(constraints, **kwargs):
   339 def stringfield_from_constraints(constraints, **kwargs):
   334     field = None
   340     field = None
   335     for cstr in constraints:
   341     for cstr in constraints:
   336         if isinstance(cstr, StaticVocabularyConstraint):
   342         if isinstance(cstr, StaticVocabularyConstraint):
   337             kwargs.setdefault('widget', Select(vocabulary=cstr.vocabulary))
   343             kwargs.setdefault('widget', Select())
   338             return StringField(**kwargs)
   344             return StringField(choices=cstr.vocabulary, **kwargs)
   339         if isinstance(cstr, SizeConstraint) and cstr.max is not None:
   345         if isinstance(cstr, SizeConstraint) and cstr.max is not None:
   340             if cstr.max > 257:
   346             if cstr.max > 257:
   341                 rows_cols_from_constraint(cstr, kwargs)
   347                 rows_cols_from_constraint(cstr, kwargs)
   342                 field = TextField(**kwargs)
   348                 field = TextField(**kwargs)
   343             else:
   349             else: