Passed
Push — master ( feb3de...56c573 )
by Jordi
04:25
created

Person.getFullname()   B

Complexity

Conditions 7

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 29
rs 7.8799
c 0
b 0
f 0
cc 7
nop 1
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from AccessControl import ClassSecurityInfo
9
from bika.lims import bikaMessageFactory as _
10
from bika.lims.browser.fields import AddressField
11
from bika.lims.browser.widgets import AddressWidget
12
from bika.lims.config import PROJECTNAME
13
from bika.lims.content.bikaschema import BikaSchema
14
from Products.Archetypes.public import BaseFolder
15
from Products.Archetypes.public import ComputedField
16
from Products.Archetypes.public import ComputedWidget
17
from Products.Archetypes.public import Schema
18
from Products.Archetypes.public import StringField
19
from Products.Archetypes.public import StringWidget
20
from Products.Archetypes.public import registerType
21
from Products.CMFCore import permissions as CMFCorePermissions
22
from Products.CMFCore.utils import getToolByName
23
24
25
schema = BikaSchema.copy() + Schema((
26
27
    StringField(
28
        "Salutation",
29
        widget=StringWidget(
30
            label=_("Salutation"),
31
            description=_("Greeting title eg. Mr, Mrs, Dr"),
32
        ),
33
    ),
34
35
    StringField(
36
        "Firstname",
37
        required=1,
38
        widget=StringWidget(
39
            label=_("Firstname"),
40
        ),
41
    ),
42
43
    StringField(
44
        "Middleinitial",
45
        required=0,
46
        widget=StringWidget(
47
            label=_("Middle initial"),
48
        ),
49
    ),
50
51
    StringField(
52
        "Middlename",
53
        required=0,
54
        widget=StringWidget(
55
            label=_("Middle name"),
56
        ),
57
    ),
58
59
    StringField(
60
        "Surname",
61
        required=1,
62
        widget=StringWidget(
63
            label=_("Surname"),
64
        ),
65
    ),
66
67
    ComputedField(
68
        "Fullname",
69
        expression="context.getFullname()",
70
        searchable=1,
71
        widget=ComputedWidget(
72
            label=_("Full Name"),
73
            visible={"edit": "invisible", "view": "invisible"},
74
        ),
75
    ),
76
77
    StringField(
78
        "Username",
79
        widget=StringWidget(
80
            visible=False
81
        ),
82
    ),
83
84
    StringField(
85
        "EmailAddress",
86
        schemata="Email Telephone Fax",
87
        searchable=1,
88
        widget=StringWidget(
89
            label=_("Email Address"),
90
        ),
91
    ),
92
93
    StringField(
94
        "BusinessPhone",
95
        schemata="Email Telephone Fax",
96
        widget=StringWidget(
97
            label=_("Phone (business)"),
98
        ),
99
    ),
100
101
    StringField(
102
        "BusinessFax",
103
        schemata="Email Telephone Fax",
104
        widget=StringWidget(
105
            label=_("Fax (business)"),
106
        ),
107
    ),
108
109
    StringField(
110
        "HomePhone",
111
        schemata="Email Telephone Fax",
112
        widget=StringWidget(
113
            label=_("Phone (home)"),
114
        ),
115
    ),
116
117
    StringField(
118
        "MobilePhone",
119
        schemata="Email Telephone Fax",
120
        widget=StringWidget(
121
            label=_("Phone (mobile)"),
122
        ),
123
    ),
124
125
    StringField(
126
        "JobTitle",
127
        widget=StringWidget(
128
            label=_("Job title"),
129
        ),
130
    ),
131
132
    StringField(
133
        "Department",
134
        widget=StringWidget(
135
            label=_("Department"),
136
        ),
137
    ),
138
139
    AddressField(
140
        "PhysicalAddress",
141
        schemata="Address",
142
        widget=AddressWidget(
143
           label=_("Physical address"),
144
        ),
145
    ),
146
147
    ComputedField(
148
        "City",
149
        expression="context.getPhysicalAddress().get('city')",
150
        searchable=1,
151
        widget=ComputedWidget(
152
            visible=False
153
        ),
154
    ),
155
156
    ComputedField(
157
        "District",
158
        expression="context.getPhysicalAddress().get('district')",
159
        searchable=1,
160
        widget=ComputedWidget(
161
            visible=False
162
        ),
163
    ),
164
165
    ComputedField(
166
        "PostalCode",
167
        expression="context.getPhysicalAddress().get('postalCode')",
168
        searchable=1,
169
        widget=ComputedWidget(
170
            visible=False
171
        ),
172
    ),
173
174
    ComputedField(
175
        "Country",
176
        expression="context.getPhysicalAddress().get('country')",
177
        searchable=1,
178
        widget=ComputedWidget(
179
            visible=False
180
        ),
181
    ),
182
183
    AddressField(
184
        "PostalAddress",
185
        schemata="Address",
186
        widget=AddressWidget(
187
           label=_("Postal address"),
188
        ),
189
    ),
190
191
    ComputedField(
192
        "ObjectWorkflowStates",
193
        expression="context.getObjectWorkflowStates()",
194
        searchable=1,
195
        widget=ComputedWidget(
196
            visible=False
197
        ),
198
    ),
199
))
200
201
202
class Person(BaseFolder):
203
    """Person base class
204
    """
205
206
    security = ClassSecurityInfo()
207
    displayContentsTab = False
208
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
209
210
    def getPossibleAddresses(self):
211
        return ["PhysicalAddress", "PostalAddress"]
212
213
    def getFullname(self):
214
        """Person's Fullname
215
        """
216
217
        fn = self.getFirstname()
218
        mi = self.getMiddleinitial()
219
        md = self.getMiddlename()
220
        sn = self.getSurname()
221
        fullname = ""
222
        if fn or sn:
223
            if mi and md:
224
                fullname = "%s %s %s %s" % (
225
                    self.getFirstname(),
226
                    self.getMiddleinitial(),
227
                    self.getMiddlename(),
228
                    self.getSurname())
229
            elif mi:
230
                fullname = "%s %s %s" % (
231
                    self.getFirstname(),
232
                    self.getMiddleinitial(),
233
                    self.getSurname())
234
            elif md:
235
                fullname = "%s %s %s" % (
236
                    self.getFirstname(),
237
                    self.getMiddlename(),
238
                    self.getSurname())
239
            else:
240
                fullname = '%s %s' % (self.getFirstname(), self.getSurname())
241
        return fullname.strip()
242
243
    def getListingname(self):
244
        """Person's Fullname as Surname, Firstname
245
        """
246
        fn = self.getFirstname()
247
        mi = self.getMiddleinitial()
248
        md = self.getMiddlename()
249
        sn = self.getSurname()
250
        fullname = ""
251
        if fn and sn:
252
            fullname = "%s, %s" % (
253
                self.getSurname(),
254
                self.getFirstname())
255
        elif fn or sn:
256
            fullname = "%s %s" % (
257
                self.getSurname(),
258
                self.getFirstname())
259
        else:
260
            fullname = ""
261
262
        if fullname != "":
263
            if mi and md:
264
                fullname = "%s %s %s" % (
265
                    fullname,
266
                    self.getMiddleinitial(),
267
                    self.getMiddlename())
268
            elif mi:
269
                fullname = "%s %s" % (
270
                    fullname,
271
                    self.getMiddleinitial())
272
            elif md:
273
                fullname = "%s %s" % (
274
                    fullname,
275
                    self.getMiddlename())
276
277
        return fullname.strip()
278
279
    Title = getFullname
280
281
    @security.protected(CMFCorePermissions.ManagePortal)
282
    def hasUser(self):
283
        """check if contact has user
284
        """
285
        return self.portal_membership.getMemberById(
286
            self.getUsername()) is not None
287
288 View Code Duplication
    def getObjectWorkflowStates(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
289
        """Returns a dictionary with the workflow id as key and workflow state
290
        as value.
291
292
        :returns: {'review_state':'active',...}
293
        """
294
        workflow = getToolByName(self, 'portal_workflow')
295
        states = {}
296
        for w in workflow.getWorkflowsFor(self):
297
            state = w._getWorkflowStateOf(self).id
298
            states[w.state_var] = state
299
        return states
300
301
302
registerType(Person, PROJECTNAME)
303