fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints stable
authorFlorent <florent@secondweb.fr>
Mon, 18 May 2009 11:50:43 +0200
branchstable
changeset 1836 d3f0c6e868ef
parent 1834 c153913924a5
child 1837 67fcc174d299
fix StringField widget selection with SizeConstraint and StaticVocabularyConstraint constraints
web/formfields.py
web/test/data/schema/testschema.py
web/test/unittest_formfields.py
--- a/web/formfields.py	Mon May 18 10:54:48 2009 +0200
+++ b/web/formfields.py	Mon May 18 11:50:43 2009 +0200
@@ -471,7 +471,10 @@
                     kwargs.setdefault('widget', Select())
                     kwargs.setdefault('choices', cstr.vocabulary)
                     if card in '?1':
+                        if isinstance(kwargs['widget'], type):
+                            kwargs['widget'] = kwargs['widget']()
                         kwargs['widget'].attrs.setdefault('size', 1)
+            for cstr in constraints:
                 if isinstance(cstr, SizeConstraint) and cstr.max is not None:
                     if cstr.max < 257:
                         kwargs.setdefault('widget', TextInput())
--- a/web/test/data/schema/testschema.py	Mon May 18 10:54:48 2009 +0200
+++ b/web/test/data/schema/testschema.py	Mon May 18 11:50:43 2009 +0200
@@ -1,6 +1,7 @@
 class Salesterm(EntityType):
     described_by_test = SubjectRelation('File', cardinality='1*', composite='subject')
     amount = Int(constraints=[IntervalBoundConstraint(0, 100)])
+    reason = String(maxsize=20, vocabulary=[u'canceled', u'sold'])
 
 class tags(RelationDefinition):
     subject = 'Tag'
--- a/web/test/unittest_formfields.py	Mon May 18 10:54:48 2009 +0200
+++ b/web/test/unittest_formfields.py	Mon May 18 11:50:43 2009 +0200
@@ -1,8 +1,9 @@
 """unittests for cw.web.formfields"""
 
+from yams.constraints import StaticVocabularyConstraint, SizeConstraint
 from logilab.common.testlib import TestCase, unittest_main
 from cubicweb.devtools import TestServerConfiguration
-from cubicweb.web.formwidgets import PasswordInput, TextArea
+from cubicweb.web.formwidgets import PasswordInput, TextArea, Select
 from cubicweb.web.formfields import *
 from cubicweb.entities.wfobjs import State
 from cubicweb.entities.authobjs import CWUser
@@ -14,6 +15,7 @@
 state_schema = schema['State']
 cwuser_schema = schema['CWUser']
 file_schema = schema['File']
+salesterm_schema = schema['Salesterm']
 
 class GuessFieldTC(TestCase):
 
@@ -80,5 +82,12 @@
         self.assertIsInstance(data_field.format_field, StringField)
         self.assertIsInstance(data_field.encoding_field, StringField)
 
+    def test_constraints_priority(self):
+        salesterm_field = guess_field(salesterm_schema, schema['reason'])
+        constraints = schema['reason'].rproperty('Salesterm', 'String', 'constraints')
+        self.assertEquals([c.__class__ for c in constraints],
+                          [SizeConstraint, StaticVocabularyConstraint])
+        self.assertIsInstance(salesterm_field.widget, Select)
+
 if __name__ == '__main__':
     unittest_main()