Passed
Push — 2.x ( 5710a9...27b54e )
by Ramon
04:59
created

senaite.core.behaviors.clientshareable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 88
dl 0
loc 154
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A ClientShareableFactory.getClients() 0 4 1
A ClientShareableFactory.getRawClients() 0 4 1
A ClientShareableFactory.accessor() 0 9 3
A ClientShareableFactory.schema() 0 8 2
A ClientShareableFactory.mutator() 0 6 2
A ClientShareableFactory.__init__() 0 3 1
A ClientShareableFactory.setClients() 0 4 1
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-2022 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 plone.autoform import directives
24
from plone.autoform.interfaces import IFormFieldProvider
25
from plone.behavior.interfaces import IBehavior
26
from plone.supermodel import model
27
from plone.supermodel.directives import fieldset
28
from Products.CMFCore import permissions
29
from senaite.core.behaviors.utils import get_behavior_schema
30
from senaite.core.schema import UIDReferenceField
31
from senaite.core.z3cform.widgets.uidreference import UIDReferenceWidgetFactory
32
from zope.interface import implementer
33
from zope.interface import Interface
34
from zope.interface import provider
35
36
37
class IClientShareable(Interface):
38
    """Marker interface to implement by types for which ClientShareableBehavior
39
    can be applied
40
    """
41
    pass
42
43
44
class IClientShareableMarker(Interface):
45
    """Marker interface provided by objects with ClientShareableBehavior
46
    """
47
    pass
48
49
50
@provider(IFormFieldProvider)
51
class IClientShareableBehavior(model.Schema):
52
    """Behavior with schema fields to allow to share the context with users
53
    that belong to other clients
54
    """
55
56
    clients = UIDReferenceField(
57
        title=_(u"Clients"),
58
        description=_(
59
            u"Clients with whom this content will be shared across. This "
60
            u"content will become available on searches to users that belong "
61
            u"to any of the selected clients thanks to the role 'ClientGuest'"
62
        ),
63
        allowed_types=("Client", ),
64
        multi_valued=True,
65
        required=False,
66
    )
67
68
    directives.widget(
69
        "clients",
70
        UIDReferenceWidgetFactory,
71
        catalog="portal_catalog",
72
        query={
73
            "portal_type": "Client",
74
            "is_active": True,
75
            "sort_on": "title",
76
            "sort_order": "ascending",
77
        },
78
        display_template="<a href='${url}'>${title}</a>",
79
        columns=[
80
            {
81
                "name": "title",
82
                "width": "30",
83
                "align": "left",
84
                "label": _(u"Title"),
85
            }, {
86
                "name": "description",
87
                "width": "70",
88
                "align": "left",
89
                "label": _(u"Description"),
90
            },
91
        ],
92
        limit=15,
93
    )
94
95
    fieldset(
96
        "clientshareable",
97
        label=u"Client share",
98
        fields=["clients"],
99
    )
100
101
102
@implementer(IBehavior, IClientShareableBehavior)
103
class ClientShareableFactory(object):
104
    """Factory that provides IClientShareableBehavior"""
105
106
    security = ClassSecurityInfo()
107
108
    def __init__(self, context):
109
        self.context = context
110
        self._schema = None
111
112
    @property
113
    def schema(self):
114
        """Return the schema provided by the underlying behavior
115
        """
116
        if self._schema is None:
117
            behavior = IClientShareableBehavior
118
            self._schema = get_behavior_schema(self.context, behavior)
119
        return self._schema
120
121
    def accessor(self, fieldname, raw=False):
122
        """Return the field accessor for the fieldname
123
        """
124
        if fieldname in self.schema:
125
            field = self.schema[fieldname]
126
            if raw:
127
                return field.get_raw
128
            return field.get
129
        return None
130
131
    def mutator(self, fieldname):
132
        """Return the field mutator for the fieldname
133
        """
134
        if fieldname in self.schema:
135
            return self.schema[fieldname].set
136
        return None
137
138
    @security.protected(permissions.View)
139
    def getClients(self):
140
        accessor = self.accessor("clients")
141
        return accessor(self.context)
142
143
    @security.protected(permissions.View)
144
    def getRawClients(self):
145
        accessor = self.accessor("clients", raw=True)
146
        return accessor(self.context)
147
148
    @security.protected(permissions.ModifyPortalContent)
149
    def setClients(self, value):
150
        mutator = self.mutator("clients")
151
        mutator(self.context, value)
152
153
    clients = property(getClients, setClients)
154