[formfields] allow fields ordering with autoform_field_kwargs stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 18 Nov 2009 12:50:23 +0100
branchstable
changeset 3874 7d0d4a6be046
parent 3873 4d95109582c7
child 3878 3af225f0faa0
[formfields] allow fields ordering with autoform_field_kwargs
web/formfields.py
web/views/autoform.py
--- a/web/formfields.py	Wed Nov 18 12:32:57 2009 +0100
+++ b/web/formfields.py	Wed Nov 18 12:50:23 2009 +0100
@@ -81,6 +81,8 @@
        role of the entity in the relation (eg 'subject' or 'object')
     :fieldset:
        optional fieldset to which this field belongs to
+    :order:
+       key used by automatic forms to sort fields
 
     """
     # default widget associated to this class of fields. May be overriden per
@@ -94,7 +96,7 @@
     def __init__(self, name=None, id=None, label=None, help=None,
                  widget=None, required=False, initial=None,
                  choices=None, sort=True, internationalizable=False,
-                 eidparam=False, role='subject', fieldset=None):
+                 eidparam=False, role='subject', fieldset=None, order=None):
         self.name = name
         self.id = id or name
         self.label = label or name
@@ -108,6 +110,7 @@
         self.role = role
         self.fieldset = fieldset
         self.init_widget(widget)
+        self.order = order
         # ordering number for this field instance
         self.creation_rank = Field.__creation_rank
         Field.__creation_rank += 1
--- a/web/views/autoform.py	Wed Nov 18 12:32:57 2009 +0100
+++ b/web/views/autoform.py	Wed Nov 18 12:50:23 2009 +0100
@@ -180,6 +180,8 @@
                 except form.FieldNotFound:
                     # meta attribute such as <attr>_format
                     continue
+        fnum = len(self.fields)
+        self.fields.sort(key=lambda f: f.order is None and fnum or f.order)
         self.maxrelitems = self.req.property_value('navigation.related-limit')
         self.force_display = bool(self.req.form.get('__force_display'))