web/formfields.py
branchstable
changeset 3334 8d831c02da9a
parent 3308 88f5f89d8d1b
child 3337 09d98666f9b3
--- a/web/formfields.py	Fri Sep 18 19:51:45 2009 +0200
+++ b/web/formfields.py	Mon Sep 21 11:41:17 2009 +0200
@@ -22,6 +22,22 @@
     Radio, Select, DateTimePicker)
 
 
+def vocab_sort(vocab):
+    """sort vocabulary, considering option groups"""
+    result = []
+    partresult = []
+    for label, value in vocab:
+        if value is None: # opt group start
+            if partresult:
+                result += sorted(partresult)
+                partresult = []
+            result.append( (label, value) )
+        else:
+            partresult.append( (label, value) )
+    result += sorted(partresult)
+    return result
+
+
 class Field(object):
     """field class is introduced to control what's displayed in forms. It makes
     the link between something to edit and its display in the form. Actual
@@ -190,7 +206,7 @@
         if self.internationalizable:
             vocab = [(form.req._(label), value) for label, value in vocab]
         if self.sort:
-            vocab = sorted(vocab)
+            vocab = vocab_sort(vocab)
         return vocab
 
     def form_init(self, form):
@@ -475,7 +491,7 @@
             relatedvocab = []
         vocab = res + form.form_field_vocabulary(self) + relatedvocab
         if self.sort:
-            vocab = sorted(vocab)
+            vocab = vocab_sort(vocab)
         return vocab
 
     def format_single_value(self, req, value):