more guess_field tests and fixes tls-sprint
authorsylvain.thenault@logilab.fr
Thu, 12 Mar 2009 19:58:58 +0100
branchtls-sprint
changeset 1104 58f27c3c0167
parent 1103 f719caf263de
child 1107 961a478593a5
more guess_field tests and fixes
web/formfields.py
web/test/unittest_formfields.py
--- a/web/formfields.py	Thu Mar 12 19:44:05 2009 +0100
+++ b/web/formfields.py	Thu Mar 12 19:58:58 2009 +0100
@@ -354,7 +354,7 @@
     kwargs.setdefault('cols', cols)
 
 
-def guess_field(eclass, rschema, role='subject', **kwargs):
+def guess_field(eclass, rschema, role='subject', skip_meta_attr=True, **kwargs):
     """return the most adapated widget to edit the relation
     'subjschema rschema objschema' according to information found in the schema
     """
@@ -369,22 +369,33 @@
     kwargs['required'] = card in '1+'
     kwargs['name'] = rschema.type
     if rschema.is_final():
-        if rschema in eschema.format_fields:
+        if skip_meta_attr and rschema in eschema.meta_attributes():
             return None
-        if targetschema == 'Password':
-            return StringField(widget=PasswordInput(), **kwargs)
-        if eschema.has_metadata(rschema, 'format'):
-            constraints = rschema.rproperty(eschema, targetschema, 'constraints')
-            for cstr in constraints:
-                if isinstance(cstr, StaticVocabularyConstraint):
-                    raise Exception('rich text field with static vocabulary')
-                if isinstance(cstr, SizeConstraint) and cstr.max is not None:
-                    rows_cols_from_constraint(cstr, kwargs)
-            return RichTextField(**kwargs)
         fieldclass = FIELDS[targetschema]
         if fieldclass is StringField:
+            if targetschema == 'Password':
+                # special case for Password field: specific PasswordInput widget
+                return StringField(widget=PasswordInput(), **kwargs)
+            if eschema.has_metadata(rschema, 'format'):
+                # use RichTextField instead of StringField if the attribute has
+                # a "format" metadata. But getting information from constraints
+                # may be useful anyway...
+                constraints = rschema.rproperty(eschema, targetschema, 'constraints')
+                for cstr in constraints:
+                    if isinstance(cstr, StaticVocabularyConstraint):
+                        raise Exception('rich text field with static vocabulary')
+                    if isinstance(cstr, SizeConstraint) and cstr.max is not None:
+                        rows_cols_from_constraint(cstr, kwargs)
+                return RichTextField(**kwargs)
+            # return StringField or TextField according to constraints
             constraints = rschema.rproperty(eschema, targetschema, 'constraints')
             return stringfield_from_constraints(constraints, **kwargs)
+        if fieldclass is FileField:
+            for metadata in ('format', 'encoding'):
+                metaschema = eschema.has_metadata(rschema, metadata)
+                if metaschema is not None:
+                    kwargs['%s_field' % metadata] = guess_field(eclass, metaschema,
+                                                                skip_meta_attr=False)
         return fieldclass(**kwargs)
     kwargs['role'] = role
     return RelationField.fromcardinality(card, **kwargs)
--- a/web/test/unittest_formfields.py	Thu Mar 12 19:44:05 2009 +0100
+++ b/web/test/unittest_formfields.py	Thu Mar 12 19:58:58 2009 +0100
@@ -1,12 +1,12 @@
 """unittests for cw.web.formfields"""
 
 from logilab.common.testlib import TestCase, unittest_main
-
 from cubicweb.devtools import TestServerConfiguration
+from cubicweb.web.formwidgets import PasswordInput
+from cubicweb.web.formfields import *
 from cubicweb.entities.lib import Card
 from cubicweb.entities.authobjs import EUser
-from cubicweb.web.formwidgets import PasswordInput
-from cubicweb.web.formfields import *
+from cubes.file.entities import File
 
 config = TestServerConfiguration('data')
 config.bootstrap_cubes()
@@ -15,6 +15,8 @@
 Card.__initialize__()
 EUser.schema = schema
 EUser.__initialize__()
+File.schema = schema
+File.__initialize__()
         
 class GuessFieldTC(TestCase):
     
@@ -59,6 +61,17 @@
         self.assertEquals(owned_by_field.required, False)
         self.assertEquals(owned_by_field.role, 'object')
 
-    
+    def test_file_fields(self):
+        data_format_field = guess_field(File, schema['data_format'])
+        self.assertEquals(data_format_field, None)
+        data_encoding_field = guess_field(File, schema['data_encoding'])
+        self.assertEquals(data_encoding_field, None)
+
+        data_field = guess_field(File, schema['data'])
+        self.assertIsInstance(data_field, FileField)
+        self.assertEquals(data_field.required, True)
+        self.assertIsInstance(data_field.format_field, StringField)
+        self.assertIsInstance(data_field.encoding_field, StringField)
+        
 if __name__ == '__main__':
     unittest_main()