Commit 1963d7e7 authored by Nicolas Dumazet's avatar Nicolas Dumazet

refactor

* use continue's on short paths to avoid nesting
* do not call 4 times getWorkflowsFor()
* when looking for triggers, only iterate through the list once





git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@42492 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a5bd35cd
...@@ -623,9 +623,14 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder, ...@@ -623,9 +623,14 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder,
# is looked up with _aq_dynamic, thus causes infinite recursions. # is looked up with _aq_dynamic, thus causes infinite recursions.
portal_workflow = aq_inner(getToolByName(portal, 'portal_workflow')) portal_workflow = aq_inner(getToolByName(portal, 'portal_workflow'))
dc_workflow_dict = dict()
interaction_workflow_dict = dict()
for wf in portal_workflow.getWorkflowsFor(self): for wf in portal_workflow.getWorkflowsFor(self):
if wf.__class__.__name__ in ('DCWorkflowDefinition', ): wf_id = wf.id
wf_id = wf.id wf_type = wf.__class__.__name__
if wf_type == "DCWorkflowDefinition":
# Create state var accessor # Create state var accessor
# and generate methods that support the translation of workflow states # and generate methods that support the translation of workflow states
state_var = wf.variables.getStateVar() state_var = wf.variables.getStateVar()
...@@ -643,95 +648,109 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder, ...@@ -643,95 +648,109 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder,
prop_holder.security.declareProtected( prop_holder.security.declareProtected(
Permissions.AccessContentsInformation, Permissions.AccessContentsInformation,
method_id ) method_id )
for wf in portal_workflow.getWorkflowsFor(self):
wf_id = wf.id storage = dc_workflow_dict
if wf.__class__.__name__ in ('DCWorkflowDefinition', ): transitions = wf.transitions
interaction_id_list = wf.transitions.objectIds() elif wf_type == "InteractionWorkflowDefinition":
for tr_id in interaction_id_list: storage = interaction_workflow_dict
tdef = wf.transitions.get(tr_id, None) transitions = wf.interactions
if tdef.trigger_type == TRIGGER_WORKFLOW_METHOD: else:
method_id = convertToMixedCase(tr_id) continue
if getattr(klass, method_id, _MARKER) is not _MARKER:
method = getattr(klass, method_id) # extract Trigger transitions from workflow definitions for later
# Wrap method transition_id_set = set(transitions.objectIds())
if callable(method): trigger_dict = dict()
if not isinstance(method, WorkflowMethod): for tr_id in transition_id_set:
method = WorkflowMethod(method) tdef = transitions.get(tr_id, None)
setattr(klass, method_id, method) if tdef.trigger_type == TRIGGER_WORKFLOW_METHOD:
else: trigger_dict[tr_id] = tdef
# We must be sure that we
# are going to register class defined storage[wf_id] = (transition_id_set, trigger_dict)
# workflow methods to the appropriate transition
transition_id = method.getTransitionId() for wf_id, v in dc_workflow_dict.iteritems():
if transition_id in interaction_id_list: transition_id_set, trigger_dict = v
method.registerTransitionAlways(ptype, wf_id, transition_id) for tr_id, tdef in trigger_dict.iteritems():
method.registerTransitionAlways(ptype, wf_id, tr_id) method_id = convertToMixedCase(tr_id)
else: if getattr(klass, method_id, _MARKER) is _MARKER:
LOG('initializePortalTypeDynamicWorkflowMethods', 100, prop_holder.security.declareProtected(Permissions.AccessContentsInformation,
'WARNING! Can not initialize %s on %s' % \ method_id)
(method_id, str(klass))) prop_holder.registerWorkflowMethod(method_id, wf_id, tr_id)
else: continue
prop_holder.security.declareProtected(Permissions.AccessContentsInformation,
method_id) method = getattr(klass, method_id)
prop_holder.registerWorkflowMethod(method_id, wf_id, tr_id) # Wrap method
if not callable(method):
LOG('initializePortalTypeDynamicWorkflowMethods', 100,
'WARNING! Can not initialize %s on %s' % \
(method_id, str(klass)))
continue
if not isinstance(method, WorkflowMethod):
method = WorkflowMethod(method)
setattr(klass, method_id, method)
else:
# We must be sure that we
# are going to register class defined
# workflow methods to the appropriate transition
transition_id = method.getTransitionId()
if transition_id in transition_id_set:
method.registerTransitionAlways(ptype, wf_id, transition_id)
method.registerTransitionAlways(ptype, wf_id, tr_id)
# XXX This part is (more or less...) a copy and paste # XXX This part is (more or less...) a copy and paste
# We need to run this part twice in order to handle interactions of interactions # We need to run this part twice in order to handle interactions of interactions
# ex. an interaction workflow creates a workflow method which matches # ex. an interaction workflow creates a workflow method which matches
# the regexp of another interaction workflow # the regexp of another interaction workflow
for wf in portal_workflow.getWorkflowsFor(self) * 2: # This is really necesary for wf_id in interaction_workflow_dict.keys()*2:
wf_id = wf.id transition_id_set, trigger_dict = interaction_workflow_dict[wf_id]
if wf.__class__.__name__ in ('InteractionWorkflowDefinition', ): for tr_id, tdef in trigger_dict.iteritems():
interaction_id_list = wf.interactions.objectIds() # XXX Prefiltering per portal type would be more efficient
for tr_id in interaction_id_list: for imethod_id in tdef.method_id:
tdef = wf.interactions.get(tr_id, None) if wildcard_interaction_method_id_match(imethod_id):
if tdef.trigger_type == TRIGGER_WORKFLOW_METHOD: # Interactions workflows can use regexp based wildcard methods
# XXX Prefiltering per portal type would be more efficient method_id_matcher = re.compile(imethod_id) # XXX What happens if exception ?
for imethod_id in tdef.method_id: method_id_list = prop_holder.getAccessorMethodIdList() + \
if wildcard_interaction_method_id_match(imethod_id): prop_holder.getWorkflowMethodIdList() + \
# Interactions workflows can use regexp based wildcard methods prop_holder.getClassMethodIdList(klass)
method_id_matcher = re.compile(imethod_id) # XXX What happens if exception ? # XXX - class stuff is missing here
method_id_list = prop_holder.getAccessorMethodIdList() + \ method_id_list = filter(method_id_matcher.match, method_id_list)
prop_holder.getWorkflowMethodIdList() + \ else:
prop_holder.getClassMethodIdList(klass) # Single method
# XXX - class stuff is missing here # XXX What if the method does not exist ?
method_id_list = filter(method_id_matcher.match, method_id_list) # It's not consistent with regexp based filters.
else: method_id_list = [imethod_id]
# Single method for method_id in method_id_list:
# XXX What if the method does not exist ? if getattr(klass, method_id, _MARKER) is _MARKER:
# It's not consistent with regexp based filters. # set a default security, if this method is not already
method_id_list = [imethod_id] # protected.
for method_id in method_id_list: if method_id not in prop_holder.security.names:
if getattr(klass, method_id, _MARKER) is not _MARKER: prop_holder.security.declareProtected(
method = getattr(klass, method_id) Permissions.AccessContentsInformation, method_id)
# Wrap method prop_holder.registerWorkflowMethod(method_id, wf_id, tr_id,
if callable(method): tdef.once_per_transaction)
if not isinstance(method, WorkflowMethod): continue
method = WorkflowMethod(method)
setattr(klass, method_id, method)
else:
# We must be sure that we
# are going to register class defined
# workflow methods to the appropriate transition
transition_id = method.getTransitionId()
if transition_id in interaction_id_list:
method.registerTransitionAlways(ptype, wf_id, transition_id)
if tdef.once_per_transaction:
method.registerTransitionOncePerTransaction(ptype, wf_id, tr_id)
else:
method.registerTransitionAlways(ptype, wf_id, tr_id)
else:
LOG('initializePortalTypeDynamicWorkflowMethods', 100,
'WARNING! Can not initialize %s on %s' % \
(method_id, str(klass)))
else:
# set a default security, if this method is not already
# protected.
if method_id not in prop_holder.security.names:
prop_holder.security.declareProtected(
Permissions.AccessContentsInformation, method_id)
prop_holder.registerWorkflowMethod(method_id, wf_id, tr_id,
tdef.once_per_transaction)
method = getattr(klass, method_id)
# Wrap method
if not callable(method):
LOG('initializePortalTypeDynamicWorkflowMethods', 100,
'WARNING! Can not initialize %s on %s' % \
(method_id, str(klass)))
continue
if not isinstance(method, WorkflowMethod):
method = WorkflowMethod(method)
setattr(klass, method_id, method)
else:
# We must be sure that we
# are going to register class defined
# workflow methods to the appropriate transition
transition_id = method.getTransitionId()
if transition_id in transition_id_set:
method.registerTransitionAlways(ptype, wf_id, transition_id)
if tdef.once_per_transaction:
method.registerTransitionOncePerTransaction(ptype, wf_id, tr_id)
else:
method.registerTransitionAlways(ptype, wf_id, tr_id)
class Base( CopyContainer, class Base( CopyContainer,
PortalContent, PortalContent,
......
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