Passed
Push — master ( a455bf...6f481d )
by Ramon
16:39 queued 12:06
created

guard_retract()   A

Complexity

Conditions 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
cc 4
nop 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from bika.lims import api
9
from bika.lims.catalog.analysis_catalog import CATALOG_ANALYSIS_LISTING
10
from bika.lims.workflow import getCurrentState, isTransitionAllowed
11
from bika.lims.workflow import isBasicTransitionAllowed
12
from bika.lims.workflow import wasTransitionPerformed
13
14
15
def _children_are_ready(obj, transition_id, dettached_states=None):
16
    """Returns true if the children of the object passed in (worksheet) have
17
    been all transitioned in accordance with the 'transition_id' passed in. If
18
    detached_states is provided, children with those states are dismissed, so
19
    they will not be taken into account in the evaluation. Nevertheless, at
20
    least one child with for which the transition_id performed is required for
21
    this function to return true (if all children are in detached states, it
22
    always return False).
23
    """
24
    detached_count = 0
25
    analyses = obj.getAnalyses()
26
    for analysis in analyses:
27
        if dettached_states:
28
            if api.get_review_status(analysis) in dettached_states:
29
                detached_count += 1
30
                continue
31
        if not api.is_active(analysis):
32
            return False
33
        if not wasTransitionPerformed(analysis, transition_id):
34
            return False
35
36
    if detached_count == len(analyses):
37
        # If all analyses are in a detached state, it means that the
38
        # condition of at least having one child for which the
39
        # transition is performed is not satisfied so return False
40
        return False
41
    return True
42
43
44
def guard_submit(obj):
45
    """Returns if 'submit' transition can be applied to the worksheet passed in.
46
    By default, the target state for the 'submit' transition for a worksheet is
47
    'to_be_verified', so this guard returns true if all the analyses assigned
48
    to the worksheet have already been submitted. Those analyses that are in a
49
    non-valid state (cancelled, inactive) are dismissed in the evaluation, but
50
    at least one analysis must be in an active state (and submitted) for this
51
    guard to return True. Otherwise, always returns False.
52
    Note this guard depends entirely on the current status of the children.
53
    """
54
    if not isBasicTransitionAllowed(obj):
55
        return False
56
57
    dettached = ['rejected', 'retracted']
58
    return _children_are_ready(obj, 'submit', dettached_states=dettached)
59
60
61
def guard_retract(worksheet):
62
    """Return whether the transition retract can be performed or not to the
63
    worksheet passed in. Since the retract transition from worksheet is a
64
    shortcut to retract transitions from all analyses the worksheet contains,
65
    this guard only returns True if retract transition is allowed for all
66
    analyses the worksheet contains
67
    """
68
    analyses = worksheet.getAnalyses()
69
    detached = ['rejected', 'retracted']
70
    num_detached = 0
71
    for analysis in analyses:
72
        if api.get_workflow_status_of(analysis) in detached:
73
            num_detached += 1
74
        elif not isTransitionAllowed(analysis, "retract"):
75
            return False
76
    return analyses and num_detached < len(analyses) or False
77
78
79
def guard_verify(obj):
80
    """Returns True if 'verify' transition can be applied to the Worksheet
81
    passed in. This is, returns true if all the analyses assigned
82
    have already been verified. Those analyses that are in an inactive state
83
    (cancelled, inactive) are dismissed, but at least one analysis must be in
84
    an active state (and verified), otherwise always return False.
85
    Note this guard depends entirely on the current status of the children
86
    :returns: true or false
87
    """
88
    if not isBasicTransitionAllowed(obj):
89
        return False
90
91
    dettached = ['rejected', 'retracted']
92
    return _children_are_ready(obj, 'verify', dettached_states=dettached)
93
94
95
def guard_rollback_to_open(worksheet):
96
    """Return whether 'rollback_to_receive' transition can be performed or not
97
    """
98
    for analysis in worksheet.getAnalyses():
99
        if api.get_review_status(analysis) in ["assigned"]:
100
            return True
101
    return False
102