Passed
Push — master ( 2e6d15...1ba9f7 )
by Jordi
06:26 queued 02:33
created

after_submit()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from bika.lims.interfaces import IAnalysisRequestPartition
23
from bika.lims.interfaces import IDetachedPartition
24
from bika.lims.interfaces import IReceived
25
from bika.lims.interfaces import IVerified
26
from bika.lims.utils import changeWorkflowState
27
from bika.lims.utils.analysisrequest import create_retest
28
from bika.lims.workflow import get_prev_status_from_history
29
from bika.lims.workflow.analysisrequest import AR_WORKFLOW_ID
30
from bika.lims.workflow.analysisrequest import do_action_to_analyses
31
from bika.lims.workflow.analysisrequest import do_action_to_ancestors
32
from bika.lims.workflow.analysisrequest import do_action_to_descendants
33
from DateTime import DateTime
34
from zope.interface import alsoProvides
35
from zope.interface import noLongerProvides
36
37
38
def before_sample(analysis_request):
39
    """Method triggered before "sample" transition for the Analysis Request
40
    passed in is performed
41
    """
42
    if not analysis_request.getDateSampled():
43
        analysis_request.setDateSampled(DateTime())
44
    if not analysis_request.getSampler():
45
        analysis_request.setSampler(api.get_current_user().id)
46
47
48
def after_reject(analysis_request):
49
    """Method triggered after a 'reject' transition for the Analysis Request
50
    passed in is performed. Cascades the transition to descendants (partitions)
51
    and analyses as well. If "notify on rejection" setting is enabled, sends a
52
    notification to the client contact.
53
    """
54
    do_action_to_descendants(analysis_request, "reject")
55
    do_action_to_analyses(analysis_request, "reject")
56
57
58
def after_retract(analysis_request):
59
    """Method triggered after a 'retract' transition for the Analysis Request
60
    passed in is performed. Cascades the transition to descendants (partitions)
61
    and analyses as well.
62
    """
63
    do_action_to_descendants(analysis_request, "retract")
64
    do_action_to_analyses(analysis_request, "retract")
65
66
67
def after_invalidate(obj):
68
    """Function triggered after 'invalidate' transition for the Analysis
69
    Request passed in is performed. Creates a retest
70
    """
71
    create_retest(obj)
72
73
74
def after_submit(analysis_request):
75
    """Function called after a 'submit' transition is triggered. Promotes the
76
    submission of ancestors and descendants (partitions).
77
    """
78
    # This transition is not meant to be triggered manually by the user, rather
79
    # promoted by analyses. Hence, there is no need to cascade the transition
80
    # to the analyses the analysis request contains.
81
    do_action_to_ancestors(analysis_request, "submit")
82
    do_action_to_descendants(analysis_request, "submit")
83
84
85
def after_verify(analysis_request):
86
    """Method triggered after a 'verify' transition for the Analysis Request
87
    passed in is performed. Promotes the 'verify' transition to ancestors,
88
    descendants and to the analyses that belong to the analysis request.
89
    """
90
    # Mark this analysis request as IReceived
91
    alsoProvides(analysis_request, IVerified)
92
93
    # This transition is not meant to be triggered manually by the user, rather
94
    # promoted by analyses. Hence, there is no need to cascade the transition
95
    # to the analyses the analysis request contains.
96
    do_action_to_ancestors(analysis_request, "verify")
97
    do_action_to_descendants(analysis_request, "verify")
98
99
100
def after_prepublish(analysis_request):
101
    """Method triggered after a 'prepublish' transition for the Analysis
102
    Request passed in is performed. Performs the 'publish' transition to the
103
    descendant partitions.
104
105
    Also see: https://github.com/senaite/senaite.core/pull/1428
106
    """
107
    do_action_to_descendants(analysis_request, "publish")
108
109
110
def after_publish(analysis_request):
111
    """Method triggered after an 'publish' transition for the Analysis Request
112
    passed in is performed. Performs the 'publish' transition Publishes the
113
    descendant partitions and all analyses associated to the analysis request
114
    as well.
115
    """
116
    do_action_to_descendants(analysis_request, "publish")
117
    do_action_to_analyses(analysis_request, "publish")
118
119
120
def after_reinstate(analysis_request):
121
    """Method triggered after a 'reinstate' transition for the Analysis Request
122
    passed in is performed. Sets its status to the last status before it was
123
    cancelled. Reinstates the descendant partitions and all the analyses
124
    associated to the analysis request as well.
125
    """
126
    do_action_to_descendants(analysis_request, "reinstate")
127
    do_action_to_analyses(analysis_request, "reinstate")
128
129
    # Force the transition to previous state before the request was cancelled
130
    prev_status = get_prev_status_from_history(analysis_request, "cancelled")
131
    changeWorkflowState(analysis_request, AR_WORKFLOW_ID, prev_status,
132
                        action="reinstate")
133
    analysis_request.reindexObject()
134
135
136
def after_cancel(analysis_request):
137
    """Method triggered after a 'cancel' transition for the Analysis Request
138
    passed in is performed. Cascades this transition to its analyses and
139
    partitions.
140
    """
141
    do_action_to_descendants(analysis_request, "cancel")
142
    do_action_to_analyses(analysis_request, "cancel")
143
144
145
def after_receive(analysis_request):
146
    """Method triggered after "receive" transition for the Analysis Request
147
    passed in is performed
148
    """
149
    # Mark this analysis request as IReceived
150
    alsoProvides(analysis_request, IReceived)
151
152
    analysis_request.setDateReceived(DateTime())
153
    do_action_to_analyses(analysis_request, "initialize")
154
155
156
def after_sample(analysis_request):
157
    """Method triggered after "sample" transition for the Analysis Request
158
    passed in is performed
159
    """
160
    analysis_request.setDateSampled(DateTime())
161
    idxs = ['getDateSampled']
162
    for analysis in analysis_request.getAnalyses(full_objects=True):
163
        analysis.reindexObject(idxs=idxs)
164
165
166
def after_rollback_to_receive(analysis_request):
167
    """Function triggered after "rollback to receive" transition is performed
168
    """
169
    if IVerified.providedBy(analysis_request):
170
        noLongerProvides(analysis_request, IVerified)
171
172
173
def after_detach(analysis_request):
174
    """Function triggered after "detach" transition is performed
175
    """
176
    # Unbind the sample from its parent (the primary)
177
    parent = analysis_request.getParentAnalysisRequest()
178
    analysis_request.setParentAnalysisRequest(None)
179
180
    # Assign the primary from which the sample has been detached
181
    analysis_request.setDetachedFrom(parent)
182
183
    # This sample is no longer a partition
184
    noLongerProvides(analysis_request, IAnalysisRequestPartition)
185
186
    # And we mark the sample with IDetachedPartition
187
    alsoProvides(analysis_request, IDetachedPartition)
188
189
    # Reindex both the parent and the detached one
190
    analysis_request.reindexObject()
191
    parent.reindexObject()
192