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,18 +648,43 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder, ...@@ -643,18 +648,43 @@ 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
else:
continue
# extract Trigger transitions from workflow definitions for later
transition_id_set = set(transitions.objectIds())
trigger_dict = dict()
for tr_id in transition_id_set:
tdef = transitions.get(tr_id, None)
if tdef.trigger_type == TRIGGER_WORKFLOW_METHOD: if tdef.trigger_type == TRIGGER_WORKFLOW_METHOD:
trigger_dict[tr_id] = tdef
storage[wf_id] = (transition_id_set, trigger_dict)
for wf_id, v in dc_workflow_dict.iteritems():
transition_id_set, trigger_dict = v
for tr_id, tdef in trigger_dict.iteritems():
method_id = convertToMixedCase(tr_id) method_id = convertToMixedCase(tr_id)
if getattr(klass, method_id, _MARKER) is not _MARKER: if getattr(klass, method_id, _MARKER) is _MARKER:
prop_holder.security.declareProtected(Permissions.AccessContentsInformation,
method_id)
prop_holder.registerWorkflowMethod(method_id, wf_id, tr_id)
continue
method = getattr(klass, method_id) method = getattr(klass, method_id)
# Wrap method # Wrap method
if callable(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): if not isinstance(method, WorkflowMethod):
method = WorkflowMethod(method) method = WorkflowMethod(method)
setattr(klass, method_id, method) setattr(klass, method_id, method)
...@@ -663,28 +693,17 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder, ...@@ -663,28 +693,17 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder,
# are going to register class defined # are going to register class defined
# workflow methods to the appropriate transition # workflow methods to the appropriate transition
transition_id = method.getTransitionId() transition_id = method.getTransitionId()
if transition_id in interaction_id_list: if transition_id in transition_id_set:
method.registerTransitionAlways(ptype, wf_id, transition_id) method.registerTransitionAlways(ptype, wf_id, transition_id)
method.registerTransitionAlways(ptype, wf_id, tr_id) method.registerTransitionAlways(ptype, wf_id, tr_id)
else:
LOG('initializePortalTypeDynamicWorkflowMethods', 100,
'WARNING! Can not initialize %s on %s' % \
(method_id, str(klass)))
else:
prop_holder.security.declareProtected(Permissions.AccessContentsInformation,
method_id)
prop_holder.registerWorkflowMethod(method_id, 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()
for tr_id in interaction_id_list:
tdef = wf.interactions.get(tr_id, None)
if tdef.trigger_type == TRIGGER_WORKFLOW_METHOD:
# XXX Prefiltering per portal type would be more efficient # XXX Prefiltering per portal type would be more efficient
for imethod_id in tdef.method_id: for imethod_id in tdef.method_id:
if wildcard_interaction_method_id_match(imethod_id): if wildcard_interaction_method_id_match(imethod_id):
...@@ -701,10 +720,23 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder, ...@@ -701,10 +720,23 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder,
# It's not consistent with regexp based filters. # It's not consistent with regexp based filters.
method_id_list = [imethod_id] method_id_list = [imethod_id]
for method_id in method_id_list: for method_id in method_id_list:
if getattr(klass, method_id, _MARKER) is not _MARKER: if getattr(klass, method_id, _MARKER) is _MARKER:
# 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)
continue
method = getattr(klass, method_id) method = getattr(klass, method_id)
# Wrap method # Wrap method
if callable(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): if not isinstance(method, WorkflowMethod):
method = WorkflowMethod(method) method = WorkflowMethod(method)
setattr(klass, method_id, method) setattr(klass, method_id, method)
...@@ -713,25 +745,12 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder, ...@@ -713,25 +745,12 @@ def initializePortalTypeDynamicWorkflowMethods(self, klass, ptype, prop_holder,
# are going to register class defined # are going to register class defined
# workflow methods to the appropriate transition # workflow methods to the appropriate transition
transition_id = method.getTransitionId() transition_id = method.getTransitionId()
if transition_id in interaction_id_list: if transition_id in transition_id_set:
method.registerTransitionAlways(ptype, wf_id, transition_id) method.registerTransitionAlways(ptype, wf_id, transition_id)
if tdef.once_per_transaction: if tdef.once_per_transaction:
method.registerTransitionOncePerTransaction(ptype, wf_id, tr_id) method.registerTransitionOncePerTransaction(ptype, wf_id, tr_id)
else: else:
method.registerTransitionAlways(ptype, wf_id, tr_id) 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)
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