Passed
Push — 2.x ( da17b1...a43b53 )
by Jordi
06:56
created

Setup.setImmediateResultsEntry()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
3
from AccessControl import ClassSecurityInfo
4
from bika.lims import api
5
from plone.app.textfield import IRichTextValue
6
from plone.app.textfield.widget import RichTextFieldWidget  # TBD: port to core
7
from plone.autoform import directives
8
from plone.formwidget.namedfile.widget import NamedFileFieldWidget
9
from plone.supermodel import model
10
from Products.CMFCore import permissions
11
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
12
from senaite.core.catalog import AUDITLOG_CATALOG
13
from senaite.core.content.base import Container
14
from senaite.core.interfaces import IHideActionsMenu
15
from senaite.core.interfaces import ISetup
16
from senaite.core.schema import RichTextField
17
from senaite.impress import senaiteMessageFactory as _
18
from zope import schema
19
from zope.interface import implementer
20
from zope.interface import provider
21
from zope.schema.interfaces import IContextAwareDefaultFactory
22
23
24
@provider(IContextAwareDefaultFactory)
25
def default_email_body_sample_publication(context):
26
    """Returns the default body text for publication emails
27
    """
28
    view = api.get_view("senaite_view", context=api.get_setup())
29
    if view is None:
30
        # Test fixture
31
        return u""
32
    tpl = ViewPageTemplateFile(
33
        "../browser/setup/templates/email_body_sample_publication.pt")
34
    return tpl(view)
35
36
37
class ISetupSchema(model.Schema):
38
    """Schema and marker interface
39
    """
40
41
    directives.widget("email_body_sample_publication", RichTextFieldWidget)
42
    email_body_sample_publication = RichTextField(
43
        title=_(u"Publication Email Text"),
44
        description=_(
45
            "The default text that is used for the publication email."),
46
        defaultFactory=default_email_body_sample_publication,
47
        required=False,
48
    )
49
50
    enable_global_auditlog = schema.Bool(
51
        title=_(u"Enable global Auditlog"),
52
        description=_(
53
            "The global Auditlog shows all modifications of the system. "
54
            "When enabled, all entities will be indexed in a separate "
55
            "catalog. This will increase the time when objects are "
56
            "created or modified."
57
        ),
58
        default=False,
59
    )
60
61
    # NOTE:
62
    # We use the `NamedFileFieldWidget` instead of `NamedImageFieldWidget`
63
    # by purpose! Using the latter rises this PIL error (appears only in log):
64
    # IOError: cannot identify image file <cStringIO.StringI object at ...>
65
    directives.widget("site_logo", NamedFileFieldWidget)
66
    site_logo = schema.Bytes(
67
        title=_(u"Site Logo"),
68
        description=_(u"This shows a custom logo on your SENAITE site."),
69
        required=False,
70
    )
71
72
    site_logo_css = schema.ASCII(
73
        title=_(u"Site Logo CSS"),
74
        description=_(
75
            u"Add custom CSS rules for the Logo, "
76
            u"e.g. height:15px; width:150px;"
77
        ),
78
        required=False,
79
    )
80
81
    immediate_results_entry = schema.Bool(
82
        title=_(u"Immediate results entry"),
83
        description=_(
84
            "description_senaitesetup_immediateresultsentry",
85
            default=u"Allow the user to directly enter results after sample "
86
            "creation, e.g. to enter field results immediately, or lab "
87
            "results, when the automatic sample reception is activated."
88
        ),
89
    )
90
91
    ###
92
    # Fieldsets
93
    ###
94
    model.fieldset(
95
        "analyses",
96
        label=_("label_senaitesetup_fieldset_analyses", default=u"Analyses"),
97
        fields=[
98
            "immediate_results_entry",
99
        ]
100
    )
101
102
    model.fieldset(
103
        "notifications",
104
        label=_(u"Notifications"),
105
        fields=[
106
            "email_body_sample_publication",
107
        ]
108
    )
109
110
    model.fieldset(
111
        "appearance",
112
        label=_(u"Appearance"),
113
        fields=[
114
            "site_logo",
115
            "site_logo_css",
116
        ]
117
    )
118
119
120
@implementer(ISetup, ISetupSchema, IHideActionsMenu)
121
class Setup(Container):
122
    """SENAITE Setup Folder
123
    """
124
    security = ClassSecurityInfo()
125
126
    @security.protected(permissions.View)
127
    def getEmailBodySamplePublication(self):
128
        """Returns the transformed email body text for publication emails
129
        """
130
        accessor = self.accessor("email_body_sample_publication")
131
        value = accessor(self)
132
        if IRichTextValue.providedBy(value):
133
            # Transforms the raw value to the output mimetype
134
            value = value.output_relative_to(self)
135
        if not value:
136
            # Always fallback to default value
137
            value = default_email_body_sample_publication(self)
138
        return value
139
140
    @security.protected(permissions.ModifyPortalContent)
141
    def setEmailBodySamplePublication(self, value):
142
        """Set email body text for publication emails
143
        """
144
        mutator = self.mutator("email_body_sample_publication")
145
        return mutator(self, value)
146
147
    @security.protected(permissions.View)
148
    def getEnableGlobalAuditlog(self):
149
        """Returns if the global Auditlog is enabled
150
        """
151
        accessor = self.accessor("enable_global_auditlog")
152
        return accessor(self)
153
154
    @security.protected(permissions.ModifyPortalContent)
155
    def setEnableGlobalAuditlog(self, value):
156
        """Enable/Disable global Auditlogging
157
        """
158
        if value is False:
159
            # clear the auditlog catalog
160
            catalog = api.get_tool(AUDITLOG_CATALOG)
161
            catalog.manage_catalogClear()
162
        mutator = self.mutator("enable_global_auditlog")
163
        return mutator(self, value)
164
165
    @security.protected(permissions.View)
166
    def getSiteLogo(self):
167
        """Returns the global site logo
168
        """
169
        accessor = self.accessor("site_logo")
170
        return accessor(self)
171
172
    @security.protected(permissions.ModifyPortalContent)
173
    def setSiteLogo(self, value):
174
        """Set the site logo
175
        """
176
        mutator = self.mutator("site_logo")
177
        return mutator(self, value)
178
179
    @security.protected(permissions.View)
180
    def getSiteLogoCSS(self):
181
        """Returns the global site logo
182
        """
183
        accessor = self.accessor("site_logo_css")
184
        return accessor(self)
185
186
    @security.protected(permissions.ModifyPortalContent)
187
    def setSiteLogoCSS(self, value):
188
        """Set the site logo
189
        """
190
        mutator = self.mutator("site_logo_css")
191
        return mutator(self, value)
192
193
    @security.protected(permissions.View)
194
    def getImmediateResultsEntry(self):
195
        """Returns if immediate results entry is enabled or not
196
        """
197
        accessor = self.accessor("immediate_results_entry")
198
        return accessor(self)
199
200
    @security.protected(permissions.ModifyPortalContent)
201
    def setImmediateResultsEntry(self, value):
202
        """Enable/Disable global Auditlogging
203
        """
204
        mutator = self.mutator("immediate_results_entry")
205
        return mutator(self, value)
206