CaseSymptomsWidget.jsondumps()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
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.Archetypes.Registry import registerWidget
23
from Products.Archetypes.Widget import TypesWidget
24
from Products.ATExtensions.widget import RecordsWidget as ATRecordsWidget
25
from Products.Archetypes.Registry import registerWidget
26
from Products.CMFCore.utils import getToolByName
27
from operator import itemgetter
28
import json
29
30
31
class CaseSymptomsWidget(ATRecordsWidget):
32
    security = ClassSecurityInfo()
33
    _properties = ATRecordsWidget._properties.copy()
34
    _properties.update({
35
        'macro': "bika_health_widgets/casesymptomswidget",
36
        'helper_js': ("bika_health_widgets/casesymptomswidget.js",),
37
        'helper_css': ("bika_health_widgets/casesymptomswidget.css",),
38
        'gender': None,
39
        'allowDelete': False,
40
        'readonly': False,
41
        'combogrid_options': '',
42
    })
43
44
    def process_form(self, instance, field, form, empty_marker=None,
45
                     emptyReturnsMarker=False):
46
        outvalues = []
47
        values = form.get(field.getName(), empty_marker)
48
        for value in values:
49
            if 'Assigned' in value and int(value['Assigned']) == 1:
50
                outvalues.append({'UID': value['UID'],
51
                                  'Title': value.get('Title', ''),
52
                                  'Description': value.get('Description', ''),
53
                                  'Severity': value.get(value['UID'], '0'),
54
                                  'SeverityAllowed':value.get('SeverityAllowed', '0'),
55
                                  'Gender':value.get('Gender', 'dk')})
56
        return outvalues, {}
57
58
    def jsondumps(self, val):
59
        return json.dumps(val)
60
61
    def getSymptoms(self, gender=None):
62
        """ Returns the symptoms from the instance merged with those symptoms
63
            active from bika setup, with severity values assigned to default 0
64
        """
65
        outsymptoms = {}
66
        field = self.aq_parent.Schema()['Symptoms']
67
        value = field.get(self.aq_parent)
68
        casesymptoms = value and value or []
69
70
        if not gender:
71
            patient = self.aq_parent.Schema()['Patient'].get(self.aq_parent)
72
            gender = patient and patient.getGender() or 'dk'
73
74
        symptoms = self.bika_setup_catalog(portal_type='Symptom',
75
                                           is_active=True)
76
        for symptom in symptoms:
77
            symptom = symptom.getObject()
78
            s_gender = symptom.getGender()
79
            outsymptoms[symptom.UID()] = {
80
                'UID': symptom.UID(),
81
                'Title': symptom.Title(),
82
                'Description':symptom.Description(),
83
                'SeverityAllowed':symptom.getSeverityAllowed() and 1 or 0,
84
                'Severity':'0',
85
                'Assigned':0,
86
                'Gender':symptom.getGender(),
87
                'Visible':symptom.getGender()=='dk' or symptom.getGender()==gender}
88
89
        for symptom in casesymptoms:
90
            if 'UID' in symptom:
91
                if symptom['UID'] in outsymptoms \
92
                    and ('Severity' in symptom \
93
                         and symptom['Severity'] is not None \
94
                         and symptom['Severity'] != '0'):
95
                    sym = outsymptoms[symptom['UID']]
96
                    sym['Severity'] = symptom['Severity']
97
                    sym['Assigned'] = 1
98
                else:
99
                    outsymptoms[symptom['UID']] = {'UID': symptom['UID'],
100
                                                   'Title': symptom.get('Title',''),
101
                                                   'Description': symptom.get('Description',''),
102
                                                   'SeverityAllowed': 'SeverityAllowed' in symptom and symptom['SeverityAllowed'] or 0,
103
                                                   'Severity': symptom.get('Severity','0'),
104
                                                   'Assigned': 1,
105
                                                   'Gender':symptom.get('Gender', 'dk'),
106
                                                   'Visible':symptom.get('Gender', 'dk')=='dk' or symptom.get('Gender', 'dk')==gender}
107
        items=[]
108
        for symptom in outsymptoms.values():
109
            items.append(symptom)
110
        items.sort(lambda x, y: cmp(x['Title'].lower(), y['Title'].lower()))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cmp does not seem to be defined.
Loading history...
111
        return items
112
113
registerWidget(CaseSymptomsWidget,
114
               title='CaseSymptomsWidget',
115
               description='Experiencing symptoms and severity',
116
               )
117