1 """GNUmed waiting list widgets."""
2
3 __author__ = 'karsten.hilbert@gmx.net'
4 __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
5
6
7 import logging
8 import sys
9
10
11
12 import wx
13
14
15 if __name__ == '__main__':
16 sys.path.insert(0, '../../')
17
18 from Gnumed.pycommon import gmDispatcher
19 from Gnumed.pycommon import gmTools
20 from Gnumed.pycommon import gmMatchProvider
21 from Gnumed.pycommon import gmI18N
22 from Gnumed.pycommon import gmExceptions
23 from Gnumed.pycommon import gmDateTime
24
25 from Gnumed.business import gmPraxis
26 from Gnumed.business import gmPerson
27
28 from Gnumed.wxpython import gmEditArea
29 from Gnumed.wxpython import gmPhraseWheel
30 from Gnumed.wxpython import gmRegetMixin
31 from Gnumed.wxpython import gmPatSearchWidgets
32 from Gnumed.wxpython import gmGuiHelpers
33
34
35 _log = logging.getLogger('gm.ui')
36
37
38
40
49
50
52 self.matcher.set_items([ {'data': i, 'list_label': i, 'field_label': i, 'weight': 1} for i in items ])
53
54
55 -def edit_waiting_list_entry(parent=None, entry=None, patient=None):
56 if parent is None:
57 parent = wx.GetApp().GetTopWindow()
58 ea = cWaitingListEntryEditAreaPnl(parent = parent, id = -1, patient = gmTools.bool2subst((entry is None), patient, None))
59 ea.data = entry
60 ea.mode = gmTools.coalesce(entry, 'new', 'edit')
61 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = True)
62 dlg.SetTitle(gmTools.coalesce(entry, _('Adding new waiting list entry'), _('Editing waiting list entry')))
63 if dlg.ShowModal() == wx.ID_OK:
64 dlg.Destroy()
65 return True
66 dlg.Destroy()
67 return False
68
69
70 from Gnumed.wxGladeWidgets import wxgWaitingListEntryEditAreaPnl
71
72 -class cWaitingListEntryEditAreaPnl(wxgWaitingListEntryEditAreaPnl.wxgWaitingListEntryEditAreaPnl, gmEditArea.cGenericEditAreaMixin):
73
74 - def __init__ (self, *args, **kwargs):
75
76 try:
77 self.patient = kwargs['patient']
78 del kwargs['patient']
79 except KeyError:
80 self.patient = None
81
82 try:
83 data = kwargs['entry']
84 del kwargs['entry']
85 except KeyError:
86 data = None
87
88 wxgWaitingListEntryEditAreaPnl.wxgWaitingListEntryEditAreaPnl.__init__(self, *args, **kwargs)
89 gmEditArea.cGenericEditAreaMixin.__init__(self)
90
91 if data is None:
92 self.mode = 'new'
93 else:
94 self.data = data
95 self.mode = 'edit'
96
97 praxis = gmPraxis.gmCurrentPraxisBranch()
98 pats = praxis.waiting_list_patients
99 zones = {}
100 zones.update([ [p['waiting_zone'], None] for p in pats if p['waiting_zone'] is not None ])
101 self._PRW_zone.update_matcher(items = zones.keys())
102
103
104
105 - def _refresh_as_new(self):
106 if self.patient is None:
107 self._PRW_patient.person = None
108 self._PRW_patient.Enable(True)
109 self._PRW_patient.SetFocus()
110 else:
111 self._PRW_patient.person = self.patient
112 self._PRW_patient.Enable(False)
113 self._TCTRL_comment.SetFocus()
114 self._PRW_patient._display_name()
115
116 self._TCTRL_comment.SetValue(u'')
117 self._PRW_zone.SetValue(u'')
118 self._SPCTRL_urgency.SetValue(0)
119
121 self._PRW_patient.person = gmPerson.cIdentity(aPK_obj = self.data['pk_identity'])
122 self._PRW_patient.Enable(False)
123 self._PRW_patient._display_name()
124
125 self._TCTRL_comment.SetValue(gmTools.coalesce(self.data['comment'], u''))
126 self._PRW_zone.SetValue(gmTools.coalesce(self.data['waiting_zone'], u''))
127 self._SPCTRL_urgency.SetValue(self.data['urgency'])
128
129 self._TCTRL_comment.SetFocus()
130
131 - def _valid_for_save(self):
132 validity = True
133
134 self.display_tctrl_as_valid(tctrl = self._PRW_patient, valid = (self._PRW_patient.person is not None))
135 validity = (self._PRW_patient.person is not None)
136
137 if validity is False:
138 gmDispatcher.send(signal = 'statustext', msg = _('Cannot add to waiting list. Missing essential input.'))
139
140 return validity
141
142 - def _save_as_new(self):
143
144 self._PRW_patient.person.put_on_waiting_list (
145 urgency = self._SPCTRL_urgency.GetValue(),
146 comment = gmTools.none_if(self._TCTRL_comment.GetValue().strip(), u''),
147 zone = gmTools.none_if(self._PRW_zone.GetValue().strip(), u'')
148 )
149
150 self.data = {'pk_identity': self._PRW_patient.person.ID, 'comment': None, 'waiting_zone': None, 'urgency': 0}
151 return True
152
153 - def _save_as_update(self):
154 gmPraxis.gmCurrentPraxisBranch().update_in_waiting_list (
155 pk = self.data['pk_waiting_list'],
156 urgency = self._SPCTRL_urgency.GetValue(),
157 comment = self._TCTRL_comment.GetValue().strip(),
158 zone = self._PRW_zone.GetValue().strip()
159 )
160 return True
161
162 from Gnumed.wxGladeWidgets import wxgWaitingListPnl
163
164 -class cWaitingListPnl(wxgWaitingListPnl.wxgWaitingListPnl, gmRegetMixin.cRegetOnPaintMixin):
165
177
178
179
181 self._LCTRL_patients.set_columns ([
182 _('Zone'),
183 _('Urgency'),
184 _('Registered'),
185 _('Waiting time'),
186 _('Patient'),
187 _('Born'),
188 _('Comment')
189 ])
190 self._LCTRL_patients.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE_USEHEADER, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE])
191 self._LCTRL_patients.item_tooltip_callback = self._on_get_list_tooltip
192 self._PRW_zone.add_callback_on_selection(callback = self._on_zone_selected)
193 self._PRW_zone.add_callback_on_lose_focus(callback = self._on_zone_selected)
194
196 """
197 This gets called when a patient has been activated, but
198 only when the waiting list is actually in use (that is,
199 the plugin is loaded)
200 """
201 pat = gmPerson.gmCurrentPatient()
202 enc = pat.emr.active_encounter
203 if gmTools.coalesce(enc['reason_for_encounter'], u'').strip() != u'':
204 return
205 entries = pat.waiting_list_entries
206 if len(entries) == 0:
207 if self.__id_most_recently_activated_patient is None:
208 return
209 if self.__id_most_recently_activated_patient != pat.ID:
210 return
211 rfe = self.__comment_most_recently_activated_patient
212 else:
213 entry = entries[0]
214 if gmTools.coalesce(entry['comment'], u'').strip() == u'':
215 return
216 rfe = entry['comment'].strip()
217 enc['reason_for_encounter'] = rfe
218 enc.save()
219 self.__id_most_recently_activated_patient = None
220
259
261 gmDispatcher.connect(signal = u'clin.waiting_list_mod_db', receiver = self._on_waiting_list_modified)
262 gmDispatcher.connect(signal = u'post_patient_selection', receiver = self._on_post_patient_selection)
263
265 self.__id_most_recently_activated_patient = None
266 col, ascending = self._LCTRL_patients.GetSortState()
267
268 praxis = gmPraxis.gmCurrentPraxisBranch()
269 pats = praxis.waiting_list_patients
270
271
272 zones = {}
273 zones.update([ [p['waiting_zone'], None] for p in pats if p['waiting_zone'] is not None ])
274 self._PRW_zone.update_matcher(items = zones.keys())
275
276
277 self.__current_zone = self._PRW_zone.GetValue().strip()
278 if self.__current_zone == u'':
279 pats = [ p for p in pats ]
280 else:
281 pats = [ p for p in pats if p['waiting_zone'] == self.__current_zone ]
282
283
284 curr_pat = gmPerson.gmCurrentPatient()
285 if curr_pat.connected:
286 if self._CHBOX_active_patient_only.IsChecked():
287 pats = [ p for p in pats if p['pk_identity'] == curr_pat.ID ]
288
289 old_pks = [ d['pk_waiting_list'] for d in self._LCTRL_patients.get_selected_item_data() ]
290 self._LCTRL_patients.set_string_items (
291 [ [
292 gmTools.coalesce(p['waiting_zone'], u''),
293 p['urgency'],
294 gmDateTime.pydt_strftime(p['registered'], format='%Y %b %d %H:%M'),
295 gmDateTime.format_interval_medically(p['waiting_time']),
296 u'%s, %s (%s)' % (p['lastnames'], p['firstnames'], p['l10n_gender']),
297 gmTools.coalesce (
298 gmTools.coalesce (
299 p['dob'],
300 u'',
301 function_initial = ('strftime', '%d %b %Y')
302 ),
303 u'',
304 function_initial = ('decode', gmI18N.get_encoding())
305 ),
306 gmTools.coalesce(p['comment'], u'').split('\n')[0]
307 ] for p in pats ]
308 )
309 self._LCTRL_patients.set_column_widths()
310 self._LCTRL_patients.set_data(pats)
311 new_selections = []
312 new_pks = [ p['pk_waiting_list'] for p in pats ]
313 for old_pk in old_pks:
314 if old_pk in new_pks:
315 new_selections.append(new_pks.index(old_pk))
316 self._LCTRL_patients.selections = new_selections
317 self._LCTRL_patients.Refresh()
318 self._LCTRL_patients.SortListItems(col, ascending)
319
320 self._LBL_no_of_patients.SetLabel(_('(%s patients)') % len(pats))
321
322 if len(pats) == 0:
323 self._BTN_activate.Enable(False)
324 self._BTN_activateplus.Enable(False)
325 self._BTN_remove.Enable(False)
326 self._BTN_edit.Enable(False)
327 self._BTN_up.Enable(False)
328 self._BTN_down.Enable(False)
329 else:
330 self._BTN_activate.Enable(True)
331 self._BTN_activateplus.Enable(True)
332 self._BTN_remove.Enable(True)
333 self._BTN_edit.Enable(True)
334 if len(pats) > 1:
335 self._BTN_up.Enable(True)
336 self._BTN_down.Enable(True)
337
338
339
341 self.__id_most_recently_activated_patient = None
342 if self.__current_zone == self._PRW_zone.GetValue().strip():
343 return True
344 wx.CallAfter(self.__refresh_waiting_list)
345 return True
346
348 self.__id_most_recently_activated_patient = None
349 wx.CallAfter(self._schedule_data_reget)
350
351 - def _on_post_patient_selection(self, *args, **kwargs):
352 wx.CallAfter(self.__on_post_patient_selection)
353
355 self._CHBOX_active_patient_only.Enable()
356 self._check_RFE()
357 self._schedule_data_reget()
358
378
397
418
426
433
472
479
486
488 self.__refresh_waiting_list()
489
490
491
492
493
495 self.__refresh_waiting_list()
496 return True
497
498
499
500 if __name__ == '__main__':
501
502 if len(sys.argv) < 2:
503 sys.exit()
504
505 if sys.argv[1] != 'test':
506 sys.exit()
507
508 gmI18N.activate_locale()
509 gmI18N.install_domain()
510
511
512
513
514
515
516
517
518
519
520
521
522 app = wx.PyWidgetTester(size = (200, 40))
523 app.SetWidget(cWaitingListPnl, -1)
524 app.MainLoop()
525
526
527