From b40fe9018deb193075e697ff8fd53a1a21d675dd Mon Sep 17 00:00:00 2001
From: iv <isabelle.vallet@nexedi.com>
Date: Mon, 5 Dec 2016 09:44:53 +0000
Subject: [PATCH] ERP5Workflow: PERF: avoid calling multiple times the same
 methods

---
 .../Document/InteractionWorkflow.py           |  5 +--
 product/ERP5Workflow/Document/Workflow.py     | 35 +++++++++----------
 product/ERP5Workflow/Document/Worklist.py     | 11 +++---
 product/ERP5Workflow/Tool/WorkflowTool.py     | 13 ++++---
 4 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/product/ERP5Workflow/Document/InteractionWorkflow.py b/product/ERP5Workflow/Document/InteractionWorkflow.py
index 0d68addebe..7927580d72 100644
--- a/product/ERP5Workflow/Document/InteractionWorkflow.py
+++ b/product/ERP5Workflow/Document/InteractionWorkflow.py
@@ -243,8 +243,7 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
       sci = StateChangeInfo(
       ob, self, former_status, tdef, None, None, kwargs=kw)
 
-      before_script_list = tdef.getBeforeScriptValueList()
-      for script in before_script_list:
+      for script in tdef.getBeforeScriptValueList():
         if script:
           script_context = self._asScriptContext()
           script = getattr(script_context, script.id)
@@ -486,8 +485,6 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
           sub_object = SubElement(variable, property_id, attrib=dict(type='int'))
         elif property_id == 'variable_value':
           property_value = vdef.getVariableValue()
-          if vdef.getVariableValue() is not None:
-            property_value = vdef.getVariableValue()
           sub_object = SubElement(variable, property_id, attrib=dict(type='string'))
         else:
           property_value = vdef.getProperty(property_id)
diff --git a/product/ERP5Workflow/Document/Workflow.py b/product/ERP5Workflow/Document/Workflow.py
index 4b50b16287..0bf2143439 100644
--- a/product/ERP5Workflow/Document/Workflow.py
+++ b/product/ERP5Workflow/Document/Workflow.py
@@ -197,11 +197,8 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
       Returns a true value if the given info name is supported.
       '''
       if name == self.getStateVariable():
-          return 1
-      vdef = self.getVariableValueDict().get(name, None)
-      if vdef is None:
-          return 0
-      return 1
+          return True
+      return self.getVariableValueDict().get(name, None) is not None
 
   def _checkTransitionGuard(self, transition, document, **kw):
     return transition.checkGuard(getSecurityManager(), self, document, **kw)
@@ -378,7 +375,8 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
       (worklist id as key) and which value is a dict composed of
       variable matches.
     """
-    if not self.getWorklistValueList():
+    worklist_value_list = self.getWorklistValueList()
+    if not worklist_value_list:
       return None
 
     portal = self.getPortalObject()
@@ -397,7 +395,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
     security_manager = getSecurityManager()
     workflow_id = self.getId()
     workflow_title = self.getTitle()
-    for worklist_value in self.getWorklistValueList():
+    for worklist_value in worklist_value_list:
       action_box_name = worklist_value.getActionName()
       is_guarded = worklist_value.isGuarded()
       guard_role_list = worklist_value.getGuardRoleList()
@@ -623,14 +621,14 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
     state_var = self.getStateVariable()
     status_dict = self.getCurrentStatusDict(document)
     current_state_value = self._getWorkflowStateOf(document, id_only=0)
-
+    source_value = self.getSourceValue()
     if current_state_value == None:
-      current_state_value = self.getSourceValue()
+      current_state_value = source_value
     old_state = current_state_value.getReference()
     old_sdef = current_state_value
 
     if tdef is None:
-      new_sdef = self.getSourceValue()
+      new_sdef = source_value
       new_state = new_sdef.getReference()
 
       if not new_sdef:
@@ -675,7 +673,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
     # update variables
     state_values = None
     # seems state variable is not used in new workflow.
-    object = self.getStateChangeInformation(document, self.getSourceValue())
+    object = self.getStateChangeInformation(document, source_value)
     if new_sdef is not None:
       state_values = getattr(new_sdef,'var_values', None)
     if state_values is None:
@@ -840,8 +838,9 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
           value = tuple(self.getProperty('workflow_managed_permission'))
           prop_type = self.getPropertyType('workflow_managed_permission')
         elif prop_id == 'initial_state':
-          if self.getSourceValue() is not None:
-            value = self.getSourceValue().getReference()
+          source_value = self.getSourceValue()
+          if source_value is not None:
+            value = source_value.getReference()
           else:
             value = ''
           prop_type = 'string'
@@ -974,8 +973,6 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
           sub_object = SubElement(variable, property_id, attrib=dict(type='int'))
         elif property_id == 'variable_value':
           property_value = vdef.getVariableValue()
-          if vdef.getVariableValue() is not None:
-            property_value = vdef.getVariableValue()
           sub_object = SubElement(variable, property_id, attrib=dict(type='string'))
         else:
           property_value = vdef.getProperty(property_id)
@@ -1107,7 +1104,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
 
     tdef_exprs = {}
     status = {}
-    for id, vdef in self.getVariableValueDict().items():
+    for id, vdef in state_values.items():
       if not vdef.getStatusIncluded():
         continue
       expr = None
@@ -1184,9 +1181,9 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject):
             value = variable_expression(ec)
           else:
             value = variable.getVariableValue()
-      if hasattr(self, 'getSourceValue'):
-        if self.getSourceValue() is not None:
-          initial_state = self.getSourceValue().getReference()
+      source_value = self.getSourceValue()
+      if source_value is not None:
+        initial_state = source_value.getReference()
       if state_var is not None:
         res[state_var] = status.get(state_var, initial_state)
       return res
diff --git a/product/ERP5Workflow/Document/Worklist.py b/product/ERP5Workflow/Document/Worklist.py
index 70eb971490..557febfb7f 100644
--- a/product/ERP5Workflow/Document/Worklist.py
+++ b/product/ERP5Workflow/Document/Worklist.py
@@ -162,9 +162,11 @@ class Worklist(IdAsReferenceMixin("worklist_", "prefix"), XMLObject,
           elif id == 'simulation_state':
             matches_id_list = self.getMatchedSimulationStateList()
           # Get workflow state's reference:
+          parent = self.getParent()
           for state_id in matches_id_list:
-            if hasattr(self.getParent(), state_id):
-              matches_ref_list.append(self.getParent()._getOb(state_id).getReference())
+            state = getattr(parent, state_id, None)
+            if state is not None:
+              matches_ref_list.append(state.getReference())
             else: matches_ref_list = matches_id_list
           matches = tuple(matches_ref_list)
         elif id == 'causality_state':
@@ -174,8 +176,9 @@ class Worklist(IdAsReferenceMixin("worklist_", "prefix"), XMLObject,
         elif id:
           # Local dynamic variable:
           dynamic_variable = self._getOb('variable_'+id)
-          if dynamic_variable.getVariableValue():
-            matches = [dynamic_variable.getVariableValue()]
+          dynamic_variable_value = dynamic_variable.getVariableValue()
+          if dynamic_variable_value:
+            matches = [dynamic_variable_value]
           # Override initial value if expression set:
           dynamic_variable_expression_text = dynamic_variable\
               .getVariableExpression()
diff --git a/product/ERP5Workflow/Tool/WorkflowTool.py b/product/ERP5Workflow/Tool/WorkflowTool.py
index 5646b5c4fd..c9035086c1 100644
--- a/product/ERP5Workflow/Tool/WorkflowTool.py
+++ b/product/ERP5Workflow/Tool/WorkflowTool.py
@@ -229,13 +229,17 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool):
     else:
         portal_type = None
 
-    # Workflow assignment:
+    portal_type_workflow_list = tuple()
     if portal_type is not None:
-      for workflow_id in portal_type.getTypeWorkflowList():
+      portal_type_workflow_list = portal_type.getTypeWorkflowList()
+
+      # Workflow assignment:
+      for workflow_id in portal_type_workflow_list:
         workflow_list.append(self._getOb(workflow_id))
+
     # DCWorkflow assignment
     for wf_id in self.getChainFor(ob):
-      if portal_type is not None and wf_id in portal_type.getTypeWorkflowList():
+      if wf_id in portal_type_workflow_list:
         continue
       wf = self.getWorkflowById(wf_id)
       if wf is not None:
@@ -248,8 +252,7 @@ class WorkflowTool(BaseTool, OriginalWorkflowTool):
       """ Get the history of an object for a given workflow.
       """
       if hasattr(aq_base(ob), 'workflow_history'):
-          wfh = ob.workflow_history
-          return wfh.get(wf_id, None)
+          return ob.workflow_history.get(wf_id, None)
       return ()
 
   def _encodeWorkflowUid(self, id):
-- 
2.30.9