def fix_workflow_transitions(portal):
    """
    Replace target states from some workflow statuses
    """
    logger.info("Fixing workflow transitions...")
    tochange = [
        {'wfid': 'bika_duplicateanalysis_workflow',
         'trid': 'submit',
         'changes': {
             'new_state_id': 'to_be_verified',
             'guard_expr': ''
            },
         'update': {
             'catalog': CATALOG_ANALYSIS_LISTING,
             'portal_type': 'DuplicateAnalysis',
             'status_from': 'attachment_due',
             'status_to': 'to_be_verified'
            }
         }
    ]

    wtool = api.get_tool('portal_workflow')
    for item in tochange:
        wfid = item['wfid']
        trid = item['trid']
        workflow = wtool.getWorkflowById(wfid)
        transitions = workflow.transitions
        transition = transitions[trid]
        changes = item.get('changes', {})

        if 'new_state_id' in changes:
            new_state_id = changes['new_state_id']
            oldstate = transition.new_state_id
            logger.info(
                "Replacing target state '{0}' from '{1}.{2}' to {3}"
                    .format(oldstate, wfid, trid, new_state_id)
            )
            transition.new_state_id = new_state_id

        if 'guard_expr' in changes:
            new_guard = changes['guard_expr']
            if not new_guard:
                transition.guard = None
                logger.info(
                    "Removing guard expression from '{0}.{1}'"
                        .format(wfid, trid))
            else:
                guard = transition.getGuard()
                guard.expr = Expression(new_guard)
                transition.guard = guard
                logger.info(
                    "Replacing guard expression from '{0}.{1}' to {2}"
                        .format(wfid, trid, new_guard))

        update = item.get('update', {})
        if update:
            catalog_id = update['catalog']
            portal_type = update['portal_type']
            catalog = api.get_tool(catalog_id)
            brains = catalog(portal_type=portal_type)
            for brain in brains:
                obj = api.get_object(brain)
                if 'status_from' in update and 'status_to' in update:
                    status_from = update['status_from']
                    status_to = update['status_to']
                    if status_from == brain.review_state:
                        logger.info(
                            "Changing status for {0} from '{1} to {2}"
                                .format(obj.getId(), status_from, status_to))
                        changeWorkflowState(obj, wfid, status_to)

                workflow.updateRoleMappingsFor(obj)
                obj.reindexObject()
Replace target states from some workflow statuses