guess_field now takes an entity schema as first argument, not an entity class tls-sprint
authorsylvain.thenault@logilab.fr
Thu, 23 Apr 2009 17:22:02 +0200
branchtls-sprint
changeset 1453 a9841184be7c
parent 1452 546b9884d92c
child 1454 1668989a8f0e
guess_field now takes an entity schema as first argument, not an entity class
web/formfields.py
web/test/unittest_formfields.py
web/views/baseviews.py
web/views/editforms.py
web/views/management.py
--- a/web/formfields.py	Thu Apr 23 17:21:39 2009 +0200
+++ b/web/formfields.py	Thu Apr 23 17:22:02 2009 +0200
@@ -436,12 +436,11 @@
     kwargs.setdefault('cols', cols)
 
 
-def guess_field(eclass, rschema, role='subject', skip_meta_attr=True, **kwargs):
+def guess_field(eschema, 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
     """
     fieldclass = None
-    eschema = eclass.e_schema
     if role == 'subject':
         targetschema = rschema.objects(eschema)[0]
         card = rschema.rproperty(eschema, targetschema, 'cardinality')[0]
@@ -484,7 +483,7 @@
             for metadata in ('format', 'encoding'):
                 metaschema = eschema.has_metadata(rschema, metadata)
                 if metaschema is not None:
-                    kwargs['%s_field' % metadata] = guess_field(eclass, metaschema,
+                    kwargs['%s_field' % metadata] = guess_field(eschema, metaschema,
                                                                 skip_meta_attr=False)
         return fieldclass(**kwargs)
     kwargs['role'] = role
--- a/web/test/unittest_formfields.py	Thu Apr 23 17:21:39 2009 +0200
+++ b/web/test/unittest_formfields.py	Thu Apr 23 17:22:02 2009 +0200
@@ -11,76 +11,73 @@
 config = TestServerConfiguration('data')
 config.bootstrap_cubes()
 schema = config.load_schema()
-Card.schema = schema
-Card.__initialize__()
-CWUser.schema = schema
-CWUser.__initialize__()
-File.schema = schema
-File.__initialize__()
-        
+card_schema = schema['Card']
+cwuser_schema = schema['CWUser']
+file_schema.schema = schema['File']
+
 class GuessFieldTC(TestCase):
-    
+
     def test_card_fields(self):
-        title_field = guess_field(Card, schema['title'])
+        title_field = guess_field(card_schema, schema['title'])
         self.assertIsInstance(title_field, StringField)
         self.assertEquals(title_field.required, True)
-        
-        synopsis_field = guess_field(Card, schema['synopsis'])
+
+        synopsis_field = guess_field(card_schema, schema['synopsis'])
         self.assertIsInstance(synopsis_field, TextField)
         self.assertEquals(synopsis_field.required, False)
         self.assertEquals(synopsis_field.help, 'an abstract for this card')
-        
-        content_field = guess_field(Card, schema['content'])
+
+        content_field = guess_field(card_schema, schema['content'])
         self.assertIsInstance(content_field, RichTextField)
         self.assertEquals(content_field.required, False)
         self.assertEquals(content_field.format_field, None)
-                          
-        content_format_field = guess_field(Card, schema['content_format'])
+
+        content_format_field = guess_field(card_schema, schema['content_format'])
         self.assertEquals(content_format_field, None)
-        
-        content_format_field = guess_field(Card, schema['content_format'], skip_meta_attr=False)
+
+        content_format_field = guess_field(card_schema, schema['content_format'], skip_meta_attr=False)
         self.assertEquals(content_format_field.internationalizable, True)
         self.assertEquals(content_format_field.sort, True)
         self.assertEquals(content_format_field.initial, 'text/rest')
 
-        wikiid_field = guess_field(Card, schema['wikiid'])
+        wikiid_field = guess_field(card_schema, schema['wikiid'])
         self.assertIsInstance(wikiid_field, StringField)
         self.assertEquals(wikiid_field.required, False)
 
-        
+
     def test_euser_fields(self):
-        upassword_field = guess_field(CWUser, schema['upassword'])
+        upassword_field = guess_field(cwuser_schema, schema['upassword'])
         self.assertIsInstance(upassword_field, StringField)
         self.assertIsInstance(upassword_field.widget, PasswordInput)
         self.assertEquals(upassword_field.required, True)
 
-        last_login_time_field = guess_field(CWUser, schema['last_login_time'])
+        last_login_time_field = guess_field(cwuser_schema, schema['last_login_time'])
         self.assertIsInstance(last_login_time_field, DateTimeField)
         self.assertEquals(last_login_time_field.required, False)
 
-        in_group_field = guess_field(CWUser, schema['in_group'])
+        in_group_field = guess_field(cwuser_schema, schema['in_group'])
         self.assertIsInstance(in_group_field, RelationField)
         self.assertEquals(in_group_field.required, True)
         self.assertEquals(in_group_field.role, 'subject')
         self.assertEquals(in_group_field.help, 'groups grant permissions to the user')
 
-        owned_by_field = guess_field(CWUser, schema['owned_by'], 'object')
+        owned_by_field = guess_field(cwuser_schema, schema['owned_by'], 'object')
         self.assertIsInstance(owned_by_field, RelationField)
         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'])
+        data_format_field = guess_field(file_schema, schema['data_format'])
         self.assertEquals(data_format_field, None)
-        data_encoding_field = guess_field(File, schema['data_encoding'])
+        data_encoding_field = guess_field(file_schema, schema['data_encoding'])
         self.assertEquals(data_encoding_field, None)
 
-        data_field = guess_field(File, schema['data'])
+        data_field = guess_field(file_schema, 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()
--- a/web/views/baseviews.py	Thu Apr 23 17:21:39 2009 +0200
+++ b/web/views/baseviews.py	Thu Apr 23 17:22:02 2009 +0200
@@ -296,7 +296,7 @@
                                                     self.req._('see them all'))
                 value +=  '</div>'
         label = display_name(self.req, rschema.type, role)
-        self.field(label, value, show_label=show_label, w=self.w, tr=False)
+        self.field(label, value, show_label=show_label, tr=False)
 
 
 class SecondaryView(EntityView):
--- a/web/views/editforms.py	Thu Apr 23 17:21:39 2009 +0200
+++ b/web/views/editforms.py	Thu Apr 23 17:22:02 2009 +0200
@@ -258,7 +258,7 @@
         return sorted(result)
 
     @iclassmethod
-    def field_by_name(cls_or_self, name, role='subject', eclass=None):
+    def field_by_name(cls_or_self, name, role='subject', eschema=None):
         """return field with the given name and role. If field is not explicitly
         defined for the form but `eclass` is specified, guess_field will be
         called.
@@ -268,7 +268,7 @@
         except FieldNotFound: # XXX should raise more explicit exception
             if eclass is None:
                 raise
-            field = guess_field(eclass, cls_or_self.schema.rschema(name), role,
+            field = guess_field(eschema, cls_or_self.schema.rschema(name), role,
                                 eidparam=True)
             if field is None:
                 raise
@@ -293,10 +293,10 @@
             widget = self.rwidgets.etype_rtag(self.edited_entity.id, rschema,
                                               role)
             if widget:
-                field = guess_field(self.edited_entity.__class__, rschema, role,
+                field = guess_field(self.edited_entity.e_schema, rschema, role,
                                     eidparam=True, widget=widget)
             else:
-                field = guess_field(self.edited_entity.__class__, rschema, role,
+                field = guess_field(self.edited_entity.e_schema, rschema, role,
                                     eidparam=True)
             if field is not None:
                 self.fields.append(field)
--- a/web/views/management.py	Thu Apr 23 17:21:39 2009 +0200
+++ b/web/views/management.py	Thu Apr 23 17:22:02 2009 +0200
@@ -83,8 +83,8 @@
                                 form_buttons=[formwidgets.SubmitButton()],
                                 __redirectvid='security',
                                 __redirectpath=entity.rest_path())
-        field = guess_field(entity.__class__, self.schema.rschema('owned_by'))
-        form.append_field(field)                          
+        field = guess_field(entity.e_schema, self.schema.rschema('owned_by'))
+        form.append_field(field)
         self.w(form.form_render())
 
     def owned_by_information(self, entity):
@@ -135,21 +135,22 @@
         newperm = self.vreg.etype_class('CWPermission')(self.req, None)
         newperm.eid = self.req.varmaker.next()
         w(u'<p>%s</p>' % _('add a new permission'))
-        form = EntityFieldsForm(self.req, None, entity=newperm, 
+        form = EntityFieldsForm(self.req, None, entity=newperm,
                                 form_buttons=[formwidgets.SubmitButton()],
                                 __redirectvid='security',
                                 __redirectpath=entity.rest_path())
         form.form_add_hidden('require_permission', entity.eid, role='object', eidparam=True)
         permnames = getattr(entity, '__permissions__', None)
+        cwpermschema = newperm.e_schema
         if permnames is not None:
-            field = guess_field(newperm.__class__, self.schema.rschema('name'),
+            field = guess_field(cwpermschema, self.schema.rschema('name'),
                                 widget=formwidgets.Select, choices=permnames)
         else:
-            field = guess_field(newperm.__class__, self.schema.rschema('name'))
+            field = guess_field(cwpermschema, self.schema.rschema('name'))
         form.append_field(field)
-        field = guess_field(newperm.__class__, self.schema.rschema('label'))
+        field = guess_field(cwpermschema, self.schema.rschema('label'))
         form.append_field(field)
-        field = guess_field(newperm.__class__, self.schema.rschema('require_group'))
+        field = guess_field(cwpermschema, self.schema.rschema('require_group'))
         form.append_field(field)
         self.w(form.form_render(renderer=HTableFormRenderer()))
 
@@ -248,7 +249,7 @@
 class ProcessInformationView(StartupView):
     id = 'info'
     __select__ = none_rset() & match_user_groups('managers')
-    
+
     title = _('server information')
 
     def call(self, **kwargs):