Passed
Push — master ( cd3c58...e71d5a )
by Jordi
04:38
created

WorksheetTemplate.getInstruments()   B

Complexity

Conditions 6

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 27
rs 8.5666
c 0
b 0
f 0
cc 6
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
import sys
9
10
from AccessControl import ClassSecurityInfo
11
from bika.lims import api
12
from bika.lims import bikaMessageFactory as _
13
from bika.lims.browser.widgets import ServicesWidget
14
from bika.lims.browser.widgets import WorksheetTemplateLayoutWidget
15
from bika.lims.config import ANALYSIS_TYPES
16
from bika.lims.config import PROJECTNAME
17
from bika.lims.content.bikaschema import BikaSchema
18
from Products.Archetypes.public import BaseContent
19
from Products.Archetypes.public import BooleanField
20
from Products.Archetypes.public import BooleanWidget
21
from Products.Archetypes.public import DisplayList
22
from Products.Archetypes.public import ReferenceField
23
from Products.Archetypes.public import ReferenceWidget
24
from Products.Archetypes.public import Schema
25
from Products.Archetypes.public import SelectionWidget
26
from Products.Archetypes.public import StringField
27
from Products.Archetypes.public import StringWidget
28
from Products.Archetypes.public import registerType
29
from Products.Archetypes.references import HoldingReference
30
from Products.ATExtensions.field.records import RecordsField
31
32
33
schema = BikaSchema.copy() + Schema((
34
    RecordsField(
35
        "Layout",
36
        schemata="Layout",
37
        required=1,
38
        type="templateposition",
39
        subfields=("pos", "type", "blank_ref", "control_ref", "dup"),
40
        required_subfields=("pos", "type"),
41
        subfield_labels={
42
            "pos": _("Position"),
43
            "type": _("Analysis Type"),
44
            "blank_ref": _("Reference"),
45
            "control_ref": _("Reference"),
46
            "dup": _("Duplicate Of")
47
        },
48
        widget=WorksheetTemplateLayoutWidget(
49
            label=_("Worksheet Layout"),
50
            description=_(
51
                "Specify the size of the Worksheet, e.g. corresponding to a "
52
                "specific instrument's tray size. "
53
                "Then select an Analysis 'type' per Worksheet position."
54
                "Where QC samples are selected, also select which Reference "
55
                "Sample should be used."
56
                "If a duplicate analysis is selected, indicate which sample "
57
                "position it should be a duplicate of"),
58
        )
59
    ),
60
61
    ReferenceField(
62
        "Service",
63
        schemata="Analyses",
64
        required=0,
65
        multiValued=1,
66
        allowed_types=("AnalysisService",),
67
        relationship="WorksheetTemplateAnalysisService",
68
        referenceClass=HoldingReference,
69
        widget=ServicesWidget(
70
            label=_("Analysis Service"),
71
            description=_(
72
                "Select which Analyses should be included on the Worksheet"
73
            ),
74
        )
75
    ),
76
77
    ReferenceField(
78
        "RestrictToMethod",
79
        schemata="Description",
80
        required=0,
81
        vocabulary_display_path_bound=sys.maxint,
82
        vocabulary="_getMethodsVoc",
83
        allowed_types=("Method",),
84
        relationship="WorksheetTemplateMethod",
85
        referenceClass=HoldingReference,
86
        widget=SelectionWidget(
87
            format="select",
88
            label=_("Method"),
89
            description=_(
90
                "Restrict the available analysis services and instruments"
91
                "to those with the selected method."
92
                " In order to apply this change to the services list, you "
93
                "should save the change first."
94
            ),
95
        ),
96
    ),
97
98
    ReferenceField(
99
        "Instrument",
100
        schemata="Description",
101
        required=0,
102
        vocabulary_display_path_bound=sys.maxint,
103
        vocabulary="getInstruments",
104
        allowed_types=("Instrument",),
105
        relationship="WorksheetTemplateInstrument",
106
        referenceClass=HoldingReference,
107
        widget=ReferenceWidget(
108
            checkbox_bound=0,
109
            label=_("Instrument"),
110
            description=_(
111
                "Select the preferred instrument"
112
            ),
113
        ),
114
    ),
115
116
    StringField(
117
        "InstrumentTitle",
118
        widget=StringWidget(
119
            visible=False
120
        )
121
    ),
122
123
    BooleanField(
124
        "EnableMultipleUseOfInstrument",
125
        default=True,
126
        schemata="Description",
127
        widget=BooleanWidget(
128
            label=_("Enable Multiple Use of Instrument in Worksheets."),
129
            description=_(
130
                "If unchecked, Lab Managers won't be able to assign the same "
131
                "Instrument more than one Analyses while creating a Worksheet."
132
            )
133
        )
134
    ),
135
136
))
137
138
schema["title"].schemata = "Description"
139
schema["title"].widget.visible = True
140
141
schema["description"].schemata = "Description"
142
schema["description"].widget.visible = True
143
144
145
class WorksheetTemplate(BaseContent):
146
    """Worksheet Templates
147
    """
148
    security = ClassSecurityInfo()
149
    displayContentsTab = False
150
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
151
    _at_rename_after_creation = True
152
153
    def _renameAfterCreation(self, check_auto_id=False):
154
        from bika.lims.idserver import renameAfterCreation
155
        renameAfterCreation(self)
156
157
    @security.public
158
    def getInstrumentTitle(self):
159
        """Return the instrument title
160
        """
161
        instrument = self.getInstrument()
162
        if not instrument:
163
            return ""
164
        return api.get_title(instrument)
165
166
    @security.public
167
    def getAnalysisTypes(self):
168
        """Return Analysis type displaylist
169
        """
170
        return ANALYSIS_TYPES
171
172
    def getInstruments(self):
173
        """Get the allowed instruments
174
        """
175
        query = {"portal_type": "Instrument", "inactive_state": "active"}
176
177
        if self.getRestrictToMethod():
178
            query.update({
179
                "getMethodUIDs": {
180
                    "query": api.get_uid(self.getRestrictToMethod()),
181
                    "operator": "or",
182
                }
183
            })
184
185
        instruments = api.search(query, "bika_setup_catalog")
186
        items = map(lambda i: (i.UID, i.Title), instruments)
187
188
        instrument = self.getInstrument()
189
        if instrument:
190
            instrument_uids = map(api.get_uid, instruments)
191
            if api.get_uid(instrument) not in instrument_uids:
192
                items.append(
193
                    (api.get_uid(instrument), api.get_title(instrument)))
194
195
        items.sort(lambda x, y: cmp(x[1], y[1]))
196
        items.insert(0, ("", _("No instrument")))
197
198
        return DisplayList(list(items))
199
200
    def getMethodUID(self):
201
        """Return method UID
202
        """
203
        method = self.getRestrictToMethod()
204
        if method:
205
            return method.UID()
206
        return ""
207
208
    def _getMethodsVoc(self):
209
        """Return the registered methods as DisplayList
210
        """
211
        methods = api.search({
212
            "portal_type": "Method",
213
            "inactive_state": "active"
214
        }, "bika_setup_catalog")
215
216
        items = map(lambda m: (api.get_uid(m), api.get_title(m)), methods)
217
        items.sort(lambda x, y: cmp(x[1], y[1]))
218
        items.insert(0, ("", _("Not specified")))
219
220
        return DisplayList(list(items))
221
222
223
registerType(WorksheetTemplate, PROJECTNAME)
224