Passed
Push — 2.x ( 285083...cd4fe9 )
by Jordi
06:30
created

bika.lims.content.person.Person.setFullname()   A

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 18
rs 9.55
c 0
b 0
f 0
cc 4
nop 2
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 bikaMessageFactory as _
23
from bika.lims.browser.fields import AddressField
24
from bika.lims.browser.widgets import AddressWidget
25
from bika.lims.config import PROJECTNAME
26
from bika.lims.content.bikaschema import BikaSchema
27
from Products.Archetypes.public import BaseFolder
28
from Products.Archetypes.public import ComputedField
29
from Products.Archetypes.public import ComputedWidget
30
from Products.Archetypes.public import Schema
31
from Products.Archetypes.public import StringField
32
from Products.Archetypes.public import StringWidget
33
from Products.Archetypes.public import registerType
34
from Products.CMFCore import permissions as CMFCorePermissions
35
36
37
schema = BikaSchema.copy() + Schema((
38
39
    StringField(
40
        "Salutation",
41
        widget=StringWidget(
42
            label=_("Salutation"),
43
            description=_("Greeting title eg. Mr, Mrs, Dr"),
44
        ),
45
    ),
46
47
    StringField(
48
        "Firstname",
49
        required=1,
50
        widget=StringWidget(
51
            label=_("Firstname"),
52
        ),
53
    ),
54
55
    StringField(
56
        "Middleinitial",
57
        required=0,
58
        widget=StringWidget(
59
            label=_("Middle initial"),
60
        ),
61
    ),
62
63
    StringField(
64
        "Middlename",
65
        required=0,
66
        widget=StringWidget(
67
            label=_("Middle name"),
68
        ),
69
    ),
70
71
    StringField(
72
        "Surname",
73
        required=1,
74
        widget=StringWidget(
75
            label=_("Surname"),
76
        ),
77
    ),
78
79
    ComputedField(
80
        "Fullname",
81
        expression="context.getFullname()",
82
        searchable=1,
83
        widget=ComputedWidget(
84
            label=_("Full Name"),
85
            visible={"edit": "invisible", "view": "invisible"},
86
        ),
87
    ),
88
89
    StringField(
90
        "Username",
91
        widget=StringWidget(
92
            visible=False
93
        ),
94
    ),
95
96
    StringField(
97
        "EmailAddress",
98
        schemata="Email Telephone Fax",
99
        searchable=1,
100
        widget=StringWidget(
101
            label=_("Email Address"),
102
        ),
103
        validators=("isEmail", )
104
    ),
105
106
    StringField(
107
        "BusinessPhone",
108
        schemata="Email Telephone Fax",
109
        widget=StringWidget(
110
            label=_("Phone (business)"),
111
        ),
112
    ),
113
114
    StringField(
115
        "BusinessFax",
116
        schemata="Email Telephone Fax",
117
        widget=StringWidget(
118
            label=_("Fax (business)"),
119
        ),
120
    ),
121
122
    StringField(
123
        "HomePhone",
124
        schemata="Email Telephone Fax",
125
        widget=StringWidget(
126
            label=_("Phone (home)"),
127
        ),
128
    ),
129
130
    StringField(
131
        "MobilePhone",
132
        schemata="Email Telephone Fax",
133
        widget=StringWidget(
134
            label=_("Phone (mobile)"),
135
        ),
136
    ),
137
138
    StringField(
139
        "JobTitle",
140
        widget=StringWidget(
141
            label=_("Job title"),
142
        ),
143
    ),
144
145
    StringField(
146
        "Department",
147
        widget=StringWidget(
148
            label=_("Department"),
149
        ),
150
    ),
151
152
    AddressField(
153
        "PhysicalAddress",
154
        schemata="Address",
155
        widget=AddressWidget(
156
           label=_("Physical address"),
157
        ),
158
    ),
159
160
    ComputedField(
161
        "City",
162
        expression="context.getPhysicalAddress().get('city')",
163
        searchable=1,
164
        widget=ComputedWidget(
165
            visible=False
166
        ),
167
    ),
168
169
    ComputedField(
170
        "District",
171
        expression="context.getPhysicalAddress().get('district')",
172
        searchable=1,
173
        widget=ComputedWidget(
174
            visible=False
175
        ),
176
    ),
177
178
    ComputedField(
179
        "PostalCode",
180
        expression="context.getPhysicalAddress().get('postalCode')",
181
        searchable=1,
182
        widget=ComputedWidget(
183
            visible=False
184
        ),
185
    ),
186
187
    ComputedField(
188
        "Country",
189
        expression="context.getPhysicalAddress().get('country')",
190
        searchable=1,
191
        widget=ComputedWidget(
192
            visible=False
193
        ),
194
    ),
195
196
    AddressField(
197
        "PostalAddress",
198
        schemata="Address",
199
        widget=AddressWidget(
200
           label=_("Postal address"),
201
        ),
202
    ),
203
204
))
205
206
207
class Person(BaseFolder):
208
    """Person base class
209
    """
210
211
    security = ClassSecurityInfo()
212
    displayContentsTab = False
213
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
214
215
    def getPossibleAddresses(self):
216
        return ["PhysicalAddress", "PostalAddress"]
217
218
    @security.protected(CMFCorePermissions.ModifyPortalContent)
219
    def setFullname(self, value):
220
        parts = value.split(" ")
221
        length = len(parts)
222
        if length == 2:
223
            self.setFirstname(parts[0])
224
            self.setSurname(parts[1])
225
        elif length == 3:
226
            self.setFirstname(parts[0])
227
            self.setMiddlename(parts[1])
228
            self.setSurname(parts[2])
229
        elif length == 4:
230
            self.setFirstname(parts[0])
231
            self.setMiddleinitial(parts[1])
232
            self.setMiddlename(parts[2])
233
            self.setSurname(parts[3])
234
        else:
235
            self.setSurname(value)
236
237
    def getFullname(self):
238
        """Person's Fullname
239
        """
240
241
        fn = self.getFirstname()
242
        mi = self.getMiddleinitial()
243
        md = self.getMiddlename()
244
        sn = self.getSurname()
245
        fullname = ""
246
        if fn or sn:
247
            if mi and md:
248
                fullname = "%s %s %s %s" % (
249
                    self.getFirstname(),
250
                    self.getMiddleinitial(),
251
                    self.getMiddlename(),
252
                    self.getSurname())
253
            elif mi:
254
                fullname = "%s %s %s" % (
255
                    self.getFirstname(),
256
                    self.getMiddleinitial(),
257
                    self.getSurname())
258
            elif md:
259
                fullname = "%s %s %s" % (
260
                    self.getFirstname(),
261
                    self.getMiddlename(),
262
                    self.getSurname())
263
            else:
264
                fullname = '%s %s' % (self.getFirstname(), self.getSurname())
265
        return fullname.strip()
266
267
    Title = getFullname
268
269
    @security.protected(CMFCorePermissions.ManagePortal)
270
    def hasUser(self):
271
        """check if contact has user
272
        """
273
        return self.portal_membership.getMemberById(
274
            self.getUsername()) is not None
275
276
277
registerType(Person, PROJECTNAME)
278