Passed
Push — 2.x ( a4e524...5d9b1d )
by Ramon
05:13
created

senaite.core.content.interpretationtemplate   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 116
dl 0
loc 191
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A InterpretationTemplate.getRawAnalysisTemplates() 0 6 1
A InterpretationTemplate.getRawSampleTypes() 0 6 1
A InterpretationTemplate.mutator() 0 8 2
A InterpretationTemplate.setSampleTypes() 0 4 1
A InterpretationTemplate.getAnalysisTemplates() 0 6 1
A InterpretationTemplate.getSampleTypes() 0 6 1
A InterpretationTemplate.setAnalysisTemplates() 0 4 1
A InterpretationTemplate.accessor() 0 13 4
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 senaiteMessageFactory as _
24
from bika.lims.catalog import SETUP_CATALOG
25
from bika.lims.interfaces import IDeactivable
26
from plone.autoform import directives
27
from plone.dexterity.content import Item
28
from plone.supermodel import model
29
from Products.CMFCore import permissions
30
from senaite.core.interfaces import IInterpretationTemplate
31
from senaite.core.schema import UIDReferenceField
32
from senaite.core.z3cform.widgets.uidreference import UIDReferenceWidgetFactory
33
from zope.interface import implementer
34
35
36
class IInterpretationTemplateSchema(model.Schema):
37
    """Results Interpretation Template content interface
38
    """
39
    # The behavior IRichTextBehavior applies to this content type, so it
40
    # already provides the "text" field that renders the TinyMCE's Wsiwyg
41
42
    analysis_templates = UIDReferenceField(
43
        title=_(u"Analysis templates"),
44
        description=_(
45
            u"If set, this interpretation template will only be available for "
46
            u"selection on samples that have assigned any of these analysis "
47
            u"templates",
48
        ),
49
        allowed_types=("ARTemplate", ),
50
        multi_valued=True,
51
        required=False,
52
    )
53
54
    sample_types = UIDReferenceField(
55
        title=_(u"Sample types"),
56
        description=_(
57
            u"If set, this interpretation template will only be available for "
58
            u"selection on samples from these types",
59
        ),
60
        allowed_types=("SampleType", ),
61
        multi_valued=True,
62
        required=False,
63
    )
64
65
    directives.widget(
66
        "analysis_templates",
67
        UIDReferenceWidgetFactory,
68
        catalog=SETUP_CATALOG,
69
        query={
70
            "portal_type": "ARTemplate",
71
            "is_active": True,
72
            "sort_on": "title",
73
            "sort_order": "ascending",
74
        },
75
        display_template="<a href='${url}'>${title}</a>",
76
        columns=[
77
            {
78
                "name": "title",
79
                "width": "30",
80
                "align": "left",
81
                "label": _(u"Title"),
82
            }, {
83
                "name": "description",
84
                "width": "70",
85
                "align": "left",
86
                "label": _(u"Description"),
87
            },
88
        ],
89
        limit=15,
90
    )
91
92
    directives.widget(
93
        "sample_types",
94
        UIDReferenceWidgetFactory,
95
        catalog=SETUP_CATALOG,
96
        query={
97
            "portal_type": "SampleType",
98
            "is_active": True,
99
            "sort_on": "title",
100
            "sort_order": "ascending",
101
        },
102
        display_template="<a href='${url}'>${title}</a>",
103
        columns=[
104
            {
105
                "name": "title",
106
                "width": "30",
107
                "align": "left",
108
                "label": _(u"Title"),
109
            }, {
110
                "name": "description",
111
                "width": "70",
112
                "align": "left",
113
                "label": _(u"Description"),
114
            },
115
        ],
116
        limit=15,
117
    )
118
119
120
@implementer(IInterpretationTemplate, IInterpretationTemplateSchema,
121
             IDeactivable)
122
class InterpretationTemplate(Item):
123
    """Results Interpretation Template content
124
    """
125
    # Catalogs where this type will be catalogued
126
    _catalogs = [SETUP_CATALOG]
127
128
    security = ClassSecurityInfo()
129
    exclude_from_nav = True
130
131
    @security.private
132
    def accessor(self, fieldname, raw=False):
133
        """Return the field accessor for the fieldname
134
        """
135
        schema = api.get_schema(self)
136
        if fieldname not in schema:
137
            return None
138
        field = schema[fieldname]
139
        if raw:
140
            if hasattr(field, "get_raw"):
141
                return field.get_raw
142
            return field.getRaw
143
        return field.get
144
145
    @security.private
146
    def mutator(self, fieldname):
147
        """Return the field mutator for the fieldname
148
        """
149
        schema = api.get_schema(self)
150
        if fieldname not in schema:
151
            return None
152
        return schema[fieldname].set
153
154
    @security.protected(permissions.View)
155
    def getAnalysisTemplates(self):  # noqa CamelCase
156
        """Return the ARTemplate objects assigned to this template, if any
157
        """
158
        accessor = self.accessor("analysis_templates")
159
        return accessor(self)
160
161
    @security.protected(permissions.View)
162
    def getRawAnalysisTemplates(self):  # noqa CamelCase
163
        """Return the UIDs of ARTemplate objects assigned, if any
164
        """
165
        accessor = self.accessor("analysis_templates", raw=True)
166
        return accessor(self)
167
168
    @security.protected(permissions.ModifyPortalContent)
169
    def setAnalysisTemplates(self, value):  # noqa CamelCase
170
        mutator = self.mutator("analysis_templates")
171
        mutator(self.context, value)
172
173
    @security.protected(permissions.View)
174
    def getSampleTypes(self):  # noqa CamelCase
175
        """Return the SampleType objects assigned to this template, if any
176
        """
177
        accessor = self.accessor("sample_types")
178
        return accessor(self)
179
180
    @security.protected(permissions.View)
181
    def getRawSampleTypes(self):  # noqa CamelCase
182
        """Return the UIDs of the SampleType objects assigned, if any
183
        """
184
        accessor = self.accessor("sample_types", raw=True)
185
        return accessor(self)
186
187
    @security.protected(permissions.ModifyPortalContent)
188
    def setSampleTypes(self, value):  # noqa CamelCase
189
        mutator = self.mutator("sample_types")
190
        mutator(self.context, value)
191