Commit 524cd007 authored by Jean-Paul Smets's avatar Jean-Paul Smets

Implemented ValidationFailed exception in DC workflows


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@383 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 031bbbf8
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
############################################################################## ##############################################################################
from Products.DCWorkflow.Expression import StateChangeInfo from Products.DCWorkflow.Expression import StateChangeInfo
from Products.PythonScripts.Utility import allow_class
allow_class(StateChangeInfo)
def setWorkflowVariable(self, object, **kw): def setWorkflowVariable(self, object, **kw):
""" """
......
...@@ -456,3 +456,132 @@ class ERP5BTreeFolder2Base (BTreeFolder2Base): ...@@ -456,3 +456,132 @@ class ERP5BTreeFolder2Base (BTreeFolder2Base):
return 0 return 0
BTreeFolder2Base._cleanup = ERP5BTreeFolder2Base._cleanup BTreeFolder2Base._cleanup = ERP5BTreeFolder2Base._cleanup
##############################################################################
# Stribger repair of BTreeFolder2
from Products.DCWorkflow.DCWorkflow import DCWorkflowDefinition, StateChangeInfo, ObjectMoved, createExprContext, aq_parent, aq_inner
from Products.DCWorkflow import DCWorkflow
class ValidationFailed(Exception):
"""Transition can not be executed because data is not in consistent state"""
DCWorkflow.ValidationFailed = ValidationFailed
from AccessControl import ModuleSecurityInfo
ModuleSecurityInfo('Products.DCWorkflow.DCWorkflow').declarePublic('ValidationFailed')
class ERP5DCWorkflowDefinition (DCWorkflowDefinition):
def _executeTransition(self, ob, tdef=None, kwargs=None):
'''
Private method.
Puts object in a new state.
'''
sci = None
econtext = None
moved_exc = None
# Figure out the old and new states.
old_sdef = self._getWorkflowStateOf(ob)
old_state = old_sdef.getId()
if tdef is None:
new_state = self.initial_state
former_status = {}
else:
new_state = tdef.new_state_id
if not new_state:
# Stay in same state.
new_state = old_state
former_status = self._getStatusOf(ob)
new_sdef = self.states.get(new_state, None)
if new_sdef is None:
raise WorkflowException, (
'Destination state undefined: ' + new_state)
# Execute the "before" script.
before_script_success = 1
if tdef is not None and tdef.script_name:
script = self.scripts[tdef.script_name]
# Pass lots of info to the script in a single parameter.
sci = StateChangeInfo(
ob, self, former_status, tdef, old_sdef, new_sdef, kwargs)
try:
script(sci) # May throw an exception.
except ValidationFailed, validation_exc:
before_script_success = 0
before_script_error_message = validation_exc
except ObjectMoved, moved_exc:
ob = moved_exc.getNewObject()
# Re-raise after transition
# Update variables.
state_values = new_sdef.var_values
if state_values is None: state_values = {}
tdef_exprs = None
if tdef is not None: tdef_exprs = tdef.var_exprs
if tdef_exprs is None: tdef_exprs = {}
status = {}
for id, vdef in self.variables.items():
if not vdef.for_status:
continue
expr = None
if state_values.has_key(id):
value = state_values[id]
elif tdef_exprs.has_key(id):
expr = tdef_exprs[id]
elif not vdef.update_always and former_status.has_key(id):
# Preserve former value
value = former_status[id]
else:
if vdef.default_expr is not None:
expr = vdef.default_expr
else:
value = vdef.default_value
if expr is not None:
# Evaluate an expression.
if econtext is None:
# Lazily create the expression context.
if sci is None:
sci = StateChangeInfo(
ob, self, former_status, tdef,
old_sdef, new_sdef, kwargs)
econtext = createExprContext(sci)
value = expr(econtext)
status[id] = value
# Do not proceed in case of failure of before script
if not before_script_success:
status[self.state_var] = old_state # Remain in state
tool = aq_parent(aq_inner(self))
tool.setStatusOf(self.id, ob, status)
sci = StateChangeInfo(
ob, self, status, tdef, old_sdef, new_sdef, kwargs)
sci.setWorkflowVariable(ob, error_message = before_script_error_message)
return new_sdef
# Update state.
status[self.state_var] = new_state
tool = aq_parent(aq_inner(self))
tool.setStatusOf(self.id, ob, status)
# Update role to permission assignments.
self.updateRoleMappingsFor(ob)
# Execute the "after" script.
if tdef is not None and tdef.after_script_name:
script = self.scripts[tdef.after_script_name]
# Pass lots of info to the script in a single parameter.
sci = StateChangeInfo(
ob, self, status, tdef, old_sdef, new_sdef, kwargs)
script(sci) # May throw an exception.
# Return the new state object.
if moved_exc is not None:
# Propagate the notification that the object has moved.
raise moved_exc
else:
return new_sdef
DCWorkflowDefinition._executeTransition = ERP5DCWorkflowDefinition._executeTransition
\ No newline at end of file
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