Passed
Push — master ( 70be41...e1228c )
by Ramon
11:12
created

bika.lims.content.person.Person.getListingname()   C

Complexity

Conditions 10

Size

Total Lines 35
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 35
rs 5.9999
c 0
b 0
f 0
cc 10
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bika.lims.content.person.Person.getObjectWorkflowStates() 12 12 2
A bika.lims.content.person.Person.hasUser() 0 6 1

How to fix   Complexity   

Complexity

Complex classes like bika.lims.content.person.Person.getListingname() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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-2019 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
from Products.CMFCore.utils import getToolByName
36
37
38
schema = BikaSchema.copy() + Schema((
39
40
    StringField(
41
        "Salutation",
42
        widget=StringWidget(
43
            label=_("Salutation"),
44
            description=_("Greeting title eg. Mr, Mrs, Dr"),
45
        ),
46
    ),
47
48
    StringField(
49
        "Firstname",
50
        required=1,
51
        widget=StringWidget(
52
            label=_("Firstname"),
53
        ),
54
    ),
55
56
    StringField(
57
        "Middleinitial",
58
        required=0,
59
        widget=StringWidget(
60
            label=_("Middle initial"),
61
        ),
62
    ),
63
64
    StringField(
65
        "Middlename",
66
        required=0,
67
        widget=StringWidget(
68
            label=_("Middle name"),
69
        ),
70
    ),
71
72
    StringField(
73
        "Surname",
74
        required=1,
75
        widget=StringWidget(
76
            label=_("Surname"),
77
        ),
78
    ),
79
80
    ComputedField(
81
        "Fullname",
82
        expression="context.getFullname()",
83
        searchable=1,
84
        widget=ComputedWidget(
85
            label=_("Full Name"),
86
            visible={"edit": "invisible", "view": "invisible"},
87
        ),
88
    ),
89
90
    StringField(
91
        "Username",
92
        widget=StringWidget(
93
            visible=False
94
        ),
95
    ),
96
97
    StringField(
98
        "EmailAddress",
99
        schemata="Email Telephone Fax",
100
        searchable=1,
101
        widget=StringWidget(
102
            label=_("Email Address"),
103
        ),
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
    ComputedField(
205
        "ObjectWorkflowStates",
206
        expression="context.getObjectWorkflowStates()",
207
        searchable=1,
208
        widget=ComputedWidget(
209
            visible=False
210
        ),
211
    ),
212
))
213
214
215
class Person(BaseFolder):
216
    """Person base class
217
    """
218
219
    security = ClassSecurityInfo()
220
    displayContentsTab = False
221
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
222
223
    def getPossibleAddresses(self):
224
        return ["PhysicalAddress", "PostalAddress"]
225
226
    def getFullname(self):
227
        """Person's Fullname
228
        """
229
230
        fn = self.getFirstname()
231
        mi = self.getMiddleinitial()
232
        md = self.getMiddlename()
233
        sn = self.getSurname()
234
        fullname = ""
235
        if fn or sn:
236
            if mi and md:
237
                fullname = "%s %s %s %s" % (
238
                    self.getFirstname(),
239
                    self.getMiddleinitial(),
240
                    self.getMiddlename(),
241
                    self.getSurname())
242
            elif mi:
243
                fullname = "%s %s %s" % (
244
                    self.getFirstname(),
245
                    self.getMiddleinitial(),
246
                    self.getSurname())
247
            elif md:
248
                fullname = "%s %s %s" % (
249
                    self.getFirstname(),
250
                    self.getMiddlename(),
251
                    self.getSurname())
252
            else:
253
                fullname = '%s %s' % (self.getFirstname(), self.getSurname())
254
        return fullname.strip()
255
256
    Title = getFullname
257
258
    @security.protected(CMFCorePermissions.ManagePortal)
259
    def hasUser(self):
260
        """check if contact has user
261
        """
262
        return self.portal_membership.getMemberById(
263
            self.getUsername()) is not None
264
265 View Code Duplication
    def getObjectWorkflowStates(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
266
        """Returns a dictionary with the workflow id as key and workflow state
267
        as value.
268
269
        :returns: {'review_state':'active',...}
270
        """
271
        workflow = getToolByName(self, 'portal_workflow')
272
        states = {}
273
        for w in workflow.getWorkflowsFor(self):
274
            state = w._getWorkflowStateOf(self).id
275
            states[w.state_var] = state
276
        return states
277
278
279
registerType(Person, PROJECTNAME)
280