Back to index

plone3  3.1.7
Functions | Variables
plone.app.workflow.remap Namespace Reference

Functions

def remap_workflow

Variables

int SAVE_THRESHOLD = 100
tuple _marker = object()

Function Documentation

def plone.app.workflow.remap.remap_workflow (   context,
  type_ids,
  chain,
  state_map = {} 
)
Change the workflow for each type in type_ids to use the workflow
chain given. state_map is a dictionary of old state names to
new ones. States that are not found will be remapped to the default
state of the new workflow.

Definition at line 12 of file remap.py.

00012 
00013 def remap_workflow(context, type_ids, chain, state_map={}):
00014     """Change the workflow for each type in type_ids to use the workflow
00015     chain given. state_map is a dictionary of old state names to
00016     new ones. States that are not found will be remapped to the default
00017     state of the new workflow.
00018     """
00019     
00020     if chain is None:
00021         chain = '(Default)'
00022 
00023     portal_workflow = getToolByName(context, 'portal_workflow')
00024     
00025     default_chain = portal_workflow.getDefaultChain()
00026     chains_by_type = dict(portal_workflow.listChainOverrides())
00027     
00028     # Build a dictionary of type id -> chain before we made changes
00029     old_chains = dict([(t, chains_by_type.get(t, default_chain)) for t in type_ids])
00030 
00031     # Work out which permissions were managed by the old chain, but not
00032     # by the new chain. This may vary by type id.
00033 
00034     # Update the workflow chain in portal_workflows.
00035      
00036     # XXX: There is no decent API for this it seems :-(
00037     if chain == '(Default)':
00038         cbt = portal_workflow._chains_by_type
00039         for type_id in type_ids:
00040             if cbt.has_key(type_id):
00041                 del cbt[type_id]
00042     else:
00043         portal_workflow.setChainForPortalTypes(type_ids, chain)
00044 
00045     # Now remap, and fix permissions
00046     
00047     # For each portal type, work out which workflows were controlling them
00048     # before, and which permissions were in that, which are not in the new
00049     # chain. These permissions need to be reset to 'Acquire'.
00050     
00051     chain_workflows = {}
00052     new_chain_permissions = set()
00053     permissions_to_reset = {}
00054 
00055     if chain == '(Default)':
00056         chain = default_chain
00057     for c in chain:
00058         if c not in chain_workflows:
00059             chain_workflows[c] = getattr(portal_workflow, c)
00060             for permission in chain_workflows[c].permissions:
00061                 new_chain_permissions.add(permission)
00062 
00063     for typeid, oc in old_chains.items():
00064         if oc == '(Default)':
00065             oc = default_chain
00066         permissions_to_reset[typeid] = set()
00067         for c in oc:
00068             if c not in chain_workflows:
00069                 chain_workflows[c] = getattr(portal_workflow, c)
00070             for permission in chain_workflows[c].permissions:
00071                 if permission not in new_chain_permissions:
00072                     permissions_to_reset[typeid].add(permission)
00073     
00074     portal_catalog = getToolByName(context, 'portal_catalog')
00075     
00076     # Then update the state of each
00077     remapped_count = 0
00078     threshold_count = 0
00079     for brain in portal_catalog(portal_type=type_ids):
00080         obj = brain.getObject()
00081         portal_type = brain.portal_type
00082         
00083         # If there are permissions to reset to acquire, do so now
00084         for permission in permissions_to_reset[brain.portal_type]:
00085             # A list makes it acquire ... if it was a tuple, it wouldn't
00086             modifyRolesForPermission(obj, permission, [])
00087         
00088         # Work out what, if any, the previous state of the object was
00089         
00090         if len(chain) > 0:
00091             old_chain = old_chains[portal_type]
00092             old_wf = None
00093             if len(old_chain) > 0:
00094                 old_wf = chain_workflows[old_chain[0]]
00095         
00096             old_state = None
00097             if old_wf is not None:
00098                 old_status = portal_workflow.getStatusOf(old_wf.getId(), obj)
00099                 if old_status is not None:
00100                     old_state = old_status.get('review_state', None)
00101             
00102             # Now add a transition
00103             for new_wf_name in chain:
00104                 new_wf = chain_workflows[new_wf_name]
00105                 new_status = { 'action'       : None,
00106                                'actor'        : None, 
00107                                'comments'     : 'State remapped from control panel',
00108                                'review_state' : state_map.get(old_state, new_wf.initial_state),
00109                                'time'         : DateTime()}
00110                 portal_workflow.setStatusOf(new_wf_name, obj, new_status)
00111             
00112                 # Trigger any automatic transitions, or else just make sure the role mappings are right
00113                 auto_transition = new_wf._findAutomaticTransition(obj, new_wf._getWorkflowStateOf(obj))
00114                 if auto_transition is not None:
00115                     new_wf._changeStateOf(obj, auto_transition)
00116                 else:
00117                     new_wf.updateRoleMappingsFor(obj)
00118 
00119         obj.reindexObject(idxs=['allowedRolesAndUsers', 'review_state'])
00120         
00121         remapped_count += 1
00122         threshold_count += 1
00123         
00124         if threshold_count > SAVE_THRESHOLD:
00125             transaction.savepoint()
00126             threshold_count = 0
00127             
00128     return remapped_count

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 4 of file remap.py.

Definition at line 3 of file remap.py.