Issues (175)

bika/health/widgets/casemenstrualstatuswidget.py (1 issue)

Severity
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
from Products.CMFCore.utils import getToolByName
25
from bika.health.config import MENSTRUAL_STATUSES
26
import json
27
28
29
class CaseMenstrualStatusWidget(ATRecordsWidget):
30
    security = ClassSecurityInfo()
31
    _properties = ATRecordsWidget._properties.copy()
32
    _properties.update({
33
        'macro': "bika_health_widgets/casemenstrualstatuswidget",
34
        'helper_js': ("bika_health_widgets/casemenstrualstatuswidget.js",
35
                      "bika_health_widgets/patientmenstrualstatuswidget.js"),
36
        'helper_css': ("bika_health_widgets/casemenstrualstatuswidget.css",),
37
    })
38
39
    def process_form(self, instance, field, form, empty_marker=None,
40
                     emptyReturnsMarker=False):
41
        outvalues = []
42
        values = form.get(field.getName(), empty_marker)
43
        for value in values:
44
            outvalues.append({'FirstDayOfLastMenses': value.get('FirstDayOfLastMenses', ''),
45
                              'MenstrualCycleType': value.get('MenstrualCycleType', ''),
46
                              'Pregnant': bool(value.get('Pregnant', False)),
47
                              'MonthOfPregnancy': value.get('MonthOfPregnancy', '')})
48
49
        # Save patient's MenstrualStatus
50
        if 'PatientID' in form:
51
            patient = self.aq_parent.Schema()['Patient'].get(self.aq_parent)
52
            if patient:
53
                patient.Schema()['MenstrualStatus'].set(patient, [{
54
                  'Hysterectomy': bool(values[0].get('Hysterectomy', False)),
55
                  'HysterectomyYear': values[0].get('HysterectomyYear', ''),
56
                  'OvariesRemoved': bool(values[0].get('OvariesRemoved', False)),
57
                  'OvariesRemovedNum': int(value.get('OvariesRemovedNum', 0)),
0 ignored issues
show
The variable value does not seem to be defined in case the for loop on line 43 is not entered. Are you sure this can never be the case?
Loading history...
58
                  'OvariesRemovedYear': values[0].get('OvariesRemovedYear', '')
59
                  }]
60
                )
61
62
        return outvalues, {}
63
64
    def jsondumps(self, val):
65
        return json.dumps(val)
66
67
    def getMenstrualStatusesList(self):
68
        statuses = {}
69
        for key in MENSTRUAL_STATUSES.keys():
70
            statuses[key] = MENSTRUAL_STATUSES.getValue(key)
71
        return statuses
72
73
    def getPatientsGender(self):
74
        gender = 'dk'
75
        patient = self.aq_parent.Schema()['Patient'].get(self.aq_parent)
76
        if patient:
77
            gender = patient.Schema()['Gender'].get(patient)
78
        return gender
79
80
    def getMenstrualStatus(self):
81
82
        statuses = {'FirstDayOfLastMenses':'',
83
                     'MenstrualCycleType': MENSTRUAL_STATUSES.keys()[0],
84
                     'Pregnant':False,
85
                     'MonthOfPregnancy':0,
86
                     'Hysterectomy': False,
87
                     'HysterectomyYear': '',
88
                     'OvariesRemoved': False,
89
                     'OvariesRemovedNum': 0,
90
                     'OvariesRemovedYear': ''}
91
92
        # Fill with patient's Menstrual status info
93
        patient = self.aq_parent.Schema()['Patient'].get(self.aq_parent)
94
        if patient:
95
            pms = self.aq_parent.Schema()['MenstrualStatus'].get(self.aq_parent)
96
            if pms:
97
                statuses = dict(statuses.items() + pms[0].items())
98
99
        cms = self.aq_parent.Schema()['MenstrualStatus'].get(self.aq_parent)
100
        if cms:
101
            statuses = dict(statuses.items() + cms[0].items())
102
103
        return [statuses]
104
105
registerWidget(CaseMenstrualStatusWidget,
106
               title='CaseMenstrualStatusWidget',
107
               description='Menstrual status information',
108
               )
109