Passed
Push — master ( 454a47...f526ab )
by Ramon
04:30
created

guard_remove()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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