Completed
Branch master (d329f3)
by Jordi
03:26
created

CasePatientConditionWidget.getPatientCondition()   F

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
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
from AccessControl import ClassSecurityInfo
9
from Products.ATExtensions.widget import RecordsWidget as ATRecordsWidget
10
from Products.Archetypes.Registry import registerWidget
11
import json
12
13
14
class CasePatientConditionWidget(ATRecordsWidget):
15
    security = ClassSecurityInfo()
16
    _properties = ATRecordsWidget._properties.copy()
17
    _properties.update({
18
        'macro': "bika_health_widgets/casepatientconditionwidget",
19
        'helper_js': ("bika_health_widgets/casepatientconditionwidget.js",),
20
        'helper_css': ("bika_health_widgets/casepatientconditionwidget.css",),
21
    })
22
23
    def process_form(self, instance, field, form, empty_marker=None,
24
                     emptyReturnsMarker=False):
25
        # ignore records with empty values
26
        outvalues = []
27
        values = form.get(field.getName(), empty_marker)
28
        for value in values:
29
            if value.get('Value', '').strip() != '':
30
                outvalues.append(value)
31
        return outvalues, {}
32
33
    def jsondumps(self, val):
34
        return json.dumps(val)
35
36
    def getPatientCondition(self):
37
        # self.aq_parent.Schema()['Patient'].set(self.aq_parent.patients.objectValues()[0].UID())
38
        value = self.aq_parent.Schema()['PatientCondition'].get(self.aq_parent)
39
        conditions = len(value) > 0 and value or []
40
41
        # Allow multiple units for each condition. Check if existing conditions
42
        # don't have bika_setup already defined units
43
        heightunits = self.getHeightUnits()
44
        for unit in heightunits:
45
            exists = False
46
            for condition in conditions:
47
                if condition['Condition'] == 'Height' \
48
                    and condition['Unit'] == unit:
49
                    exists = True
50
                    break
51
            if not exists:
52
                conditions.append({'Condition': 'Height',
53
                               'Unit': unit,
54
                               'Value': ''})
55
56
        weightunits = self.getWeightUnits()
57
        for unit in weightunits:
58
            exists = False
59
            for condition in conditions:
60
                if condition['Condition'] == 'Weight' \
61
                    and condition['Unit'] == unit:
62
                    exists = True
63
                    break
64
            if not exists:
65
                conditions.append({'Condition': 'Weight',
66
                               'Unit': unit,
67
                               'Value': ''})
68
69
        weightunits = self.getWaistUnits()
70
        for unit in weightunits:
71
            exists = False
72
            for condition in conditions:
73
                if condition['Condition'] == 'Waist' \
74
                    and condition['Unit'] == unit:
75
                    exists = True
76
                    break
77
            if not exists:
78
                conditions.append({'Condition': 'Waist',
79
                               'Unit': unit,
80
                               'Value': ''})
81
82
        return conditions
83
84
    def getUnits(self, units=None):
85
        return (units and "/" in units) and units.split('/') or [units]
86
87
    def getHeightUnits(self):
88
        field = self.bika_setup.Schema()['PatientConditionsHeightUnits']
89
        return self.getUnits(field.get(self.bika_setup))
90
91
    def getWeightUnits(self):
92
        field = self.bika_setup.Schema()['PatientConditionsWeightUnits']
93
        return self.getUnits(field.get(self.bika_setup))
94
95
    def getWaistUnits(self):
96
        field = self.bika_setup.Schema()['PatientConditionsWaistUnits']
97
        return self.getUnits(field.get(self.bika_setup))
98
99
    def getConditionValue(self, condition, unit):
100
        conditions = self.getPatientCondition()
101
        for cond in conditions:
102
            if cond['Condition'] == condition \
103
                and cond['Unit'] == unit:
104
                return cond['Value']
105
        return ''
106
107
registerWidget(CasePatientConditionWidget,
108
               title='CasePatientConditionWidget',
109
               description='Patient Condition information',)
110