Commit b8881e47 authored by iv's avatar iv

ERP5Workflow: call getStateChangeInformation once if multiple after/before/... scripts

parent 2b58c8fb
...@@ -259,11 +259,13 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow): ...@@ -259,11 +259,13 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
sci = StateChangeInfo( sci = StateChangeInfo(
ob, self, former_status, tdef, None, None, kwargs=kw) ob, self, former_status, tdef, None, None, kwargs=kw)
for script in tdef.getBeforeScriptValueList(): script_value_list = tdef.getBeforeScriptValueList()
if script: if script_value_list:
script_context = self._asScriptContext() script_context = self._asScriptContext()
for script in script_value_list:
if script:
script = getattr(script_context, script.id) script = getattr(script_context, script.id)
script(sci) script(sci) # May throw an exception.
return filtered_transition_list return filtered_transition_list
def notifySuccess(self, ob, transition_list, result, args=None, kw=None): def notifySuccess(self, ob, transition_list, result, args=None, kw=None):
...@@ -326,12 +328,13 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow): ...@@ -326,12 +328,13 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
ob, self, former_status, tdef, None, None, kwargs=kw) ob, self, former_status, tdef, None, None, kwargs=kw)
# Execute the "after" script. # Execute the "after" script.
after_script_list = tdef.getAfterScriptValueList() after_script_value_list = tdef.getAfterScriptValueList()
for script in after_script_list: if after_script_value_list:
if script:
script_context = self._asScriptContext() script_context = self._asScriptContext()
for script in after_script_value_list:
if script:
script = getattr(script_context, script.id) script = getattr(script_context, script.id)
script(sci) # May throw an exception script(sci) # May throw an exception.
# Queue the "Before Commit" scripts # Queue the "Before Commit" scripts
sm = getSecurityManager() sm = getSecurityManager()
...@@ -342,10 +345,12 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow): ...@@ -342,10 +345,12 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
# Execute "activity" scripts # Execute "activity" scripts
activity_script_list = tdef.getActivateScriptValueList() activity_script_list = tdef.getActivateScriptValueList()
if activity_script_list:
script_context = self._asScriptContext()
for script in activity_script_list: for script in activity_script_list:
self.activate(activity='SQLQueue')\ self.activate(activity='SQLQueue')\
.activeScript(script.id, ob.getRelativeUrl(), .activeScript(script.id, ob.getRelativeUrl(), status, tdef.getId(),
status, tdef.getId()) script_context=script_context)
def _before_commit(self, sci, script_name, security_manager): def _before_commit(self, sci, script_name, security_manager):
# check the object still exists before calling the script # check the object still exists before calling the script
...@@ -364,7 +369,9 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow): ...@@ -364,7 +369,9 @@ class InteractionWorkflow(IdAsReferenceMixin("", "prefix"), Workflow):
finally: finally:
setSecurityManager(current_security_manager) setSecurityManager(current_security_manager)
def activeScript(self, script_name, ob_url, former_status, tdef_id): def activeScript(self, script_name, ob_url, former_status, tdef_id,
script_context=None):
if script_context is None:
script_context = self._asScriptContext() script_context = self._asScriptContext()
script = getattr(script_context, script_name) script = getattr(script_context, script_name)
ob = self.unrestrictedTraverse(ob_url) ob = self.unrestrictedTraverse(ob_url)
......
...@@ -734,12 +734,13 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin): ...@@ -734,12 +734,13 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin):
kwargs = form_kw kwargs = form_kw
sci = StateChangeInfo(document, self, former_status, tdef, old_sdef, sci = StateChangeInfo(document, self, former_status, tdef, old_sdef,
new_sdef, kwargs) new_sdef, kwargs)
if script_value_list:
script_context = self._asScriptContext()
for script in script_value_list: for script in script_value_list:
# Pass lots of info to the script in a single parameter. # Pass lots of info to the script in a single parameter.
if script.getPortalType() != 'Workflow Script': if script.getPortalType() != 'Workflow Script':
raise NotImplementedError ('Unsupported Script %s for state %s' % raise NotImplementedError ('Unsupported Script %s for state %s' %
(script.id, old_sdef.getReference())) (script.id, old_sdef.getReference()))
script_context = self._asScriptContext()
script = getattr(script_context, script.id) script = getattr(script_context, script.id)
try: try:
script(sci) # May throw an exception. script(sci) # May throw an exception.
...@@ -754,7 +755,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin): ...@@ -754,7 +755,7 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin):
# update variables # update variables
state_values = None state_values = None
# seems state variable is not used in new workflow. # seems state variable is not used in new workflow.
object = self.getStateChangeInformation(document, source_value) object_context = None
if new_sdef is not None: if new_sdef is not None:
state_values = getattr(new_sdef,'var_values', None) state_values = getattr(new_sdef,'var_values', None)
if state_values is None: if state_values is None:
...@@ -769,7 +770,9 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin): ...@@ -769,7 +770,9 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin):
# Update all transition variables # Update all transition variables
if form_kw is not None: if form_kw is not None:
object.REQUEST.other.update(form_kw) if object_context is None:
object_context = self.getStateChangeInformation(document, source_value)
object_context.REQUEST.other.update(form_kw)
kwargs = form_kw kwargs = form_kw
for vdef in self.objectValues(portal_type='Workflow Variable'): for vdef in self.objectValues(portal_type='Workflow Variable'):
...@@ -790,7 +793,9 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin): ...@@ -790,7 +793,9 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin):
if variable_expression is not None: if variable_expression is not None:
expr = variable_expression expr = variable_expression
else: else:
value = vdef.getVariableValue(object=object) if object_context is None:
object_context = self.getStateChangeInformation(document, source_value)
value = vdef.getVariableValue(object=object_context)
if expr not in (None, ''): if expr not in (None, ''):
# Evaluate an expression. # Evaluate an expression.
if econtext is None: if econtext is None:
...@@ -830,14 +835,17 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin): ...@@ -830,14 +835,17 @@ class Workflow(IdAsReferenceMixin("", "prefix"), XMLObject, CacheCookieMixin):
sci = StateChangeInfo(document, self, former_status, tdef, old_sdef, sci = StateChangeInfo(document, self, former_status, tdef, old_sdef,
new_sdef, kwargs) new_sdef, kwargs)
old_state_destination_list = old_sdef.getDestinationValueList() old_state_destination_list = old_sdef.getDestinationValueList()
script_context = None
for script in script_value_list: for script in script_value_list:
# Script can be either script or workflow method # Script can be either script or workflow method
script_value = self._getOb(script.id)
if script in old_state_destination_list and \ if script in old_state_destination_list and \
self._getOb(script.id).getTriggerType() == TRIGGER_WORKFLOW_METHOD: script_value.getTriggerType() == TRIGGER_WORKFLOW_METHOD:
getattr(document, convertToMixedCase(self._getOb(script.id).getReference()))() getattr(document, convertToMixedCase(script_value.getReference()))()
else: else:
# Pass lots of info to the script in a single parameter. # Pass lots of info to the script in a single parameter.
if script.getPortalType() == 'Workflow Script': if script.getPortalType() == 'Workflow Script':
if script_context is None:
script_context = self._asScriptContext() script_context = self._asScriptContext()
script = getattr(script_context, script.id) script = getattr(script_context, script.id)
script(sci) # May throw an exception. script(sci) # May throw an exception.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment