<commit_before>from DateTime import DateTime
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.WorkflowCore import WorkflowException
from bika.lims import logger

def AfterTransitionEventHandler(sample, event):

    # creation doesn't have a 'transition'
    if not event.transition:
        return

    if not sample.REQUEST.has_key('workflow_skiplist'):
        sample.REQUEST['workflow_skiplist'] = [sample.UID(), ]
    else:
        if sample.UID() in sample.REQUEST['workflow_skiplist']:
            ##logger.info("SM Skip")
            return
        else:
            sample.REQUEST["workflow_skiplist"].append(sample.UID())

    logger.info("Starting: %s on %s" % (event.transition.id, sample))

    workflow = getToolByName(sample, 'portal_workflow')
    membership_tool = getToolByName(sample, 'portal_membership')
    member = membership_tool.getAuthenticatedMember()
    props = getToolByName(sample, 'portal_properties').bika_properties
    parts = sample.objectValues('SamplePartition')

    if event.transition.id == "sampled":
        # Transition all sample partitions that are still 'to_be_sampled'
        tbs = [sp for sp in parts \
               if workflow.getInfoFor(sp, 'review_state') == 'to_be_sampled']
        for sp in tbs:
            workflow.doActionFor(sp, 'sampled')

        # All associated AnalysisRequests are also transitioned
        for ar in sample.getAnalysisRequests():
            if not ar.UID() in sample.REQUEST['workflow_skiplist']:
                try: workflow.doActionFor(ar, "sampled")
                except WorkflowException: pass

    elif event.transition.id == "preserved":
        # Transition all sample partitions that are still 'to_be_preserved'
        tbp = [sp for sp in parts \
               if workflow.getInfoFor(sp, 'review_state') == 'to_be_preserved']
        for sp in tbp:
            workflow.doActionFor(sp, 'preserved')

        # All associated AnalysisRequests are also transitioned
        for ar in sample.getAnalysisRequests():
            if not ar.UID() in sample.REQUEST['workflow_skiplist']:
                try: workflow.doActionFor(ar, "preserved")
                except WorkflowException: pass

    elif event.transition.id == "receive":
        if not props.getProperty('sampling_workflow_enabled', True):
            # If the sampling workflow is disabled, we set the DateSampled
            # to the current date
            DateSampled = DateTime()
            Sampler = member.id
            sample.setSamplingDate(DateSampled)
            sample.setSampler(Sampler)

        sample.setDateReceived(DateTime())
        sample.reindexObject(idxs = ["review_state", "getDateReceived"])

        # Receive all sample partitions that are still 'sample_due'
        sample_due = [sp for sp in parts \<commit_msg>re-index AR when parts are preserved<commit_after>from DateTime import DateTime
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.WorkflowCore import WorkflowException
from bika.lims import logger

def AfterTransitionEventHandler(sample, event):

    # creation doesn't have a 'transition'
    if not event.transition:
        return

    if not sample.REQUEST.has_key('workflow_skiplist'):
        sample.REQUEST['workflow_skiplist'] = [sample.UID(), ]
    else:
        if sample.UID() in sample.REQUEST['workflow_skiplist']:
            ##logger.info("SM Skip")
            return
        else:
            sample.REQUEST["workflow_skiplist"].append(sample.UID())

    logger.info("Starting: %s on %s" % (event.transition.id, sample))

    workflow = getToolByName(sample, 'portal_workflow')
    membership_tool = getToolByName(sample, 'portal_membership')
    member = membership_tool.getAuthenticatedMember()
    props = getToolByName(sample, 'portal_properties').bika_properties
    parts = sample.objectValues('SamplePartition')

    if event.transition.id == "sampled":
        # Transition all sample partitions that are still 'to_be_sampled'
        tbs = [sp for sp in parts \
               if workflow.getInfoFor(sp, 'review_state') == 'to_be_sampled']
        for sp in tbs:
            workflow.doActionFor(sp, 'sampled')

        # All associated AnalysisRequests are also transitioned
        for ar in sample.getAnalysisRequests():
            if not ar.UID() in sample.REQUEST['workflow_skiplist']:
                try: workflow.doActionFor(ar, "sampled")
                except WorkflowException: pass

    elif event.transition.id == "preserved":
        # Transition all sample partitions that are still 'to_be_preserved'
        tbp = [sp for sp in parts \
               if workflow.getInfoFor(sp, 'review_state') == 'to_be_preserved']
        for sp in tbp:
            workflow.doActionFor(sp, 'preserved')

        # All associated AnalysisRequests are also transitioned
        for ar in sample.getAnalysisRequests():
            if not ar.UID() in sample.REQUEST['workflow_skiplist']:
                try: workflow.doActionFor(ar, "preserved")
                except WorkflowException: pass
                ar.reindexObject()

    elif event.transition.id == "receive":
        if not props.getProperty('sampling_workflow_enabled', True):
            # If the sampling workflow is disabled, we set the DateSampled
            # to the current date
            DateSampled = DateTime()
            Sampler = member.id
            sample.setSamplingDate(DateSampled)
            sample.setSampler(Sampler)

        sample.setDateReceived(DateTime())
        sample.reindexObject(idxs = ["review_state", "getDateReceived"])

        # Receive all sample partitions that are still 'sample_due'
        sample_due = [sp for sp in parts \
{'fraction_non_alphanumeric': 0.0935856992639327, 'fraction_numerical': 0.0, 'mean_word_length': 2.7570770243581304, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7197192', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1548, 'n_words': 412}