Commit 8e10c692 authored by Nicolas Dumazet's avatar Nicolas Dumazet

identation: use 2-spaces everywhere instead of a 2/4/6 mix


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@42722 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a38c1884
...@@ -34,320 +34,320 @@ from Products.ERP5Type import Permissions ...@@ -34,320 +34,320 @@ from Products.ERP5Type import Permissions
_MARKER = [] _MARKER = []
class InteractionWorkflowDefinition (DCWorkflowDefinition, ActiveObject): class InteractionWorkflowDefinition (DCWorkflowDefinition, ActiveObject):
""" """
The InteractionTool implements portal object The InteractionTool implements portal object
interaction policies. interaction policies.
An interaction is defined by
a domain and a behaviour:
The domain is defined as:
- the meta_type it applies to
- the portal_type it applies to
- the conditions of application (category membership, value range,
security, function, etc.)
An interaction is defined by The transformation template is defined as:
a domain and a behaviour:
The domain is defined as: - pre method executed before
- the meta_type it applies to - pre async executed anyway
- the portal_type it applies to - post method executed after success before return
- the conditions of application (category membership, value range, - post method executed after success anyway
security, function, etc.)
The transformation template is defined as: This is similar to signals and slots except is applies to classes
rather than instances. Similar to
stateless workflow methods with more options. Similar to ZSQL scipts
but in more cases.
- pre method executed before Examples of applications:
- pre async executed anyway - when movement is updated, apply transformation rules to movement
- post method executed after success before return - when stock is 0, post an event of stock empty
- post method executed after success anyway - when birthday is called, call the happy birthday script
This is similar to signals and slots except is applies to classes ERP5 main application: specialize behaviour of classes "on the fly".
rather than instances. Similar to Make the architecture as modular as possible. Implement connections
stateless workflow methods with more options. Similar to ZSQL scipts a la Qt.
but in more cases.
Examples of applications: Try to mimic: Workflow...
- when movement is updated, apply transformation rules to movement Question: should be use it for values ? or use a global value model ?
- when stock is 0, post an event of stock empty Status : OK
- when birthday is called, call the happy birthday script
ERP5 main application: specialize behaviour of classes "on the fly".
Make the architecture as modular as possible. Implement connections
a la Qt.
Try to mimic: Workflow... Implementation:
Question: should be use it for values ? or use a global value model ? A new kind of workflow (stateless). Follow the DCWorkflow class.
Provide filters (per portal_type, etc.). Allow inspection of objects ?
"""
meta_type = 'Workflow'
title = 'Interaction Workflow Definition'
Status : OK interactions = None
security = ClassSecurityInfo()
Implementation: manage_options = (
{'label': 'Properties', 'action': 'manage_properties'},
{'label': 'Interactions', 'action': 'interactions/manage_main'},
{'label': 'Variables', 'action': 'variables/manage_main'},
{'label': 'Scripts', 'action': 'scripts/manage_main'},
) + App.Undo.UndoSupport.manage_options
A new kind of workflow (stateless). Follow the DCWorkflow class. def __init__(self, id):
Provide filters (per portal_type, etc.). Allow inspection of objects ? self.id = id
from Interaction import Interaction
self._addObject(Interaction('interactions'))
from Products.DCWorkflow.Variables import Variables
self._addObject(Variables('variables'))
from Products.DCWorkflow.Worklists import Worklists
self._addObject(Worklists('worklists'))
from Products.DCWorkflow.Scripts import Scripts
self._addObject(Scripts('scripts'))
security.declareProtected(Permissions.View, 'getChainedPortalTypeList')
def getChainedPortalTypeList(self):
"""Returns the list of portal types that are chained to this
interaction workflow."""
chained_ptype_list = []
wf_tool = getToolByName(self, 'portal_workflow')
types_tool = getToolByName(self, 'portal_types')
for ptype in types_tool.objectIds():
if self.getId() in wf_tool._chains_by_type.get(ptype, []) :
chained_ptype_list.append(ptype)
return chained_ptype_list
security.declarePrivate('listObjectActions')
def listObjectActions(self, info):
return []
security.declarePrivate('_changeStateOf')
def _changeStateOf(self, ob, tdef=None, kwargs=None) :
"""
InteractionWorkflow is stateless. Thus, this function should do nothing.
"""
return
security.declarePrivate('isInfoSupported')
def isInfoSupported(self, ob, name):
'''
Returns a true value if the given info name is supported.
'''
vdef = self.variables.get(name, None)
if vdef is None:
return 0
return 1
security.declarePrivate('getInfoFor')
def getInfoFor(self, ob, name, default):
'''
Allows the user to request information provided by the
workflow. This method must perform its own security checks.
'''
vdef = self.variables.get(name, _MARKER)
if vdef is _MARKER:
return default
if vdef.info_guard is not None and not vdef.info_guard.check(
getSecurityManager(), self, ob):
return default
status = self._getStatusOf(ob)
if status is not None and status.has_key(name):
value = status[name]
# Not set yet. Use a default.
elif vdef.default_expr is not None:
ec = createExprContext(StateChangeInfo(ob, self, status))
value = vdef.default_expr(ec)
else:
value = vdef.default_value
return value
security.declarePrivate('isWorkflowMethodSupported')
def isWorkflowMethodSupported(self, ob, method_id):
'''
Returns a true value if the given workflow is
automatic with the propper method_id
NOTE: this method is not used in ERP5 because
of transition_list approach
'''
tdef = self.interactions.get(method_id, None)
if (tdef is not None and
tdef.trigger_type == TRIGGER_WORKFLOW_METHOD and
self._checkTransitionGuard(tdef, ob)):
return 1
return 0
security.declarePrivate('wrapWorkflowMethod')
def wrapWorkflowMethod(self, ob, method_id, func, args, kw):
'''
Allows the user to request a workflow action. This method
must perform its own security checks.
'''
return
security.declarePrivate('notifyWorkflowMethod')
def notifyWorkflowMethod(self, ob, transition_list, args=None, kw=None):
""" """
meta_type = 'Workflow' InteractionWorkflow is stateless. Thus, this function should do nothing.
title = 'Interaction Workflow Definition' """
return
interactions = None
security.declarePrivate('notifyBefore')
security = ClassSecurityInfo() def notifyBefore(self, ob, transition_list, args=None, kw=None):
'''
manage_options = ( Notifies this workflow of an action before it happens,
{'label': 'Properties', 'action': 'manage_properties'}, allowing veto by exception. Unless an exception is thrown, either
{'label': 'Interactions', 'action': 'interactions/manage_main'}, a notifySuccess() or notifyException() can be expected later on.
{'label': 'Variables', 'action': 'variables/manage_main'}, The action usually corresponds to a method name.
{'label': 'Scripts', 'action': 'scripts/manage_main'}, '''
) + App.Undo.UndoSupport.manage_options if type(transition_list) in StringTypes:
def __init__(self, id):
self.id = id
from Interaction import Interaction
self._addObject(Interaction('interactions'))
from Products.DCWorkflow.Variables import Variables
self._addObject(Variables('variables'))
from Products.DCWorkflow.Worklists import Worklists
self._addObject(Worklists('worklists'))
from Products.DCWorkflow.Scripts import Scripts
self._addObject(Scripts('scripts'))
security.declareProtected(Permissions.View, 'getChainedPortalTypeList')
def getChainedPortalTypeList(self):
"""Returns the list of portal types that are chained to this
interaction workflow."""
chained_ptype_list = []
wf_tool = getToolByName(self, 'portal_workflow')
types_tool = getToolByName(self, 'portal_types')
for ptype in types_tool.objectIds():
if self.getId() in wf_tool._chains_by_type.get(ptype, []) :
chained_ptype_list.append(ptype)
return chained_ptype_list
security.declarePrivate('listObjectActions')
def listObjectActions(self, info):
return []
security.declarePrivate('_changeStateOf')
def _changeStateOf(self, ob, tdef=None, kwargs=None) :
"""
InteractionWorkflow is stateless. Thus, this function should do nothing.
"""
return return
security.declarePrivate('isInfoSupported') # Wrap args into kw since this is the only way
def isInfoSupported(self, ob, name): # to be compatible with DCWorkflow
''' # A better approach consists in extending DCWorkflow
Returns a true value if the given info name is supported. if kw is None:
''' kw = {'workflow_method_args' : args}
vdef = self.variables.get(name, None) else:
if vdef is None: kw = kw.copy()
return 0 kw['workflow_method_args'] = args
return 1 filtered_transition_list = []
security.declarePrivate('getInfoFor') for t_id in transition_list:
def getInfoFor(self, ob, name, default): tdef = self.interactions[t_id]
''' assert tdef.trigger_type == TRIGGER_WORKFLOW_METHOD
Allows the user to request information provided by the if (tdef.portal_type_filter is None or \
workflow. This method must perform its own security checks. ob.getPortalType() in tdef.portal_type_filter) and \
''' self._checkTransitionGuard(tdef, ob, **kw):
vdef = self.variables.get(name, _MARKER) filtered_transition_list.append(tdef.id)
if vdef is _MARKER: former_status = self._getStatusOf(ob)
return default # Execute the "before" script.
if vdef.info_guard is not None and not vdef.info_guard.check( for script_name in tdef.script_name:
getSecurityManager(), self, ob): script = self.scripts[script_name]
return default # Pass lots of info to the script in a single parameter.
status = self._getStatusOf(ob) sci = StateChangeInfo(
if status is not None and status.has_key(name): ob, self, former_status, tdef, None, None, kwargs=kw)
value = status[name] script(sci) # May throw an exception
# Not set yet. Use a default.
elif vdef.default_expr is not None: return filtered_transition_list
ec = createExprContext(StateChangeInfo(ob, self, status))
value = vdef.default_expr(ec)
else:
value = vdef.default_value
return value security.declarePrivate('notifySuccess')
def notifySuccess(self, ob, transition_list, result, args=None, kw=None):
security.declarePrivate('isWorkflowMethodSupported') '''
def isWorkflowMethodSupported(self, ob, method_id): Notifies this workflow that an action has taken place.
''' '''
Returns a true value if the given workflow is if type(transition_list) in StringTypes:
automatic with the propper method_id
NOTE: this method is not used in ERP5 because
of transition_list approach
'''
tdef = self.interactions.get(method_id, None)
if (tdef is not None and
tdef.trigger_type == TRIGGER_WORKFLOW_METHOD and
self._checkTransitionGuard(tdef, ob)):
return 1
return 0
security.declarePrivate('wrapWorkflowMethod')
def wrapWorkflowMethod(self, ob, method_id, func, args, kw):
'''
Allows the user to request a workflow action. This method
must perform its own security checks.
'''
return
security.declarePrivate('notifyWorkflowMethod')
def notifyWorkflowMethod(self, ob, transition_list, args=None, kw=None):
"""
InteractionWorkflow is stateless. Thus, this function should do nothing.
"""
return return
security.declarePrivate('notifyBefore') kw = kw.copy()
def notifyBefore(self, ob, transition_list, args=None, kw=None): kw['workflow_method_args'] = args
''' kw['workflow_method_result'] = result
Notifies this workflow of an action before it happens,
allowing veto by exception. Unless an exception is thrown, either for t_id in transition_list:
a notifySuccess() or notifyException() can be expected later on. tdef = self.interactions[t_id]
The action usually corresponds to a method name. assert tdef.trigger_type == TRIGGER_WORKFLOW_METHOD
''' if (tdef.portal_type_filter is not None and \
if type(transition_list) in StringTypes: ob.getPortalType() not in tdef.portal_type_filter):
return continue
# Wrap args into kw since this is the only way # Initialize variables
# to be compatible with DCWorkflow former_status = self._getStatusOf(ob)
# A better approach consists in extending DCWorkflow econtext = None
if kw is None: sci = None
kw = {'workflow_method_args' : args}
# Update variables.
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 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: else:
kw = kw.copy() if vdef.default_expr is not None:
kw['workflow_method_args'] = args expr = vdef.default_expr
filtered_transition_list = [] else:
value = vdef.default_value
for t_id in transition_list: if expr is not None:
tdef = self.interactions[t_id] # Evaluate an expression.
assert tdef.trigger_type == TRIGGER_WORKFLOW_METHOD if econtext is None:
if (tdef.portal_type_filter is None or \ # Lazily create the expression context.
ob.getPortalType() in tdef.portal_type_filter) and \ if sci is None:
self._checkTransitionGuard(tdef, ob, **kw):
filtered_transition_list.append(tdef.id)
former_status = self._getStatusOf(ob)
# Execute the "before" script.
for script_name in tdef.script_name:
script = self.scripts[script_name]
# Pass lots of info to the script in a single parameter.
sci = StateChangeInfo( sci = StateChangeInfo(
ob, self, former_status, tdef, None, None, kwargs=kw) ob, self, former_status, tdef,
script(sci) # May throw an exception None, None, None)
econtext = createExprContext(sci)
return filtered_transition_list value = expr(econtext)
status[id] = value
security.declarePrivate('notifySuccess')
def notifySuccess(self, ob, transition_list, result, args=None, kw=None): sci = StateChangeInfo(
''' ob, self, former_status, tdef, None, None, kwargs=kw)
Notifies this workflow that an action has taken place. # Execute the "after" script.
''' for script_name in tdef.after_script_name:
if type(transition_list) in StringTypes: script = self.scripts[script_name]
return # Pass lots of info to the script in a single parameter.
script(sci) # May throw an exception
kw = kw.copy()
kw['workflow_method_args'] = args # Execute Before Commit
kw['workflow_method_result'] = result for script_name in tdef.before_commit_script_name:
transaction.get().addBeforeCommitHook(self._before_commit,
for t_id in transition_list: (sci, script_name))
tdef = self.interactions[t_id]
assert tdef.trigger_type == TRIGGER_WORKFLOW_METHOD # Execute "activity" scripts
if (tdef.portal_type_filter is not None and \ for script_name in tdef.activate_script_name:
ob.getPortalType() not in tdef.portal_type_filter): self.activate(activity='SQLQueue')\
continue .activeScript(script_name, ob.getRelativeUrl(),
status, tdef.id)
# Initialize variables
former_status = self._getStatusOf(ob) def _before_commit(self, sci, script_name):
econtext = None # check the object still exists before calling the script
sci = None ob = sci.object
while ob.isTempObject():
# Update variables. ob = ob.getParentValue()
tdef_exprs = tdef.var_exprs if aq_base(self.unrestrictedTraverse(ob.getPhysicalPath(), None)) is \
if tdef_exprs is None: tdef_exprs = {} aq_base(ob):
status = {} self.scripts[script_name](sci)
for id, vdef in self.variables.items():
if not vdef.for_status: security.declarePrivate('activeScript')
continue def activeScript(self, script_name, ob_url, status, tdef_id):
expr = None script = self.scripts[script_name]
if tdef_exprs.has_key(id): ob = self.unrestrictedTraverse(ob_url)
expr = tdef_exprs[id] tdef = self.interactions.get(tdef_id)
elif not vdef.update_always and former_status.has_key(id): sci = StateChangeInfo(
# Preserve former value ob, self, status, tdef, None, None, None)
value = former_status[id] script(sci)
else:
if vdef.default_expr is not None: def _getWorkflowStateOf(self, ob, id_only=0):
expr = vdef.default_expr return None
else:
value = vdef.default_value def _checkTransitionGuard(self, t, ob, **kw):
if expr is not None: # This check can be implemented with a guard expression, but
# Evaluate an expression. # it has a lot of overhead to use a TALES, so we make a special
if econtext is None: # treatment for the frequent case, that is, disallow the trigger
# Lazily create the expression context. # on a temporary document.
if sci is None: if t.temporary_document_disallowed:
sci = StateChangeInfo( isTempDocument = getattr(ob, 'isTempDocument', None)
ob, self, former_status, tdef, if isTempDocument is not None:
None, None, None) if isTempDocument():
econtext = createExprContext(sci) return 0
value = expr(econtext)
status[id] = value return DCWorkflowDefinition._checkTransitionGuard(self, t, ob, **kw)
sci = StateChangeInfo(
ob, self, former_status, tdef, None, None, kwargs=kw)
# Execute the "after" script.
for script_name in tdef.after_script_name:
script = self.scripts[script_name]
# Pass lots of info to the script in a single parameter.
script(sci) # May throw an exception
# Execute Before Commit
for script_name in tdef.before_commit_script_name:
transaction.get().addBeforeCommitHook(self._before_commit,
(sci, script_name))
# Execute "activity" scripts
for script_name in tdef.activate_script_name:
self.activate(activity='SQLQueue')\
.activeScript(script_name, ob.getRelativeUrl(),
status, tdef.id)
def _before_commit(self, sci, script_name):
# check the object still exists before calling the script
ob = sci.object
while ob.isTempObject():
ob = ob.getParentValue()
if aq_base(self.unrestrictedTraverse(ob.getPhysicalPath(), None)) is \
aq_base(ob):
self.scripts[script_name](sci)
security.declarePrivate('activeScript')
def activeScript(self, script_name, ob_url, status, tdef_id):
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)
script(sci)
def _getWorkflowStateOf(self, ob, id_only=0):
return None
def _checkTransitionGuard(self, t, ob, **kw):
# This check can be implemented with a guard expression, but
# it has a lot of overhead to use a TALES, so we make a special
# treatment for the frequent case, that is, disallow the trigger
# on a temporary document.
if t.temporary_document_disallowed:
isTempDocument = getattr(ob, 'isTempDocument', None)
if isTempDocument is not None:
if isTempDocument():
return 0
return DCWorkflowDefinition._checkTransitionGuard(self, t, ob, **kw)
Globals.InitializeClass(InteractionWorkflowDefinition) Globals.InitializeClass(InteractionWorkflowDefinition)
......
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