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