Passed
Push — 2.x ( 2a5080...5515a0 )
by Jordi
06:06
created

InterpretationTemplate.accessor()   A

Complexity

Conditions 4

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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