Completed
Branch master (9edffc)
by Jordi
04:36
created

before_sample()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
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 DateTime import DateTime
9
from bika.lims import api
10
from bika.lims.utils import changeWorkflowState
11
from bika.lims.utils.analysisrequest import create_retest
12
from bika.lims.workflow import get_prev_status_from_history
13
from bika.lims.workflow.analysisrequest import AR_WORKFLOW_ID, \
14
    do_action_to_descendants, do_action_to_analyses, do_action_to_ancestors
15
16
17
def before_sample(analysis_request):
18
    """Method triggered before "sample" transition for the Analysis Request
19
    passed in is performed
20
    """
21
    if not analysis_request.getDateSampled():
22
        analysis_request.setDateSampled(DateTime())
23
    if not analysis_request.getSampler():
24
        analysis_request.setSampler(api.get_current_user().id)
25
26
27
def after_reject(analysis_request):
28
    """Method triggered after a 'reject' transition for the Analysis Request
29
    passed in is performed. Cascades the transition to descendants (partitions)
30
    and analyses as well. If "notify on rejection" setting is enabled, sends a
31
    notification to the client contact.
32
    """
33
    do_action_to_descendants(analysis_request, "reject")
34
    do_action_to_analyses(analysis_request, "reject")
35
36
    # TODO Workflow - AnalysisRequest - Revisit rejection notification
37
    if not analysis_request.bika_setup.getNotifyOnRejection():
38
        return
39
40
    ancestor = analysis_request.getParentAnalysisRequest()
41
    if ancestor and api.get_workflow_status_of(ancestor) == "rejected":
42
        # No need to notify, notification done by the ancestor
43
        return
44
45
    # Notify the Client about the Rejection.
46
    from bika.lims.utils.analysisrequest import notify_rejection
47
    notify_rejection(analysis_request)
48
49
50
def after_retract(analysis_request):
51
    """Method triggered after a 'retract' transition for the Analysis Request
52
    passed in is performed. Cascades the transition to descendants (partitions)
53
    and analyses as well.
54
    """
55
    do_action_to_descendants(analysis_request, "retract")
56
    do_action_to_analyses(analysis_request, "retract")
57
58
59
def after_invalidate(obj):
60
    """Function triggered after 'invalidate' transition for the Analysis
61
    Request passed in is performed. Creates a retest
62
    """
63
    create_retest(obj)
64
65
66
def after_submit(analysis_request):
67
    """Function called after a 'submit' transition is triggered. Promotes the
68
    submission of ancestors and descendants (partitions).
69
    """
70
    # This transition is not meant to be triggered manually by the user, rather
71
    # promoted by analyses. Hence, there is no need to cascade the transition
72
    # to the analyses the analysis request contains.
73
    do_action_to_ancestors(analysis_request, "submit")
74
    do_action_to_descendants(analysis_request, "submit")
75
76
77
def after_verify(analysis_request):
78
    """Method triggered after a 'verify' transition for the Analysis Request
79
    passed in is performed. Promotes the 'verify' transition to ancestors,
80
    descendants and to the analyses that belong to the analysis request.
81
    """
82
    # This transition is not meant to be triggered manually by the user, rather
83
    # promoted by analyses. Hence, there is no need to cascade the transition
84
    # to the analyses the analysis request contains.
85
    do_action_to_ancestors(analysis_request, "verify")
86
    do_action_to_descendants(analysis_request, "verify")
87
88
89
def after_publish(analysis_request):
90
    """Method triggered after an 'publish' transition for the Analysis Request
91
    passed in is performed. Performs the 'publish' transition Publishes the
92
    descendant partitions and all analyses associated to the analysis request
93
    as well.
94
    """
95
    do_action_to_descendants(analysis_request, "publish")
96
    do_action_to_analyses(analysis_request, "publish")
97
98
99
def after_reinstate(analysis_request):
100
    """Method triggered after a 'reinstate' transition for the Analysis Request
101
    passed in is performed. Sets its status to the last status before it was
102
    cancelled. Reinstates the descendant partitions and all the analyses
103
    associated to the analysis request as well.
104
    """
105
    do_action_to_descendants(analysis_request, "reinstate")
106
    do_action_to_analyses(analysis_request, "reinstate")
107
108
    # Force the transition to previous state before the request was cancelled
109
    prev_status = get_prev_status_from_history(analysis_request, "cancelled")
110
    changeWorkflowState(analysis_request, AR_WORKFLOW_ID, prev_status,
111
                        action="reinstate",
112
                        actor=api.get_current_user().getId())
113
    analysis_request.reindexObject()
114
115
116
def after_cancel(analysis_request):
117
    """Method triggered after a 'cancel' transition for the Analysis Request
118
    passed in is performed. Cascades this transition to its analyses and
119
    partitions.
120
    """
121
    do_action_to_descendants(analysis_request, "cancel")
122
    do_action_to_analyses(analysis_request, "cancel")
123
124
125
def after_receive(analysis_request):
126
    """Method triggered after "receive" transition for the Analysis Request
127
    passed in is performed
128
    """
129
    analysis_request.setDateReceived(DateTime())
130
    idxs = ['getDateReceived', 'isSampleReceived']
131
    for analysis in analysis_request.getAnalyses(full_objects=True):
132
        analysis.reindexObject(idxs=idxs)
133
134
135
def after_sample(analysis_request):
136
    """Method triggered after "sample" transition for the Analysis Request
137
    passed in is performed
138
    """
139
    analysis_request.setDateSampled(DateTime())
140
    idxs = ['getDateSampled']
141
    for analysis in analysis_request.getAnalyses(full_objects=True):
142
        analysis.reindexObject(idxs=idxs)
143