Passed
Pull Request — 2.x (#1911)
by Ramon
04:43
created

senaite.core.content.samplecontainer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 127
dl 0
loc 187
rs 10
c 0
b 0
f 0

15 Methods

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