Passed
Push — 2.x ( 44675d...5e1af5 )
by Jordi
09:20
created

senaite.core.content.department   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 96
dl 0
loc 159
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Department.getManager() 0 4 1
A IDepartmentSchema.validate_department_id() 0 17 4
A Department.setManager() 0 4 1
A Department.getDepartmentID() 0 5 1
A Department.setDepartmentID() 0 4 1
A Department.getRawManager() 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-2024 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.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.catalog import CONTACT_CATALOG
29
from senaite.core.catalog import SETUP_CATALOG
30
from senaite.core.config.widgets import get_labcontact_columns
31
from senaite.core.content.base import Container
32
from senaite.core.interfaces import IDepartment
33
from senaite.core.schema import UIDReferenceField
34
from senaite.core.z3cform.widgets.uidreference import UIDReferenceWidgetFactory
35
from zope import schema
36
from zope.interface import Invalid
37
from zope.interface import implementer
38
from zope.interface import invariant
39
40
41
class IDepartmentSchema(model.Schema):
42
    """Schema interface
43
    """
44
45
    title = schema.TextLine(
46
        title=u"Title",
47
        required=False,
48
    )
49
50
    description = schema.Text(
51
        title=u"Description",
52
        required=False,
53
    )
54
55
    # Department ID
56
    department_id = schema.TextLine(
57
        title=_(
58
            u"title_department_id",
59
            default=u"Department ID"
60
        ),
61
        description=_(
62
            u"description_department_id",
63
            default=u"Please provide a unique department identifier"
64
        ),
65
        required=True,
66
    )
67
68
    directives.widget(
69
        "manager",
70
        UIDReferenceWidgetFactory,
71
        catalog=CONTACT_CATALOG,
72
        query={
73
            "portal_type": "LabContact",
74
            "is_active": True,
75
            "sort_on": "title",
76
            "sort_order": "ascending",
77
        },
78
        display_template="<a href='${url}'>${getFullname}</a>",
79
        columns=get_labcontact_columns,
80
        limit=5,
81
    )
82
    manager = UIDReferenceField(
83
        title=_(
84
            u"label_department_manager",
85
            default=u"Manager"
86
        ),
87
        description=_(
88
            u"description_department_manager",
89
            default=u"Select a manager from the available personnel "
90
                    u"configured under the 'lab contacts' setup item. "
91
                    u"Departmental managers are referenced on analysis "
92
                    u"results reports containing analyses by their department."
93
        ),
94
        allowed_types=("LabContact", ),
95
        relationship="DepartmentLabContact",
96
        multi_valued=False,
97
        required=True,
98
    )
99
100
    @invariant
101
    def validate_department_id(data):
102
        """Checks if the department ID is unique
103
        """
104
        dpt_id = data.department_id
105
        context = getattr(data, "__context__", None)
106
        if context and context.department_id == dpt_id:
107
            # nothing changed
108
            return
109
        # TODO: Catalog query for same ID
110
        query = {
111
            "portal_type": "Department",
112
            "department_id": dpt_id,
113
        }
114
        results = api.search(query, catalog=SETUP_CATALOG)
115
        if len(results) > 0:
116
            raise Invalid(_("Department ID must be unique"))
117
118
119
@implementer(IDepartment, IDepartmentSchema, IDeactivable)
120
class Department(Container):
121
    """Department
122
    """
123
    # Catalogs where this type will be catalogued
124
    _catalogs = [SETUP_CATALOG]
125
126
    security = ClassSecurityInfo()
127
128
    @security.protected(permissions.View)
129
    def getDepartmentID(self):
130
        accessor = self.accessor("department_id")
131
        value = accessor(self) or ""
132
        return value.encode("utf-8")
133
134
    @security.protected(permissions.ModifyPortalContent)
135
    def setDepartmentID(self, value):
136
        mutator = self.mutator("department_id")
137
        mutator(self, api.safe_unicode(value))
138
139
    # BBB: AT schema field property
140
    DepartmentID = property(getDepartmentID, setDepartmentID)
141
142
    @security.protected(permissions.View)
143
    def getRawManager(self):
144
        accessor = self.accessor("manager", raw=True)
145
        return accessor(self)
146
147
    @security.protected(permissions.View)
148
    def getManager(self):
149
        accessor = self.accessor("manager")
150
        return accessor(self)
151
152
    @security.protected(permissions.ModifyPortalContent)
153
    def setManager(self, value):
154
        mutator = self.mutator("manager")
155
        mutator(self, value)
156
157
    # BBB: AT schema field property
158
    Manager = property(getManager, setManager)
159