Passed
Push — master ( af2087...3e7509 )
by Ramon
05:04
created

bika.lims.content.referenceanalysis   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 186
Duplicated Lines 5.38 %

Importance

Changes 0
Metric Value
wmc 29
eloc 103
dl 10
loc 186
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A ReferenceAnalysis.getSample() 0 6 1
A ReferenceAnalysis.getDueDate() 0 8 2
A ReferenceAnalysis.getSupplier() 0 8 2
A ReferenceAnalysis.getSupplierUID() 0 5 2
A ReferenceAnalysis.getDependencies() 0 6 1
A ReferenceAnalysis.setResult() 0 7 2
A ReferenceAnalysis.getInstrumentUID() 0 9 2
A ReferenceAnalysis.getServiceDefaultInstrumentUID() 0 9 2
A ReferenceAnalysis.getServiceDefaultInstrumentURL() 0 9 2
A ReferenceAnalysis.getReferenceResults() 0 5 1
A ReferenceAnalysis.getServiceDefaultInstrumentTitle() 0 9 2
A ReferenceAnalysis.getDependents() 0 6 1
A ReferenceAnalysis.getResultsRange() 0 21 2
B ReferenceAnalysis.workflow_script_attach() 10 19 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 AccessControl import ClassSecurityInfo
9
10
from DateTime import DateTime
11
from Products.Archetypes.public import *
12
from Products.CMFCore.utils import getToolByName
13
from bika.lims.config import PROJECTNAME, STD_TYPES
14
from bika.lims.content.abstractanalysis import AbstractAnalysis
15
from bika.lims.content.abstractanalysis import schema
16
from bika.lims.content.analysisspec import ResultsRangeDict
17
from bika.lims.interfaces import IReferenceAnalysis
18
from bika.lims.subscribers import skip
19
from bika.lims.workflow import doActionFor
20
from plone.app.blob.field import BlobField
21
from zope.interface import implements
22
23
schema = schema.copy() + Schema((
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'Schema'
Loading history...
24
    StringField(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'StringField'
Loading history...
25
        'ReferenceType',
26
        vocabulary=STD_TYPES,
27
    ),
28
    BlobField(
29
        'RetractedAnalysesPdfReport',
30
    ),
31
    StringField(
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'StringField'
Loading history...
32
        'ReferenceAnalysesGroupID',
33
    )
34
))
35
36
37
class ReferenceAnalysis(AbstractAnalysis):
38
    implements(IReferenceAnalysis)
39
    security = ClassSecurityInfo()
40
    displayContentsTab = False
41
    schema = schema
42
43
    @security.public
44
    def getSupplier(self):
45
        """ Returns the Supplier of the ReferenceSample this ReferenceAnalysis
46
        refers to
47
        """
48
        sample = self.getSample()
49
        if sample:
50
            return sample.aq_parent
51
52
    @security.public
53
    def getSupplierUID(self):
54
        supplier = self.getSupplier()
55
        if supplier:
56
            return supplier.UID()
57
58
    @security.public
59
    def getSample(self):
60
        """ Returns the ReferenceSample this ReferenceAnalysis refers to
61
        Delegates to self.aq_parent
62
        """
63
        return self.aq_parent
64
65
    @security.public
66
    def getDueDate(self):
67
        """Used to populate getDueDate index and metadata.
68
        This very simply returns the expiry date of the parent reference sample.
69
        """
70
        sample = self.getSample()
71
        if sample:
72
            return sample.getExpiryDate()
73
74
    @security.public
75
    def setResult(self, value):
76
        # Always update ResultCapture date when this field is modified
77
        self.setResultCaptureDate(DateTime())
78
        # Ensure result integrity regards to None, empty and 0 values
79
        val = str('' if not value and value != 0 else value).strip()
80
        self.getField('Result').set(self, val)
81
82
    def getReferenceResults(self):
83
        """
84
        It is used as metacolumn
85
        """
86
        return self.getSample().getReferenceResults()
87
88
    @security.public
89
    def getResultsRange(self):
90
        """Returns the valid result range for this reference analysis based on
91
        the results ranges defined in the Reference Sample from which this
92
        analysis has been created.
93
94
        A Reference Analysis (control or blank) will be considered out of range
95
        if its results does not match with the result defined on its parent
96
        Reference Sample, with the % error as the margin of error, that will be
97
        used to set the range's min and max values
98
        :return: A dictionary with the keys min and max
99
        :rtype: dict
100
        """
101
        specs = ResultsRangeDict(result="")
102
        sample = self.getSample()
103
        if not sample:
104
            return specs
105
106
        service_uid = self.getServiceUID()
107
        sample_range = sample.getResultsRangeDict()
108
        return sample_range.get(service_uid, specs)
109
110
    def getInstrumentUID(self):
111
        """
112
        It is a metacolumn.
113
        Returns the same value as the service.
114
        """
115
        instrument = self.getInstrument()
116
        if not instrument:
117
            return None
118
        return instrument.UID()
119
120
    def getServiceDefaultInstrumentUID(self):
121
        """
122
        It is used as a metacolumn.
123
        Returns the default service's instrument UID
124
        """
125
        ins = self.getInstrument()
126
        if ins:
127
            return ins.UID()
128
        return ''
129
130
    def getServiceDefaultInstrumentTitle(self):
131
        """
132
        It is used as a metacolumn.
133
        Returns the default service's instrument UID
134
        """
135
        ins = self.getInstrument()
136
        if ins:
137
            return ins.Title()
138
        return ''
139
140
    def getServiceDefaultInstrumentURL(self):
141
        """
142
        It is used as a metacolumn.
143
        Returns the default service's instrument UID
144
        """
145
        ins = self.getInstrument()
146
        if ins:
147
            return ins.absolute_url_path()
148
        return ''
149
150
    def getDependencies(self, retracted=False):
151
        """It doesn't make sense for a ReferenceAnalysis to use
152
        dependencies, since them are only used in calculations for
153
        routine analyses
154
        """
155
        return []
156
157
    def getDependents(self, retracted=False):
158
        """It doesn't make sense for a ReferenceAnalysis to use
159
        dependents, since them are only used in calculations for
160
        routine analyses
161
        """
162
        return []
163
164
    def workflow_script_attach(self):
165
        if skip(self, "attach"):
166
            return
167
        workflow = getToolByName(self, 'portal_workflow')
168
        # If all analyses on the worksheet have been attached,
169
        # then attach the worksheet.
170
        ws = self.getWorksheet()
171
        ws_state = workflow.getInfoFor(ws, 'review_state')
172 View Code Duplication
        if ws_state == 'attachment_due' and not skip(ws, "attach", peek=True):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
173
            can_attach = True
174
            for a in ws.getAnalyses():
175
                if workflow.getInfoFor(a, 'review_state') in \
176
                        ('sample_due', 'sample_received', 'attachment_due',
177
                         'assigned',):
178
                    can_attach = False
179
                    break
180
            if can_attach:
181
                workflow.doActionFor(ws, 'attach')
182
        self.reindexObject()
183
184
185
registerType(ReferenceAnalysis, PROJECTNAME)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'registerType'
Loading history...
186