[Checkins] SVN: grok/trunk/src/grok/ (working with JW). Fix a bug where the formlib support was not

Martijn Faassen faassen at infrae.com
Thu Nov 30 16:12:09 EST 2006


Log message for revision 71340:
  (working with JW). Fix a bug where the formlib support was not
  taking field order into account.
  

Changed:
  U   grok/trunk/src/grok/components.py
  U   grok/trunk/src/grok/tests/form/form.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2006-11-30 16:52:11 UTC (rev 71339)
+++ grok/trunk/src/grok/components.py	2006-11-30 21:12:09 UTC (rev 71340)
@@ -46,9 +46,9 @@
 from grok import util, security, interfaces
 
 
-class Model(Contained, persistent.Persistent):  
-    # XXX Inheritance order is important here. If we reverse this, 
-    # then containers can't be models anymore because no unambigous MRO 
+class Model(Contained, persistent.Persistent):
+    # XXX Inheritance order is important here. If we reverse this,
+    # then containers can't be models anymore because no unambigous MRO
     # can be established.
     pass
 
@@ -59,7 +59,7 @@
 
 class Site(SiteManagerContainer):
     pass
-    
+
 class Adapter(object):
 
     def __init__(self, context):
@@ -76,7 +76,7 @@
 
 class View(BrowserPage):
     interface.implements(interfaces.IGrokView)
-    
+
     def __init__(self, context, request):
         # Jim would say: WAAAAAAAAAAAAH!
         self.context = removeSecurityProxy(context)
@@ -113,7 +113,7 @@
                     'or string argument')
             name = obj
             obj = None
-            
+
         if name is None and obj is None:
             # create URL to view itself
             obj = self
@@ -130,7 +130,7 @@
 
     def redirect(self, url):
         return self.request.response.redirect(url)
-    
+
     def before(self):
         pass
 
@@ -140,8 +140,8 @@
         return getattr(context, '__view_name__', None)
     # XXX breadcrumbs method on AbsoluteURL breaks as it does not use
     # _getContextName to get to the name of the view. What does breadcrumbs do?
-    
 
+
 class XMLRPC(object):
     pass
 
@@ -247,7 +247,7 @@
                 return result
         # try to get the item from the container
         return self.context.get(name)
-        
+
 class Form(View):
     def _init(self):
         fields = schema_fields(self.context)
@@ -275,12 +275,15 @@
 def schema_fields(obj):
     fields = []
     fields_class = getattr(obj, 'fields', None)
-    if fields_class is not None:
-        if type(fields_class) == types.ClassType:
-            for name in dir(fields_class):
-                field = getattr(fields_class, name)
-                if IField.providedBy(field):
-                    if not getattr(field, '__name__', None):
-                        field.__name__ = name
-                    fields.append(field)
+    if fields_class is None:
+        return fields
+    if type(fields_class) != types.ClassType:
+        return fields
+    for name in dir(fields_class):
+        field = getattr(fields_class, name)
+        if IField.providedBy(field):
+            if not getattr(field, '__name__', None):
+                field.__name__ = name
+            fields.append(field)
+    fields.sort(key=lambda field: field.order)
     return fields

Modified: grok/trunk/src/grok/tests/form/form.py
===================================================================
--- grok/trunk/src/grok/tests/form/form.py	2006-11-30 16:52:11 UTC (rev 71339)
+++ grok/trunk/src/grok/tests/form/form.py	2006-11-30 21:12:09 UTC (rev 71340)
@@ -41,6 +41,16 @@
   2
   >>> [w.__name__ for w in view.form_fields]
   ['name', 'size']
+
+It is important to keep the order of the fields:
+
+  >>> view = component.getMultiAdapter(
+  ...    (DifferentMammoth(), request), name='editdifferent')
+  >>> len(view.form_fields)
+  2
+  >>> [w.__name__ for w in view.form_fields]
+  ['size', 'name']
+
 """
 import grok
 from zope import schema
@@ -51,10 +61,18 @@
         size = schema.TextLine(title=u"Size", default=u"Quite normal")
         somethingelse = None
 
-grok.context(Mammoth)
 
 class Edit(grok.EditForm):
-    pass
+    grok.context(Mammoth)
 
 class Cave(grok.Model):
     fields = ['ignored']
+
+class DifferentMammoth(grok.Model):
+    class fields:
+        # mind the different order of fields
+        size = schema.TextLine(title=u"Size", default=u"Quite normal")
+        name = schema.TextLine(title=u"Name")
+
+class EditDifferent(grok.EditForm):
+    grok.context(DifferentMammoth)



More information about the Checkins mailing list