Passed
Pull Request — 2.x (#1911)
by Jordi
12:55 queued 07:53
created

senaite.core.content.samplecontainer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 154
dl 0
loc 218
rs 10
c 0
b 0
f 0

16 Methods

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