CasePatientConditionWidget.getPatientCondition()   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 47
rs 2.4
c 0
b 0
f 0
cc 16
nop 1

How to fix   Complexity   

Complexity

Complex classes like bika.health.widgets.casepatientconditionwidget.CasePatientConditionWidget.getPatientCondition() 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.HEALTH.
4
#
5
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software 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 Products.ATExtensions.widget import RecordsWidget as ATRecordsWidget
23
from Products.Archetypes.Registry import registerWidget
24
import json
25
26
27
class CasePatientConditionWidget(ATRecordsWidget):
28
    security = ClassSecurityInfo()
29
    _properties = ATRecordsWidget._properties.copy()
30
    _properties.update({
31
        'macro': "bika_health_widgets/casepatientconditionwidget",
32
        'helper_js': ("bika_health_widgets/casepatientconditionwidget.js",),
33
        'helper_css': ("bika_health_widgets/casepatientconditionwidget.css",),
34
    })
35
36
    def process_form(self, instance, field, form, empty_marker=None,
37
                     emptyReturnsMarker=False):
38
        # ignore records with empty values
39
        outvalues = []
40
        values = form.get(field.getName(), empty_marker)
41
        for value in values:
42
            if value.get('Value', '').strip() != '':
43
                outvalues.append(value)
44
        return outvalues, {}
45
46
    def jsondumps(self, val):
47
        return json.dumps(val)
48
49
    def getPatientCondition(self):
50
        # self.aq_parent.Schema()['Patient'].set(self.aq_parent.patients.objectValues()[0].UID())
51
        value = self.aq_parent.Schema()['PatientCondition'].get(self.aq_parent)
52
        conditions = len(value) > 0 and value or []
53
54
        # Allow multiple units for each condition. Check if existing conditions
55
        # don't have bika_setup already defined units
56
        heightunits = self.getHeightUnits()
57
        for unit in heightunits:
58
            exists = False
59
            for condition in conditions:
60
                if condition['Condition'] == 'Height' \
61
                    and condition['Unit'] == unit:
62
                    exists = True
63
                    break
64
            if not exists:
65
                conditions.append({'Condition': 'Height',
66
                               'Unit': unit,
67
                               'Value': ''})
68
69
        weightunits = self.getWeightUnits()
70
        for unit in weightunits:
71
            exists = False
72
            for condition in conditions:
73
                if condition['Condition'] == 'Weight' \
74
                    and condition['Unit'] == unit:
75
                    exists = True
76
                    break
77
            if not exists:
78
                conditions.append({'Condition': 'Weight',
79
                               'Unit': unit,
80
                               'Value': ''})
81
82
        weightunits = self.getWaistUnits()
83
        for unit in weightunits:
84
            exists = False
85
            for condition in conditions:
86
                if condition['Condition'] == 'Waist' \
87
                    and condition['Unit'] == unit:
88
                    exists = True
89
                    break
90
            if not exists:
91
                conditions.append({'Condition': 'Waist',
92
                               'Unit': unit,
93
                               'Value': ''})
94
95
        return conditions
96
97
    def getUnits(self, units=None):
98
        return (units and "/" in units) and units.split('/') or [units]
99
100
    def getHeightUnits(self):
101
        field = self.bika_setup.Schema()['PatientConditionsHeightUnits']
102
        return self.getUnits(field.get(self.bika_setup))
103
104
    def getWeightUnits(self):
105
        field = self.bika_setup.Schema()['PatientConditionsWeightUnits']
106
        return self.getUnits(field.get(self.bika_setup))
107
108
    def getWaistUnits(self):
109
        field = self.bika_setup.Schema()['PatientConditionsWaistUnits']
110
        return self.getUnits(field.get(self.bika_setup))
111
112
    def getConditionValue(self, condition, unit):
113
        conditions = self.getPatientCondition()
114
        for cond in conditions:
115
            if cond['Condition'] == condition \
116
                and cond['Unit'] == unit:
117
                return cond['Value']
118
        return ''
119
120
registerWidget(CasePatientConditionWidget,
121
               title='CasePatientConditionWidget',
122
               description='Patient Condition information',)
123