Commit 07a53d06 authored by Nicolas Delaby's avatar Nicolas Delaby

Add new Interaction Executing Type: Before Commit

this Executing Type allow to execute a interaction script at the end of transaction


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@24180 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 7f7c2d49
......@@ -54,6 +54,7 @@ class InteractionDefinition (SimpleItem):
var_exprs = None # A mapping.
script_name = () # Executed before transition
after_script_name = () # Executed after transition
before_commit_script_name = () #Executed Before Commit Transaction
activate_script_name = () # Executed as activity
method_id = ()
portal_type_filter = None
......@@ -120,6 +121,7 @@ class InteractionDefinition (SimpleItem):
once_per_transaction=False,
script_name=(),
after_script_name=(),
before_commit_script_name=(),
activate_script_name=(),
actbox_name='', actbox_url='',
actbox_category='workflow',
......@@ -149,6 +151,7 @@ class InteractionDefinition (SimpleItem):
self.once_per_transaction = bool(once_per_transaction)
self.script_name = script_name
self.after_script_name = after_script_name
self.before_commit_script_name = before_commit_script_name
self.activate_script_name = activate_script_name
g = Guard()
if g.changeFromProperties(props or REQUEST):
......
......@@ -301,20 +301,27 @@ class InteractionWorkflowDefinition (DCWorkflowDefinition, ActiveObject):
ob, self, former_status, tdef, None, None, kwargs=kw)
script(sci) # May throw an exception
# Execute Before Commit
for script_name in tdef.before_commit_script_name:
method = getattr(self, 'activeScript')
get_transaction().beforeCommitHook(method, script_name,
ob.getRelativeUrl(),
status, tdef.id, kw)
# Execute "activity" scripts
for script_name in tdef.activate_script_name:
self.activate(activity='SQLQueue')\
.activeScript(script_name, ob.getRelativeUrl(), status, tdef.id)
security.declarePrivate('activeScript')
def activeScript(self, script_name, ob_url, status, tdef_id):
def activeScript(self, script_name, ob_url, status, tdef_id, kwargs=None):
script = self.scripts[script_name]
ob = self.unrestrictedTraverse(ob_url)
tdef = self.interactions.get(tdef_id)
sci = StateChangeInfo(
ob, self, status, tdef, None, None, None)
ob, self, status, tdef, None, None, kwargs)
script(sci)
def _getWorkflowStateOf(self, ob, id_only=0):
return None
......
......@@ -81,6 +81,20 @@
</td>
</tr>
<tr>
<th align="left">Script (end of transaction)</th>
<td>
<select name="before_commit_script_name:list" multiple size="5">
<option value="None">(None)</option>
<dtml-in getAvailableScriptIds sort>
<dtml-let selected="_['sequence-item'] in before_commit_script_name and 'selected' or ' '">
<option value="&dtml-sequence-item;" &dtml-selected;>&dtml-sequence-item;</option>
</dtml-let>
</dtml-in>
</select>
</td>
</tr>
<tr>
<th align="left">Activities (later)</th>
<td>
......
......@@ -81,13 +81,16 @@ class TestInteractionWorkflow(ERP5TypeTestCase):
def createInteractionWorkflow(self):
id = 'test_workflow'
wf_type = "interaction_workflow (Web-configurable interaction workflow)"
self.getWorkflowTool().manage_addWorkflow(workflow_type=wf_type,id=id)
if getattr(self.getWorkflowTool(), id, None) is None:
self.getWorkflowTool().manage_addWorkflow(workflow_type=wf_type,id=id)
wf = self.getWorkflowTool()[id]
self.wf = wf
wf.scripts.manage_addProduct['PythonScripts']\
.manage_addPythonScript(id='afterEdit')
if getattr(wf.scripts, 'afterEdit', None) is None:
wf.scripts.manage_addProduct['PythonScripts']\
.manage_addPythonScript(id='afterEdit')
self.script = wf.scripts['afterEdit']
wf.interactions.addInteraction(id='edit_interaction')
if getattr(wf.interactions, 'edit_interaction', None) is None:
wf.interactions.addInteraction(id='edit_interaction')
self.interaction = wf.interactions['edit_interaction']
self.getWorkflowTool().setChainForPortalTypes(
[self.portal_type],'test_workflow, validation_workflow')
......@@ -496,6 +499,34 @@ context.setDescription('%s,%s,%s' % (d, args, result))
value = organisation.getDescription()
self.assertEquals(value, "toto,('description',),bad")
def test_16_BeforeCommitParameters(self, quiet=0, run=run_all_test):
if not run: return
if not quiet:
self.logMessage('Before Commit Script Parameters')
self.createInteractionWorkflow()
self.interaction.setProperties(
'beforeCommit',
method_id='getProperty',
before_commit_script_name=('afterEdit',))
params = 'sci, **kw'
body = """\
context = sci['object']
kwargs = sci['kwargs'] or {}
d = kwargs.get('d', None)
args = kwargs.get('workflow_method_args', ())
result = kwargs.get('workflow_method_result', None)
context.setDescription('%s,%s,%s' % (d, args, result))
"""
self.script.ZPythonScript_edit(params, body)
self.createData()
organisation = self.organisation
organisation.setDescription('bad')
self.assertEquals(organisation.getDescription(), 'bad')
organisation.getProperty('description', d='toto')
self.assertEquals(organisation.getDescription(), 'bad')
get_transaction().commit()
self.assertEquals(organisation.getDescription(), "toto,('description',),bad")
def test_regular_expression(self):
# test that we can add an interaction by defining methods using regular
# expression
......
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