Passed
Pull Request — 2.x (#1911)
by Ramon
05:17
created

SampleContainer.getSecuritySealIntact()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims import senaiteMessageFactory as _
5
from bika.lims.interfaces import IDeactivable
6
from plone.autoform import directives
7
from plone.dexterity.content import Container
8
from plone.supermodel import model
9
from senaite.core.catalog import SETUP_CATALOG
10
from senaite.core.interfaces import ISampleContainer
11
from senaite.core.schema import UIDReferenceField
12
from senaite.core.z3cform.widgets.uidreference import UIDReferenceWidgetFactory
13
from zope import schema
14
from zope.interface import Invalid
15
from zope.interface import implementer
16
from zope.interface import invariant
17
18
19
class ISampleContainerSchema(model.Schema):
20
    """Schema interface
21
    """
22
23
    title = schema.TextLine(
24
        title=u"Title",
25
        required=False,
26
    )
27
28
    description = schema.Text(
29
        title=u"Description",
30
        required=False,
31
    )
32
33
    # Container type reference
34
    directives.widget(
35
        "containertype",
36
        UIDReferenceWidgetFactory,
37
        catalog=SETUP_CATALOG,
38
        query="get_containertype_query",
39
        display_template="<a href='${url}'>${title}</a>",
40
        columns="get_default_columns",
41
        limit=5,
42
    )
43
    containertype = UIDReferenceField(
44
        title=_(u"Container Type"),
45
        allowed_types=("ContainerType", ),
46
        multi_valued=False,
47
        description=_(u"Type of the container"),
48
        required=False,
49
    )
50
51
    capacity = schema.TextLine(
52
        title=_("Capacity"),
53
        description=_("Maximum possible size or volume of samples"),
54
        default=u"0 ml",
55
        required=True,
56
    )
57
58
    pre_preserved = schema.Bool(
59
        title=_("Pre-preserved"),
60
        description=_(
61
            "Check this box if this container is already preserved."
62
            "Setting this will short-circuit the preservation workflow "
63
            "for sample partitions stored in this container."),
64
        required=True,
65
    )
66
67
    @invariant
68
    def validate_pre_preserved(data):
69
        """Checks if a preservation is selected
70
        """
71
        if data.pre_preserved is False:
72
            return
73
        if not data.preservation:
74
            raise Invalid(_(
75
                "Pre-preserved containers must have a preservation selected"))
76
77
    # Preservation reference
78
    directives.widget(
79
        "preservation",
80
        UIDReferenceWidgetFactory,
81
        catalog=SETUP_CATALOG,
82
        query="get_preservation_query",
83
        display_template="<a href='${url}'>${title}</a>",
84
        columns="get_default_columns",
85
        limit=5,
86
    )
87
    preservation = UIDReferenceField(
88
        title=_(u"Preservation"),
89
        allowed_types=("Preservation", ),
90
        multi_valued=False,
91
        description=_(u"Preservation method of this container"),
92
        required=False,
93
    )
94
95
    security_seal_intact = schema.Bool(
96
        title=_("Security seal intact"),
97
        description=_(""),
98
        required=False,
99
    )
100
101
102
@implementer(ISampleContainer, ISampleContainerSchema, IDeactivable)
103
class SampleContainer(Container):
104
    """Sample container type
105
    """
106
    # Catalogs where this type will be catalogued
107
    _catalogs = [SETUP_CATALOG]
108
109
    def accessor(self, fieldname):
110
        """Return the field accessor for the fieldname
111
        """
112
        schema = api.get_schema(self)
113
        if fieldname not in schema:
114
            return None
115
        return schema[fieldname].get
116
117
    def mutator(self, fieldname):
118
        """Return the field mutator for the fieldname
119
        """
120
        schema = api.get_schema(self)
121
        if fieldname not in schema:
122
            return None
123
        return schema[fieldname].set
124
125
    def getContainerType(self):
126
        accessor = self.accessor("containertype")
127
        return accessor(self)
128
129
    def setContainerType(self, value):
130
        mutator = self.mutator("containertype")
131
        return mutator(self, value)
132
133
    def getCapacity(self):
134
        accessor = self.accessor("capacity")
135
        return accessor(self)
136
137
    def setCapacity(self, value):
138
        mutator = self.mutator("capacity")
139
        return mutator(self, api.safe_unicode(value))
140
141
    def getPrePreserved(self):
142
        accessor = self.accessor("pre_preserved")
143
        return accessor(self)
144
145
    def setPrePreserved(self, value):
146
        mutator = self.mutator("pre_preserved")
147
        return mutator(self, value)
148
149
    def getPreservation(self):
150
        accessor = self.accessor("preservation")
151
        return accessor(self)
152
153
    def setPreservation(self, value):
154
        mutator = self.mutator("preservation")
155
        return mutator(self, value)
156
157
    def getSecuritySealIntact(self):
158
        accessor = self.accessor("security_seal_intact")
159
        return accessor(self)
160
161
    def setSecuritySealIntact(self, value):
162
        mutator = self.mutator("security_seal_intact")
163
        return mutator(self, bool(value))
164
165
    def get_containertype_query(self):
166
        """Return the query for the containertype field
167
        """
168
        return {
169
            "portal_type": "ContainerType",
170
            "is_active": True,
171
            "sort_on": "title",
172
            "sort_order": "ascending",
173
        }
174
175
    def get_preservation_query(self):
176
        """Return the query for the preservation field
177
        """
178
        return {
179
            "portal_type": "Preservation",
180
            "is_active": True,
181
            "sort_on": "title",
182
            "sort_order": "ascending",
183
        }
184
185
    def get_default_columns(self):
186
        """Returns the default columns for the reference dropdown
187
        """
188
        return [
189
            {
190
                "name": "title",
191
                "width": "30",
192
                "align": "left",
193
                "label": _(u"Title"),
194
            }, {
195
                "name": "description",
196
                "width": "70",
197
                "align": "left",
198
                "label": _(u"Description"),
199
            },
200
        ]
201