field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary tls-sprint
authorsylvain.thenault@logilab.fr
Fri, 27 Mar 2009 17:09:43 +0100
branchtls-sprint
changeset 1183 62afd820d3ae
parent 1182 78e4080252be
child 1184 a33deae7c6a2
field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
web/form.py
web/views/editforms.py
--- a/web/form.py	Fri Mar 27 17:08:27 2009 +0100
+++ b/web/form.py	Fri Mar 27 17:09:43 2009 +0100
@@ -220,13 +220,13 @@
         self.context = None
 
     @iclassmethod
-    def field_by_name(cls_or_self, name):
+    def field_by_name(cls_or_self, name, role='subject'):
         if isinstance(cls_or_self, type):
             fields = cls_or_self._fields_
         else:
             fields = cls_or_self.fields
         for field in fields:
-            if field.name == name:
+            if field.name == name and field.role == role:
                 return field
         raise Exception('field %s not found' % name)
     
@@ -309,7 +309,7 @@
     def form_field_id(self, field):
         return field.id
    
-    def form_field_vocabulary(self, field):
+    def form_field_vocabulary(self, field, limit=None):
         raise NotImplementedError
 
     def form_buttons(self):
--- a/web/views/editforms.py	Fri Mar 27 17:08:27 2009 +0100
+++ b/web/views/editforms.py	Fri Mar 27 17:09:43 2009 +0100
@@ -11,6 +11,7 @@
 
 from simplejson import dumps
 
+from logilab.common.decorators import iclassmethod
 from logilab.mtconverter import html_escape
 
 from cubicweb import typed_eid
@@ -292,7 +293,20 @@
                 continue
             result.append((rschema.display_name(entity.req, role), rschema, role))
         return sorted(result)
-                        
+    
+    @iclassmethod
+    def field_by_name(cls_or_self, name, role='subject', eclass=None):
+        try:
+            return super(AutomaticEntityForm, cls_or_self, name, role)
+        except Exception: # XXX should raise more explicit exception
+            if eclass is None:
+                raise
+            field = guess_field(eclass, cls_or_self.schema.rschema(name), role,
+                                eidparam=True)
+            if field is None:
+                raise
+            raise
+            
     
     def __init__(self, *args, **kwargs):
         super(AutomaticEntityForm, self).__init__(*args, **kwargs)