From 766c9a0106bfbb26c827ecb1c92c7534531b8fd4 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Tue, 13 Dec 2011 10:51:14 +0100
Subject: [PATCH] Simplify Delivery.getMovementList

---
 product/ERP5/Document/Delivery.py | 88 +++++++++++++------------------
 1 file changed, 36 insertions(+), 52 deletions(-)

diff --git a/product/ERP5/Document/Delivery.py b/product/ERP5/Document/Delivery.py
index 54ac96809e..97905f0bc2 100644
--- a/product/ERP5/Document/Delivery.py
+++ b/product/ERP5/Document/Delivery.py
@@ -241,58 +241,42 @@ class Delivery(XMLObject, ImmobilisationDelivery,
       """
       Return a list of movements
       """
-      movement_portal_type_list = self.getPortalMovementTypeList()
-      sub_object_list = self.objectValues(
-          portal_type=movement_portal_type_list, **kw)
-      if not sub_object_list:
-        return []
-
-      if isinstance(portal_type, str):
-        portal_type = set((portal_type,))
-      elif isinstance(portal_type, (list, tuple)):
-        portal_type = set(portal_type)
-
-      movement_list = []
-      add_movement = movement_list.append
-      object_list_stack = [sub_object_list]
-      stack_index = 0
-      object_list_index_stack = []
-      object_index = 0
-      while object_list_stack:
-        try:
-          sub_object = object_list_stack[stack_index][object_index]
-        except IndexError:
-          object_list_stack.pop()
-          stack_index -= 1
-          if object_list_index_stack:
-            object_index = object_list_index_stack.pop()
-        else:
-          content_list = sub_object.objectValues(
-              portal_type=movement_portal_type_list, **kw)
-
-          new_stack = []
-          if sub_object.hasCellContent():
-            cell_list = sub_object.getCellValueList()
-            if len(cell_list) != len(content_list):
-              for x in content_list:
-                if x not in cell_list:
-                  new_stack.append(x)
-            else:
-              for sub_object in content_list:
-                if (portal_type is None or
-                    sub_object.getPortalType() in portal_type):
-                  add_movement(sub_object)
-          elif content_list:
-            new_stack = content_list
-          elif portal_type is None or sub_object.getPortalType() in portal_type:
-            add_movement(sub_object)
-
-          object_index += 1
-          if new_stack:
-            object_list_stack.append(new_stack)
-            object_list_index_stack.append(object_index)
-            stack_index += 1
-            object_index = 0
+      movement_portal_type_set = set(
+        self.getPortalObject().getPortalMovementTypeList())
+      movement_list = self.objectValues(
+        portal_type=movement_portal_type_set, **kw)
+      if movement_list:
+
+        if isinstance(portal_type, str):
+          portal_type = set((portal_type,))
+        elif isinstance(portal_type, (list, tuple)):
+          portal_type = set(portal_type)
+
+        # Browse lines recursively and collect leafs.
+        stack = [iter(movement_list)]
+        movement_list = []
+        while stack:
+          for sub_object in stack[-1]:
+            content_list = sub_object.objectValues(
+              portal_type=movement_portal_type_set, **kw)
+            if sub_object.hasCellContent():
+              cell_list = sub_object.getCellValueList()
+              if len(cell_list) != len(content_list):
+                content_list = set(content_list).difference(cell_list)
+                if content_list:
+                  stack.append(iter(content_list))
+                  break
+              else:
+                movement_list.extend(x for x in content_list
+                  if portal_type is None or x.getPortalType() in portal_type)
+            elif content_list:
+              stack.append(iter(content_list))
+              break
+            elif portal_type is None or \
+                 sub_object.getPortalType() in portal_type:
+              movement_list.append(sub_object)
+          else:
+            del stack[-1]
 
       return movement_list
     
-- 
2.30.9