[Erp5-report] r17919 - in /erp5/trunk/products/CMFCategory: ./ tests/

nobody at svn.erp5.org nobody at svn.erp5.org
Fri Nov 30 13:22:42 CET 2007


Author: jerome
Date: Fri Nov 30 13:22:41 2007
New Revision: 17919

URL: http://svn.erp5.org?rev=17919&view=rev
Log:
support a list as local_sort_id

Modified:
    erp5/trunk/products/CMFCategory/Category.py
    erp5/trunk/products/CMFCategory/tests/testCMFCategory.py

Modified: erp5/trunk/products/CMFCategory/Category.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFCategory/Category.py?rev=17919&r1=17918&r2=17919&view=diff
==============================================================================
--- erp5/trunk/products/CMFCategory/Category.py (original)
+++ erp5/trunk/products/CMFCategory/Category.py Fri Nov 30 13:22:41 2007
@@ -250,7 +250,9 @@
 
           local_sort_id     - When using the default preorder traversal, sort
                               objects of the same depth by comparing their
-                              'local_sort_id' property.
+                              'local_sort_id' property. local_sort_id can be a
+                              list, in this case properties are compared in the
+                              same order than this list.
 
           Renderer parameters are also supported here.
       """
@@ -263,8 +265,17 @@
 
       child_value_list = self.objectValues(self.allowed_types)
       if local_sort_id:
-        local_sort_method = lambda a, b: cmp(a.getProperty(local_sort_id, 0),
-                                             b.getProperty(local_sort_id, 0))
+        if isinstance(local_sort_id, (tuple, list)):
+          def sort_method(a, b):
+            for sort_id in local_sort_id:
+              diff = cmp(a.getProperty(sort_id, 0), b.getProperty(sort_id, 0))
+              if diff != 0:
+                return diff
+            return 0
+          local_sort_method = sort_method
+        else:
+          local_sort_method = lambda a, b: cmp(a.getProperty(local_sort_id, 0),
+                                               b.getProperty(local_sort_id, 0))
       if local_sort_method:
         # sort objects at the current level
         child_value_list = list(child_value_list)
@@ -272,8 +283,8 @@
 
       if recursive:
         for c in child_value_list:
-          # Do not global pass sort parameters intentionally, because sorting
-          # needs to be done only at the end of recursive calls.
+          # Do not pass sort_on / sort_order parameters intentionally, because
+          # sorting needs to be done only at the end of recursive calls.
           value_list.extend(c.getCategoryChildValueList(recursive=1,
                                        is_self_excluded=0,
                                        include_if_child=include_if_child,
@@ -758,7 +769,9 @@
           
           local_sort_id     - When using the default preorder traversal, sort
                               objects of the same depth by comparing their
-                              'local_sort_id' property.
+                              'local_sort_id' property. local_sort_id can be a
+                              list, in this case properties are compared in the
+                              same order than this list.
           
           Renderer parameters are also supported here.
 
@@ -770,7 +783,16 @@
 
       child_value_list = self.objectValues(self.allowed_types)
       if local_sort_id:
-        local_sort_method = lambda a, b: cmp(a.getProperty(local_sort_id, 0),
+        if isinstance(local_sort_id, (tuple, list)):
+          def sort_method(a, b):
+            for sort_id in local_sort_id:
+              diff = cmp(a.getProperty(sort_id, 0), b.getProperty(sort_id, 0))
+              if diff != 0:
+                return diff
+            return 0
+          local_sort_method = sort_method
+        else:
+          local_sort_method = lambda a, b: cmp(a.getProperty(local_sort_id, 0),
                                              b.getProperty(local_sort_id, 0))
       if local_sort_method:
         # sort objects at the current level

Modified: erp5/trunk/products/CMFCategory/tests/testCMFCategory.py
URL: http://svn.erp5.org/erp5/trunk/products/CMFCategory/tests/testCMFCategory.py?rev=17919&r1=17918&r2=17919&view=diff
==============================================================================
--- erp5/trunk/products/CMFCategory/tests/testCMFCategory.py (original)
+++ erp5/trunk/products/CMFCategory/tests/testCMFCategory.py Fri Nov 30 13:22:41 2007
@@ -899,6 +899,21 @@
     self.assertEquals(list(c1.getCategoryChildValueList(
                               local_sort_id='int_index')), [c12, c11, c111])
 
+    # local_sort_id can be a list, in this case document are sorted with the
+    # first sort_id in the list, then the second, and so on.
+
+    # When we use those category properties:
+    #        int_index     title
+    #   c1                   C
+    #   c2                   B
+    #   c3      1            A
+    c1.setIntIndex(None)
+    c2.setIntIndex(None)
+    # and we sort on int_index then title, we should have this order:
+    self.assertEquals(list(bc.getCategoryChildValueList(
+                          local_sort_id=['int_index', 'title'])),
+                      [c2, c1, c12, c11, c111, c3])
+
   
   def test_25_getCategoryChildItemList_base_parameter(self):
     pc = self.getCategoriesTool()




More information about the Erp5-report mailing list