Passed
Push — 2.x ( 041ff6...87c3c9 )
by Jordi
08:54
created

bika.lims.content.worksheettemplate.WorksheetTemplate.getRawInstruments()   A

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 23
rs 9.6
c 0
b 0
f 0
cc 4
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A bika.lims.content.worksheettemplate.WorksheetTemplate.getMethodUID() 0 4 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-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from AccessControl import ClassSecurityInfo
22
from bika.lims import api
23
from bika.lims import bikaMessageFactory as _
24
from bika.lims.browser.fields import UIDReferenceField
25
from bika.lims.browser.widgets import ServicesWidget
26
from bika.lims.browser.widgets import WorksheetTemplateLayoutWidget
27
from bika.lims.config import ANALYSIS_TYPES
28
from bika.lims.config import PROJECTNAME
29
from bika.lims.content.bikaschema import BikaSchema
30
from bika.lims.interfaces import IDeactivable
31
from bika.lims.interfaces import IHaveInstrument
32
from bika.lims.interfaces import IWorksheetTemplate
33
from Products.Archetypes.public import BaseContent
34
from Products.Archetypes.public import BooleanField
35
from Products.Archetypes.public import BooleanWidget
36
from Products.Archetypes.public import Schema
37
from Products.Archetypes.public import StringField
38
from Products.Archetypes.public import StringWidget
39
from Products.Archetypes.public import registerType
40
from senaite.core import logger
41
from senaite.core.browser.fields.records import RecordsField
42
from senaite.core.browser.widgets.referencewidget import ReferenceWidget
43
from senaite.core.catalog import SETUP_CATALOG
44
from zope.interface import implements
45
46
schema = BikaSchema.copy() + Schema((
47
    RecordsField(
48
        "Layout",
49
        schemata="Layout",
50
        required=1,
51
        type="templateposition",
52
        subfields=("pos", "type", "blank_ref", "control_ref", "dup"),
53
        required_subfields=("pos", "type"),
54
        subfield_labels={
55
            "pos": _("Position"),
56
            "type": _("Analysis Type"),
57
            "blank_ref": _("Reference"),
58
            "control_ref": _("Reference"),
59
            "dup": _("Duplicate Of")
60
        },
61
        widget=WorksheetTemplateLayoutWidget(
62
            label=_("Worksheet Layout"),
63
            description=_(
64
                "Specify the size of the Worksheet, e.g. corresponding to a "
65
                "specific instrument's tray size. "
66
                "Then select an Analysis 'type' per Worksheet position."
67
                "Where QC samples are selected, also select which Reference "
68
                "Sample should be used."
69
                "If a duplicate analysis is selected, indicate which sample "
70
                "position it should be a duplicate of"),
71
        )
72
    ),
73
74
    UIDReferenceField(
75
        "Service",
76
        schemata="Analyses",
77
        required=0,
78
        multiValued=1,
79
        allowed_types=("AnalysisService",),
80
        widget=ServicesWidget(
81
            label=_("Analysis Service"),
82
            description=_(
83
                "Select which Analyses should be included on the Worksheet"
84
            ),
85
        )
86
    ),
87
88
    UIDReferenceField(
89
        "RestrictToMethod",
90
        allowed_types=("Method",),
91
        widget=ReferenceWidget(
92
            label=_(
93
                "label_worksheettemplate_restrict_to_method",
94
                default="Restrict to Method"),
95
            description=_(
96
                "description_worksheettemplate_restrict_to_method",
97
                default="Restrict the available analysis services and "
98
                "instruments to those with the selected method. <br/>"
99
                "In order to apply this change to the services list, you "
100
                "should save the change first."
101
            ),
102
            catalog=SETUP_CATALOG,
103
            query={
104
                "portal_type": "Method",
105
                "is_active": True,
106
                "sort_limit": 5,
107
                "sort_on": "sortable_title",
108
                "sort_order": "ascending",
109
            },
110
        ),
111
    ),
112
113
    UIDReferenceField(
114
        "Instrument",
115
        allowed_types=("Instrument",),
116
        widget=ReferenceWidget(
117
            label=_(
118
                "label_worksheettemplate_instrument",
119
                default="Preferred instrument"),
120
            description=_(
121
                "description_worksheettemplate_instrument",
122
                default="Select the preferred instrument"
123
            ),
124
            catalog=SETUP_CATALOG,
125
            query="get_widget_instrument_query",
126
        ),
127
    ),
128
129
    StringField(
130
        "InstrumentTitle",
131
        widget=StringWidget(
132
            visible=False
133
        )
134
    ),
135
136
    BooleanField(
137
        "EnableMultipleUseOfInstrument",
138
        default=True,
139
        widget=BooleanWidget(
140
            label=_("Enable Multiple Use of Instrument in Worksheets."),
141
            description=_(
142
                "If unchecked, Lab Managers won't be able to assign the same "
143
                "Instrument more than one Analyses while creating a Worksheet."
144
            )
145
        )
146
    ),
147
148
))
149
150
schema["title"].widget.visible = True
151
schema["description"].widget.visible = True
152
schema["description"].widget.description = ""
153
154
155
class WorksheetTemplate(BaseContent):
156
    """Worksheet Templates
157
    """
158
    implements(IWorksheetTemplate, IHaveInstrument, IDeactivable)
159
    security = ClassSecurityInfo()
160
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
161
    _at_rename_after_creation = True
162
163
    def _renameAfterCreation(self, check_auto_id=False):
164
        from senaite.core.idserver import renameAfterCreation
165
        renameAfterCreation(self)
166
167
    @security.public
168
    def getAnalysisTypes(self):
169
        """Return Analysis type displaylist
170
        """
171
        return ANALYSIS_TYPES
172
173
    def get_widget_instrument_query(self, **kw):
174
        """Return the preferred instruments
175
        """
176
        query = {
177
            "portal_type": "Instrument",
178
            "is_active": True,
179
            "sort_on": "sortable_title",
180
            "sort_order": "ascending",
181
        }
182
183
        # Restrict available instruments to those with the selected method
184
        method_uid = self.getRawRestrictToMethod()
185
        if method_uid:
186
            # prepare subquery
187
            uids = []
188
            brains = api.search(query, SETUP_CATALOG)
189
            for brain in brains:
190
                uid = api.get_uid(brain)
191
                instrument = api.get_object(brain)
192
                if method_uid in instrument.getRawMethods():
193
                    uids.append(uid)
194
            # create a simple UID query
195
            query = {"UID": uids}
196
197
        logger.info("get_widget_contact_query: %r" % query)
198
        return query
199
200
    def getMethodUID(self):
201
        """Return method UID
202
        """
203
        return self.getRawRestrictToMethod()
204
205
206
registerType(WorksheetTemplate, PROJECTNAME)
207