Package Gnumed :: Package wxpython :: Module gmMedicationWidgets
[frames] | no frames]

Source Code for Module Gnumed.wxpython.gmMedicationWidgets

   1  """GNUmed medication/substances handling widgets. 
   2  """ 
   3  #================================================================ 
   4  __version__ = "$Revision: 1.33 $" 
   5  __author__ = "Karsten Hilbert <Karsten.Hilbert@gmx.net>" 
   6   
   7  import logging, sys, os.path, webbrowser, decimal 
   8   
   9   
  10  import wx, wx.grid 
  11   
  12   
  13  if __name__ == '__main__': 
  14          sys.path.insert(0, '../../') 
  15  from Gnumed.pycommon import gmDispatcher, gmCfg, gmShellAPI, gmTools, gmDateTime 
  16  from Gnumed.pycommon import gmMatchProvider, gmI18N, gmPrinting, gmCfg2 
  17  from Gnumed.business import gmPerson, gmATC, gmSurgery, gmMedication, gmForms 
  18  from Gnumed.wxpython import gmGuiHelpers, gmRegetMixin, gmAuthWidgets, gmEditArea, gmMacro 
  19  from Gnumed.wxpython import gmCfgWidgets, gmListWidgets, gmPhraseWheel, gmFormWidgets 
  20  from Gnumed.wxpython import gmAllergyWidgets 
  21   
  22   
  23  _log = logging.getLogger('gm.ui') 
  24  _log.info(__version__) 
  25   
  26  #============================================================ 
  27  # generic drug database access 
  28  #============================================================ 
29 -def configure_drug_data_source(parent=None):
30 gmCfgWidgets.configure_string_from_list_option ( 31 parent = parent, 32 message = _( 33 '\n' 34 'Please select the default drug data source from the list below.\n' 35 '\n' 36 'Note that to actually use it you need to have the database installed, too.' 37 ), 38 option = 'external.drug_data.default_source', 39 bias = 'user', 40 default_value = None, 41 choices = gmMedication.drug_data_source_interfaces.keys(), 42 columns = [_('Drug data source')], 43 data = gmMedication.drug_data_source_interfaces.keys(), 44 caption = _('Configuring default drug data source') 45 )
46 #============================================================
47 -def get_drug_database(parent = None):
48 dbcfg = gmCfg.cCfgSQL() 49 50 default_db = dbcfg.get2 ( 51 option = 'external.drug_data.default_source', 52 workplace = gmSurgery.gmCurrentPractice().active_workplace, 53 bias = 'workplace' 54 ) 55 56 if default_db is None: 57 gmDispatcher.send('statustext', msg = _('No default drug database configured.'), beep = True) 58 configure_drug_data_source(parent = parent) 59 default_db = dbcfg.get2 ( 60 option = 'external.drug_data.default_source', 61 workplace = gmSurgery.gmCurrentPractice().active_workplace, 62 bias = 'workplace' 63 ) 64 if default_db is None: 65 gmGuiHelpers.gm_show_error ( 66 aMessage = _('There is no default drug database configured.'), 67 aTitle = _('Jumping to drug database') 68 ) 69 return None 70 71 try: 72 drug_db = gmMedication.drug_data_source_interfaces[default_db]() 73 except KeyError: 74 _log.error('faulty default drug data source configuration: %s', default_db) 75 configure_drug_data_source(parent = parent) 76 default_db = dbcfg.get2 ( 77 option = 'external.drug_data.default_source', 78 workplace = gmSurgery.gmCurrentPractice().active_workplace, 79 bias = 'workplace' 80 ) 81 if default_db is None: 82 return None 83 84 pat = gmPerson.gmCurrentPatient() 85 if pat.connected: 86 drug_db.patient = pat 87 88 return drug_db
89 #============================================================
90 -def jump_to_drug_database():
91 dbcfg = gmCfg.cCfgSQL() 92 drug_db = get_drug_database() 93 if drug_db is None: 94 return 95 drug_db.switch_to_frontend(blocking = False)
96 97 #============================================================
98 -def jump_to_ifap(import_drugs=False):
99 100 dbcfg = gmCfg.cCfgSQL() 101 102 ifap_cmd = dbcfg.get2 ( 103 option = 'external.ifap-win.shell_command', 104 workplace = gmSurgery.gmCurrentPractice().active_workplace, 105 bias = 'workplace', 106 default = 'wine "C:\Ifapwin\WIAMDB.EXE"' 107 ) 108 found, binary = gmShellAPI.detect_external_binary(ifap_cmd) 109 if not found: 110 gmDispatcher.send('statustext', msg = _('Cannot call IFAP via [%s].') % ifap_cmd) 111 return False 112 ifap_cmd = binary 113 114 if import_drugs: 115 transfer_file = os.path.expanduser(dbcfg.get2 ( 116 option = 'external.ifap-win.transfer_file', 117 workplace = gmSurgery.gmCurrentPractice().active_workplace, 118 bias = 'workplace', 119 default = '~/.wine/drive_c/Ifapwin/ifap2gnumed.csv' 120 )) 121 # file must exist for Ifap to write into it 122 try: 123 f = open(transfer_file, 'w+b').close() 124 except IOError: 125 _log.exception('Cannot create IFAP <-> GNUmed transfer file [%s]', transfer_file) 126 gmDispatcher.send('statustext', msg = _('Cannot create IFAP <-> GNUmed transfer file [%s].') % transfer_file) 127 return False 128 129 wx.BeginBusyCursor() 130 gmShellAPI.run_command_in_shell(command = ifap_cmd, blocking = import_drugs) 131 wx.EndBusyCursor() 132 133 if import_drugs: 134 # COMMENT: this file must exist PRIOR to invoking IFAP 135 # COMMENT: or else IFAP will not write data into it ... 136 try: 137 csv_file = open(transfer_file, 'rb') # FIXME: encoding 138 except: 139 _log.exception('cannot access [%s]', fname) 140 csv_file = None 141 142 if csv_file is not None: 143 import csv 144 csv_lines = csv.DictReader ( 145 csv_file, 146 fieldnames = u'PZN Handelsname Form Abpackungsmenge Einheit Preis1 Hersteller Preis2 rezeptpflichtig Festbetrag Packungszahl Packungsgr\xf6\xdfe'.split(), 147 delimiter = ';' 148 ) 149 pat = gmPerson.gmCurrentPatient() 150 emr = pat.get_emr() 151 # dummy episode for now 152 epi = emr.add_episode(episode_name = _('Current medication')) 153 for line in csv_lines: 154 narr = u'%sx %s %s %s (\u2258 %s %s) von %s (%s)' % ( 155 line['Packungszahl'].strip(), 156 line['Handelsname'].strip(), 157 line['Form'].strip(), 158 line[u'Packungsgr\xf6\xdfe'].strip(), 159 line['Abpackungsmenge'].strip(), 160 line['Einheit'].strip(), 161 line['Hersteller'].strip(), 162 line['PZN'].strip() 163 ) 164 emr.add_clin_narrative(note = narr, soap_cat = 's', episode = epi) 165 csv_file.close() 166 167 return True
168 169 #============================================================ 170 # ATC related widgets 171 #============================================================ 172
173 -def browse_atc_reference(parent=None):
174 175 if parent is None: 176 parent = wx.GetApp().GetTopWindow() 177 #------------------------------------------------------------ 178 def refresh(lctrl): 179 atcs = gmATC.get_reference_atcs() 180 181 items = [ [ 182 a['atc'], 183 a['term'], 184 u'%s' % gmTools.coalesce(a['ddd'], u''), 185 gmTools.coalesce(a['unit'], u''), 186 gmTools.coalesce(a['administrative_route'], u''), 187 gmTools.coalesce(a['comment'], u''), 188 a['version'], 189 a['lang'] 190 ] for a in atcs ] 191 lctrl.set_string_items(items) 192 lctrl.set_data(atcs)
193 #------------------------------------------------------------ 194 gmListWidgets.get_choices_from_list ( 195 parent = parent, 196 msg = _('\nThe ATC codes as known to GNUmed.\n'), 197 caption = _('Showing ATC codes.'), 198 columns = [ u'ATC', _('Term'), u'DDD', _('Unit'), _(u'Route'), _('Comment'), _('Version'), _('Language') ], 199 single_selection = True, 200 refresh_callback = refresh 201 ) 202 203 #============================================================
204 -def update_atc_reference_data():
205 206 dlg = wx.FileDialog ( 207 parent = None, 208 message = _('Choose an ATC import config file'), 209 defaultDir = os.path.expanduser(os.path.join('~', 'gnumed')), 210 defaultFile = '', 211 wildcard = "%s (*.conf)|*.conf|%s (*)|*" % (_('config files'), _('all files')), 212 style = wx.OPEN | wx.HIDE_READONLY | wx.FILE_MUST_EXIST 213 ) 214 215 result = dlg.ShowModal() 216 if result == wx.ID_CANCEL: 217 return 218 219 cfg_file = dlg.GetPath() 220 dlg.Destroy() 221 222 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('importing ATC reference data')) 223 if conn is None: 224 return False 225 226 wx.BeginBusyCursor() 227 228 if gmATC.atc_import(cfg_fname = cfg_file, conn = conn): 229 gmDispatcher.send(signal = 'statustext', msg = _('Successfully imported ATC reference data.')) 230 else: 231 gmDispatcher.send(signal = 'statustext', msg = _('Importing ATC reference data failed.'), beep = True) 232 233 wx.EndBusyCursor() 234 return True
235 236 #============================================================ 237
238 -class cATCPhraseWheel(gmPhraseWheel.cPhraseWheel):
239
240 - def __init__(self, *args, **kwargs):
241 242 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 243 244 query = u""" 245 246 SELECT DISTINCT ON (label) 247 atc_code, 248 label 249 FROM ( 250 251 SELECT 252 code as atc_code, 253 (code || ': ' || term || coalesce(' (' || ddd || unit || ')', '')) 254 AS label 255 FROM ref.atc 256 WHERE 257 term %(fragment_condition)s 258 OR 259 code %(fragment_condition)s 260 261 UNION ALL 262 263 SELECT 264 atc_code, 265 (atc_code || ': ' || description) 266 AS label 267 FROM ref.consumable_substance 268 WHERE 269 description %(fragment_condition)s 270 OR 271 atc_code %(fragment_condition)s 272 273 UNION ALL 274 275 SELECT 276 atc_code, 277 (atc_code || ': ' || description || ' (' || preparation || ')') 278 AS label 279 FROM ref.branded_drug 280 WHERE 281 description %(fragment_condition)s 282 OR 283 atc_code %(fragment_condition)s 284 285 -- it would be nice to be able to include clin.vacc_indication but that's hard to do in SQL 286 287 ) AS candidates 288 WHERE atc_code IS NOT NULL 289 ORDER BY label 290 LIMIT 50""" 291 292 mp = gmMatchProvider.cMatchProvider_SQL2(queries = query) 293 mp.setThresholds(1, 2, 4) 294 # mp.word_separators = '[ \t=+&:@]+' 295 self.SetToolTipString(_('Select an ATC (Anatomical-Therapeutic-Chemical) code.')) 296 self.matcher = mp 297 self.selection_only = True
298 299 #============================================================ 300 # consumable substances widgets 301 #------------------------------------------------------------
302 -def manage_consumable_substances(parent=None):
303 304 if parent is None: 305 parent = wx.GetApp().GetTopWindow() 306 #------------------------------------------------------------ 307 def add_from_db(substance): 308 drug_db = get_drug_database(parent = parent) 309 if drug_db is None: 310 return False 311 drug_db.import_drugs() 312 return True
313 #------------------------------------------------------------ 314 def edit(substance=None): 315 return edit_consumable_substance(parent = parent, substance = substance, single_entry = (substance is not None)) 316 #------------------------------------------------------------ 317 def delete(substance): 318 if substance.is_in_use_by_patients: 319 gmDispatcher.send(signal = 'statustext', msg = _('Cannot delete this substance. It is in use.'), beep = True) 320 return False 321 322 return gmMedication.delete_consumable_substance(substance = substance['pk']) 323 #------------------------------------------------------------ 324 def refresh(lctrl): 325 substs = gmMedication.get_consumable_substances(order_by = 'description') 326 items = [ [ 327 s['description'], 328 s['amount'], 329 s['unit'], 330 gmTools.coalesce(s['atc_code'], u''), 331 s['pk'] 332 ] for s in substs ] 333 lctrl.set_string_items(items) 334 lctrl.set_data(substs) 335 #------------------------------------------------------------ 336 msg = _('\nThese are the consumable substances registered with GNUmed.\n') 337 338 gmListWidgets.get_choices_from_list ( 339 parent = parent, 340 msg = msg, 341 caption = _('Showing consumable substances.'), 342 columns = [_('Substance'), _('Amount'), _('Unit'), 'ATC', u'#'], 343 single_selection = True, 344 new_callback = edit, 345 edit_callback = edit, 346 delete_callback = delete, 347 refresh_callback = refresh, 348 left_extra_button = (_('Import'), _('Import consumable substances from a drug database.'), add_from_db) 349 ) 350 351 #------------------------------------------------------------
352 -def edit_consumable_substance(parent=None, substance=None, single_entry=False):
353 354 if substance is not None: 355 if substance.is_in_use_by_patients: 356 gmDispatcher.send(signal = 'statustext', msg = _('Cannot edit this substance. It is in use.'), beep = True) 357 return False 358 359 ea = cConsumableSubstanceEAPnl(parent = parent, id = -1) 360 ea.data = substance 361 ea.mode = gmTools.coalesce(substance, 'new', 'edit') 362 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry) 363 dlg.SetTitle(gmTools.coalesce(substance, _('Adding new consumable substance'), _('Editing consumable substance'))) 364 if dlg.ShowModal() == wx.ID_OK: 365 dlg.Destroy() 366 return True 367 dlg.Destroy() 368 return False
369 370 #============================================================ 371 from Gnumed.wxGladeWidgets import wxgConsumableSubstanceEAPnl 372
373 -class cConsumableSubstanceEAPnl(wxgConsumableSubstanceEAPnl.wxgConsumableSubstanceEAPnl, gmEditArea.cGenericEditAreaMixin):
374
375 - def __init__(self, *args, **kwargs):
376 377 try: 378 data = kwargs['substance'] 379 del kwargs['substance'] 380 except KeyError: 381 data = None 382 383 wxgConsumableSubstanceEAPnl.wxgConsumableSubstanceEAPnl.__init__(self, *args, **kwargs) 384 gmEditArea.cGenericEditAreaMixin.__init__(self) 385 386 # Code using this mixin should set mode and data 387 # after instantiating the class: 388 self.mode = 'new' 389 self.data = data 390 if data is not None: 391 self.mode = 'edit'
392 393 # self.__init_ui() 394 #---------------------------------------------------------------- 395 # def __init_ui(self): 396 # self._PRW_atc.selection_only = False 397 #---------------------------------------------------------------- 398 # generic Edit Area mixin API 399 #----------------------------------------------------------------
400 - def _valid_for_save(self):
401 402 validity = True 403 404 if self._TCTRL_substance.GetValue().strip() == u'': 405 validity = False 406 self.display_tctrl_as_valid(tctrl = self._TCTRL_substance, valid = False) 407 self._TCTRL_substance.SetFocus() 408 else: 409 self.display_tctrl_as_valid(tctrl = self._TCTRL_substance, valid = True) 410 411 try: 412 decimal.Decimal(self._TCTRL_amount.GetValue().strip().replace(',', '.')) 413 self.display_tctrl_as_valid(tctrl = self._TCTRL_amount, valid = True) 414 except (TypeError, decimal.InvalidOperation): 415 validity = False 416 self.display_tctrl_as_valid(tctrl = self._TCTRL_amount, valid = False) 417 self._TCTRL_amount.SetFocus() 418 419 if self._PRW_unit.GetValue().strip() == u'': 420 validity = False 421 self._PRW_unit.display_as_valid(valid = False) 422 self._TCTRL_substance.SetFocus() 423 else: 424 self._PRW_unit.display_as_valid(valid = True) 425 426 if validity is False: 427 gmDispatcher.send(signal = 'statustext', msg = _('Cannot save consumable substance. Missing essential input.')) 428 429 return validity
430 #----------------------------------------------------------------
431 - def _save_as_new(self):
432 subst = gmMedication.create_consumable_substance ( 433 substance = self._TCTRL_substance.GetValue().strip(), 434 atc = self._PRW_atc.GetData(), 435 amount = decimal.Decimal(self._TCTRL_amount.GetValue().strip().replace(',', '.')), 436 unit = gmTools.coalesce(self._PRW_unit.GetData(), self._PRW_unit.GetValue().strip(), function_initial = ('strip', None)) 437 ) 438 success, data = subst.save() 439 if not success: 440 err, msg = data 441 _log.error(err) 442 _log.error(msg) 443 gmDispatcher.send(signal = 'statustext', msg = _('Cannot save consumable substance. %s') % msg, beep = True) 444 return False 445 446 self.data = subst 447 return True
448 #----------------------------------------------------------------
449 - def _save_as_update(self):
450 self.data['description'] = self._TCTRL_substance.GetValue().strip() 451 self.data['atc_code'] = self._PRW_atc.GetData() 452 self.data['amount'] = decimal.Decimal(self._TCTRL_amount.GetValue().strip().replace(',', '.')) 453 self.data['unit'] = gmTools.coalesce(self._PRW_unit.GetData(), self._PRW_unit.GetValue().strip(), function_initial = ('strip', None)) 454 success, data = self.data.save() 455 456 if not success: 457 err, msg = data 458 _log.error(err) 459 _log.error(msg) 460 gmDispatcher.send(signal = 'statustext', msg = _('Cannot save consumable substance. %s') % msg, beep = True) 461 return False 462 463 return True
464 #----------------------------------------------------------------
465 - def _refresh_as_new(self):
466 self._TCTRL_substance.SetValue(u'') 467 self._TCTRL_amount.SetValue(u'') 468 self._PRW_unit.SetText(u'', None) 469 self._PRW_atc.SetText(u'', None) 470 471 self._TCTRL_substance.SetFocus()
472 #----------------------------------------------------------------
473 - def _refresh_from_existing(self):
474 self._TCTRL_substance.SetValue(self.data['description']) 475 self._TCTRL_amount.SetValue(u'%s' % self.data['amount']) 476 self._PRW_unit.SetText(self.data['unit'], self.data['unit']) 477 self._PRW_atc.SetText(gmTools.coalesce(self.data['atc_code'], u''), self.data['atc_code']) 478 479 self._TCTRL_substance.SetFocus()
480 #----------------------------------------------------------------
482 self._refresh_as_new()
483 484 #============================================================ 485 # drug component widgets 486 #------------------------------------------------------------
487 -def manage_drug_components(parent=None):
488 489 if parent is None: 490 parent = wx.GetApp().GetTopWindow() 491 492 #------------------------------------------------------------ 493 def edit(component=None): 494 substance = gmMedication.cConsumableSubstance(aPK_obj = component['pk_consumable_substance']) 495 return edit_consumable_substance(parent = parent, substance = substance, single_entry = True)
496 #------------------------------------------------------------ 497 def delete(component): 498 if component.is_in_use_by_patients: 499 gmDispatcher.send(signal = 'statustext', msg = _('Cannot remove this component from the drug. It is in use.'), beep = True) 500 return False 501 502 return component.containing_drug.remove_component(substance = component['pk_component']) 503 #------------------------------------------------------------ 504 def refresh(lctrl): 505 comps = gmMedication.get_drug_components() 506 items = [ [ 507 u'%s%s' % (c['brand'], gmTools.coalesce(c['atc_brand'], u'', u' [%s]')), 508 u'%s%s' % (c['substance'], gmTools.coalesce(c['atc_substance'], u'', u' [%s]')), 509 u'%s%s' % (c['amount'], c['unit']), 510 c['preparation'], 511 gmTools.coalesce(c['external_code_brand'], u'', u'%%s [%s]' % c['external_code_type_brand']), 512 c['pk_component'] 513 ] for c in comps ] 514 lctrl.set_string_items(items) 515 lctrl.set_data(comps) 516 #------------------------------------------------------------ 517 msg = _('\nThese are the components in the drug brands known to GNUmed.\n') 518 519 gmListWidgets.get_choices_from_list ( 520 parent = parent, 521 msg = msg, 522 caption = _('Showing drug brand components.'), 523 columns = [_('Brand'), _('Substance'), _('Strength'), _('Preparation'), _('Code'), u'#'], 524 single_selection = True, 525 #new_callback = edit, 526 edit_callback = edit, 527 delete_callback = delete, 528 refresh_callback = refresh 529 ) 530 531 #------------------------------------------------------------
532 -def edit_drug_component(parent=None, drug_component=None, single_entry=False):
533 ea = cDrugComponentEAPnl(parent = parent, id = -1) 534 ea.data = drug_component 535 ea.mode = gmTools.coalesce(drug_component, 'new', 'edit') 536 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry) 537 dlg.SetTitle(gmTools.coalesce(drug_component, _('Adding new drug component'), _('Editing drug component'))) 538 if dlg.ShowModal() == wx.ID_OK: 539 dlg.Destroy() 540 return True 541 dlg.Destroy() 542 return False
543 544 #============================================================ 545 from Gnumed.wxGladeWidgets import wxgDrugComponentEAPnl 546
547 -class cDrugComponentEAPnl(wxgDrugComponentEAPnl.wxgDrugComponentEAPnl, gmEditArea.cGenericEditAreaMixin):
548
549 - def __init__(self, *args, **kwargs):
550 551 try: 552 data = kwargs['component'] 553 del kwargs['component'] 554 except KeyError: 555 data = None 556 557 wxgDrugComponentEAPnl.wxgDrugComponentEAPnl.__init__(self, *args, **kwargs) 558 gmEditArea.cGenericEditAreaMixin.__init__(self) 559 560 # Code using this mixin should set mode and data 561 # after instantiating the class: 562 self.mode = 'new' 563 self.data = data 564 if data is not None: 565 self.mode = 'edit'
566 567 #self.__init_ui() 568 #---------------------------------------------------------------- 569 # def __init_ui(self): 570 # # adjust phrasewheels etc 571 #---------------------------------------------------------------- 572 # generic Edit Area mixin API 573 #----------------------------------------------------------------
574 - def _valid_for_save(self):
575 if self.data is not None: 576 if self.data['is_in_use']: 577 gmDispatcher.send(signal = 'statustext', msg = _('Cannot edit drug component. It is in use.'), beep = True) 578 return False 579 580 validity = True 581 582 if self._PRW_substance.GetData() is None: 583 validity = False 584 self._PRW_substance.display_as_valid(False) 585 else: 586 self._PRW_substance.display_as_valid(True) 587 588 val = self._TCTRL_amount.GetValue().strip().replace(',', u'.', 1) 589 try: 590 decimal.Decimal(val) 591 self.display_tctrl_as_valid(tctrl = self._TCTRL_amount, valid = True) 592 except: 593 validity = False 594 self.display_tctrl_as_valid(tctrl = self._TCTRL_amount, valid = False) 595 596 if self._PRW_unit.GetValue().strip() == u'': 597 validity = False 598 self._PRW_unit.display_as_valid(False) 599 else: 600 self._PRW_unit.display_as_valid(True) 601 602 if validity is False: 603 gmDispatcher.send(signal = 'statustext', msg = _('Cannot save drug component. Invalid or missing essential input.')) 604 605 return validity
606 #----------------------------------------------------------------
607 - def _save_as_new(self):
608 # save the data as a new instance 609 data = 1 610 data[''] = 1 611 data[''] = 1 612 # data.save() 613 614 # must be done very late or else the property access 615 # will refresh the display such that later field 616 # access will return empty values 617 # self.data = data 618 return False 619 return True
620 #----------------------------------------------------------------
621 - def _save_as_update(self):
622 self.data['pk_consumable_substance'] = self._PRW_substance.GetData() 623 self.data['amount'] = decimal.Decimal(self._TCTRL_amount.GetValue().strip().replace(',', u'.', 1)) 624 self.data['unit'] = self._PRW_unit.GetValue().strip() 625 return self.data.save()
626 #----------------------------------------------------------------
627 - def _refresh_as_new(self):
628 self._TCTRL_brand.SetValue(u'') 629 self._TCTRL_components.SetValue(u'') 630 self._TCTRL_codes.SetValue(u'') 631 self._PRW_substance.SetText(u'', None) 632 self._TCTRL_amount.SetValue(u'') 633 self._PRW_unit.SetText(u'', None) 634 635 self._PRW_substance.SetFocus()
636 #----------------------------------------------------------------
637 - def _refresh_from_existing(self):
638 self._TCTRL_brand.SetValue(u'%s (%s)' % (self.data['brand'], self.data['preparation'])) 639 self._TCTRL_components.SetValue(u' / '.join(self.data.containing_drug['components'])) 640 details = [] 641 if self.data['atc_brand'] is not None: 642 details.append(u'ATC: %s' % self.data['atc_brand']) 643 if self.data['external_code_brand'] is not None: 644 details.append(u'%s: %s' % (self.data['external_code_type_brand'], self.data['external_code_brand'])) 645 self._TCTRL_codes.SetValue(u'; '.join(details)) 646 647 self._PRW_substance.SetText(self.data['substance'], self.data['pk_consumable_substance']) 648 self._TCTRL_amount.SetValue(u'%s' % self.data['amount']) 649 self._PRW_unit.SetText(self.data['unit'], self.data['unit']) 650 651 self._PRW_substance.SetFocus()
652 #----------------------------------------------------------------
654 #self._PRW_brand.SetText(u'', None) 655 #self._TCTRL_prep.SetValue(u'') 656 #self._TCTRL_brand_details.SetValue(u'') 657 self._PRW_substance.SetText(u'', None) 658 self._TCTRL_amount.SetValue(u'') 659 self._PRW_unit.SetText(u'', None) 660 661 self._PRW_substance.SetFocus()
662 663 #============================================================
664 -class cDrugComponentPhraseWheel(gmPhraseWheel.cPhraseWheel):
665
666 - def __init__(self, *args, **kwargs):
667 668 mp = gmMedication.cDrugComponentMatchProvider() 669 mp.setThresholds(2, 3, 4) 670 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 671 self.SetToolTipString(_('A drug component with optional strength.')) 672 self.matcher = mp 673 self.selection_only = False
674 #============================================================ 675 #============================================================
676 -class cSubstancePreparationPhraseWheel(gmPhraseWheel.cPhraseWheel):
677
678 - def __init__(self, *args, **kwargs):
679 680 query = u""" 681 ( 682 SELECT DISTINCT ON (preparation) 683 preparation as prep, preparation 684 FROM ref.branded_drug 685 WHERE preparation %(fragment_condition)s 686 ) UNION ( 687 SELECT DISTINCT ON (preparation) 688 preparation as prep, preparation 689 FROM clin.substance_intake 690 WHERE preparation %(fragment_condition)s 691 ) 692 ORDER BY prep 693 limit 30""" 694 695 mp = gmMatchProvider.cMatchProvider_SQL2(queries = query) 696 mp.setThresholds(1, 2, 4) 697 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 698 self.SetToolTipString(_('The preparation (form) of the substance or brand.')) 699 self.matcher = mp 700 self.selection_only = False
701 #============================================================
702 -class cSubstancePhraseWheel(gmPhraseWheel.cPhraseWheel):
703
704 - def __init__(self, *args, **kwargs):
705 706 mp = gmMedication.cSubstanceMatchProvider() 707 mp.setThresholds(1, 2, 4) 708 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 709 self.SetToolTipString(_('The substance with optional strength.')) 710 self.matcher = mp 711 self.selection_only = False 712 self.phrase_separators = None
713 #============================================================ 714 # branded drugs widgets 715 #------------------------------------------------------------
716 -def manage_components_of_branded_drug(parent=None, brand=None):
717 718 if brand is not None: 719 if brand['is_in_use']: 720 gmGuiHelpers.gm_show_info ( 721 aTitle = _('Managing components of a drug'), 722 aMessage = _( 723 'Cannot manage the components of the branded drug product\n' 724 '\n' 725 ' "%s" (%s)\n' 726 '\n' 727 'because it is currently taken by patients.\n' 728 ) % (brand['brand'], brand['preparation']) 729 ) 730 return False 731 #-------------------------------------------------------- 732 if parent is None: 733 parent = wx.GetApp().GetTopWindow() 734 #-------------------------------------------------------- 735 if brand is None: 736 msg = _('Pick the substances which are components of this drug.') 737 right_col = _('Components of drug') 738 comp_substs = [] 739 else: 740 right_col = u'%s (%s)' % (brand['brand'], brand['preparation']) 741 msg = _( 742 'Adjust the components of "%s"\n' 743 '\n' 744 'The drug must contain at least one component. Any given\n' 745 'substance can only be included once per drug.' 746 ) % right_col 747 comp_substs = [ c.substance for c in brand.components ] 748 749 substs = gmMedication.get_consumable_substances(order_by = 'description') 750 choices = [ u'%s %s%s' % (s['description'], s['amount'], s['unit']) for s in substs ] 751 picks = [ u'%s %s%s' % (c['description'], c['amount'], c['unit']) for c in comp_substs ] 752 753 picker = gmListWidgets.cItemPickerDlg ( 754 parent, 755 -1, 756 title = _('Managing components of a drug ...'), 757 msg = msg 758 ) 759 picker.set_columns(['Substances'], [right_col]) 760 picker.set_choices(choices = choices, data = substs) 761 picker.set_picks(picks = picks, data = comp_substs) 762 763 btn_pressed = picker.ShowModal() 764 substs = picker.get_picks() 765 picker.Destroy() 766 767 if btn_pressed != wx.ID_OK: 768 return (False, None) 769 770 if brand is not None: 771 brand.set_substances_as_components(substances = substs) 772 773 return (True, substs)
774 #------------------------------------------------------------
775 -def manage_branded_drugs(parent=None, ignore_OK_button=False):
776 777 if parent is None: 778 parent = wx.GetApp().GetTopWindow() 779 #------------------------------------------------------------ 780 def add_from_db(brand): 781 drug_db = get_drug_database(parent = parent) 782 if drug_db is None: 783 return False 784 drug_db.import_drugs() 785 return True
786 #------------------------------------------------------------ 787 def get_tooltip(brand=None): 788 tt = u'%s %s\n' % (brand['brand'], brand['preparation']) 789 tt += u'\n' 790 tt += u'%s%s%s\n' % ( 791 gmTools.bool2subst(brand.is_vaccine, u'%s, ' % _('Vaccine'), u''), 792 u'%s, ' % gmTools.bool2subst(brand['is_in_use'], _('in use'), _('not in use')), 793 gmTools.bool2subst(brand['is_fake_brand'], _('fake'), u'') 794 ) 795 tt += gmTools.coalesce(brand['atc'], u'', _('ATC: %s\n')) 796 tt += gmTools.coalesce(brand['external_code'], u'', u'%s: %%s\n' % brand['external_code_type']) 797 if brand['components'] is not None: 798 tt += u'- %s' % u'\n- '.join(brand['components']) 799 return tt 800 #------------------------------------------------------------ 801 def edit(brand=None): 802 if brand.is_vaccine: 803 gmGuiHelpers.gm_show_info ( 804 aTitle = _('Editing medication'), 805 aMessage = _( 806 'Cannot edit the medication\n' 807 '\n' 808 ' "%s" (%s)\n' 809 '\n' 810 'because it is a vaccine. Please edit it\n' 811 'from the vaccine management section !\n' 812 ) % (brand['brand'], brand['preparation']) 813 ) 814 return False 815 816 return edit_branded_drug(parent = parent, branded_drug = brand, single_entry = True) 817 #------------------------------------------------------------ 818 def delete(brand): 819 if brand.is_vaccine: 820 gmGuiHelpers.gm_show_info ( 821 aTitle = _('Deleting medication'), 822 aMessage = _( 823 'Cannot delete the medication\n' 824 '\n' 825 ' "%s" (%s)\n' 826 '\n' 827 'because it is a vaccine. Please delete it\n' 828 'from the vaccine management section !\n' 829 ) % (brand['brand'], brand['preparation']) 830 ) 831 return False 832 gmMedication.delete_branded_drug(brand = brand['pk_brand']) 833 return True 834 #------------------------------------------------------------ 835 def new(): 836 return edit_branded_drug(parent = parent, branded_drug = None, single_entry = False) 837 #------------------------------------------------------------ 838 def refresh(lctrl): 839 drugs = gmMedication.get_branded_drugs() 840 items = [ [ 841 u'%s%s' % ( 842 d['brand'], 843 gmTools.bool2subst(d['is_fake_brand'], ' (%s)' % _('fake'), u'') 844 ), 845 d['preparation'], 846 gmTools.coalesce(d['atc'], u''), 847 gmTools.coalesce(d['components'], u''), 848 gmTools.coalesce(d['external_code'], u'', u'%%s [%s]' % d['external_code_type']), 849 d['pk_brand'] 850 ] for d in drugs ] 851 lctrl.set_string_items(items) 852 lctrl.set_data(drugs) 853 #------------------------------------------------------------ 854 msg = _('\nThese are the drug brands known to GNUmed.\n') 855 856 gmListWidgets.get_choices_from_list ( 857 parent = parent, 858 msg = msg, 859 caption = _('Showing branded drugs.'), 860 columns = [_('Name'), _('Preparation'), _('ATC'), _('Components'), _('Code'), u'#'], 861 single_selection = True, 862 ignore_OK_button = ignore_OK_button, 863 refresh_callback = refresh, 864 new_callback = new, 865 edit_callback = edit, 866 delete_callback = delete, 867 list_tooltip_callback = get_tooltip, 868 left_extra_button = (_('Import'), _('Import substances and brands from a drug database.'), add_from_db) 869 #, middle_extra_button = (_('Clone'), _('Clone selected drug into a new entry for editing.'), clone_from_existing) 870 #, right_extra_button = (_('Reassign'), _('Reassign all patients taking the selected drug to another drug.'), reassign_patients) 871 ) 872 873 #------------------------------------------------------------
874 -def edit_branded_drug(parent=None, branded_drug=None, single_entry=False):
875 if branded_drug is not None: 876 if branded_drug['is_in_use']: 877 gmGuiHelpers.gm_show_info ( 878 aTitle = _('Editing drug'), 879 aMessage = _( 880 'Cannot edit the branded drug product\n' 881 '\n' 882 ' "%s" (%s)\n' 883 '\n' 884 'because it is currently taken by patients.\n' 885 ) % (branded_drug['brand'], branded_drug['preparation']) 886 ) 887 return False 888 889 ea = cBrandedDrugEAPnl(parent = parent, id = -1) 890 ea.data = branded_drug 891 ea.mode = gmTools.coalesce(branded_drug, 'new', 'edit') 892 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry) 893 dlg.SetTitle(gmTools.coalesce(branded_drug, _('Adding new drug brand'), _('Editing drug brand'))) 894 if dlg.ShowModal() == wx.ID_OK: 895 dlg.Destroy() 896 return True 897 dlg.Destroy() 898 return False
899 900 #============================================================ 901 from Gnumed.wxGladeWidgets import wxgBrandedDrugEAPnl 902
903 -class cBrandedDrugEAPnl(wxgBrandedDrugEAPnl.wxgBrandedDrugEAPnl, gmEditArea.cGenericEditAreaMixin):
904
905 - def __init__(self, *args, **kwargs):
906 907 try: 908 data = kwargs['drug'] 909 del kwargs['drug'] 910 except KeyError: 911 data = None 912 913 wxgBrandedDrugEAPnl.wxgBrandedDrugEAPnl.__init__(self, *args, **kwargs) 914 gmEditArea.cGenericEditAreaMixin.__init__(self) 915 916 self.mode = 'new' 917 self.data = data 918 if data is not None: 919 self.mode = 'edit' 920 self.__component_substances = data.components_as_substances
921 922 #self.__init_ui() 923 #---------------------------------------------------------------- 924 # def __init_ui(self): 925 # adjust external type PRW 926 #---------------------------------------------------------------- 927 # generic Edit Area mixin API 928 #----------------------------------------------------------------
929 - def _valid_for_save(self):
930 931 if self.data is not None: 932 if self.data['is_in_use']: 933 gmDispatcher.send(signal = 'statustext', msg = _('Cannot edit drug brand. It is in use.'), beep = True) 934 return False 935 936 validity = True 937 938 if self._PRW_brand.GetValue().strip() == u'': 939 validity = False 940 self._PRW_brand.display_as_valid(False) 941 else: 942 self._PRW_brand.display_as_valid(True) 943 944 if self._PRW_preparation.GetValue().strip() == u'': 945 validity = False 946 self._PRW_preparation.display_as_valid(False) 947 else: 948 self._PRW_preparation.display_as_valid(True) 949 950 if validity is False: 951 gmDispatcher.send(signal = 'statustext', msg = _('Cannot save branded drug. Invalid or missing essential input.')) 952 953 return validity
954 #----------------------------------------------------------------
955 - def _save_as_new(self):
956 957 drug = gmMedication.create_branded_drug ( 958 brand_name = self._PRW_brand.GetValue().strip(), 959 preparation = gmTools.coalesce ( 960 self._PRW_preparation.GetData(), 961 self._PRW_preparation.GetValue() 962 ).strip(), 963 return_existing = True 964 ) 965 drug['is_fake_brand'] = self._CHBOX_is_fake.GetValue() 966 drug['atc'] = self._PRW_atc.GetData() 967 code = self._TCTRL_external_code.GetValue().strip() 968 if code != u'': 969 drug['external_code'] = code 970 drug['external_code_type'] = self._PRW_external_code_type.GetData().strip() 971 972 drug.save() 973 974 if len(self.__component_substances) > 0: 975 drug.set_substances_as_components(substances = self.__component_substances) 976 977 self.data = drug 978 979 return True
980 #----------------------------------------------------------------
981 - def _save_as_update(self):
982 self.data['brand'] = self._PRW_brand.GetValue().strip() 983 self.data['preparation'] = gmTools.coalesce ( 984 self._PRW_preparation.GetData(), 985 self._PRW_preparation.GetValue() 986 ).strip() 987 self.data['is_fake_brand'] = self._CHBOX_is_fake.GetValue() 988 self.data['atc'] = self._PRW_atc.GetData() 989 code = self._TCTRL_external_code.GetValue().strip() 990 if code != u'': 991 self.data['external_code'] = code 992 self.data['external_code_type'] = self._PRW_external_code_type.GetData().strip() 993 success, data = self.data.save() 994 if not success: 995 err, msg = data 996 _log.error('problem saving') 997 _log.error('%s', err) 998 _log.error('%s', msg) 999 return (success is True)
1000 #----------------------------------------------------------------
1001 - def _refresh_as_new(self):
1002 self._PRW_brand.SetText(u'', None) 1003 self._PRW_preparation.SetText(u'', None) 1004 self._CHBOX_is_fake.SetValue(False) 1005 self._TCTRL_components.SetValue(u'') 1006 self._PRW_atc.SetText(u'', None) 1007 self._TCTRL_external_code.SetValue(u'') 1008 self._PRW_external_code_type.SetText(u'', None) 1009 1010 self._PRW_brand.SetFocus() 1011 1012 self.__component_substances = []
1013 #----------------------------------------------------------------
1015 self._refresh_as_new()
1016 #----------------------------------------------------------------
1017 - def _refresh_from_existing(self):
1018 self._PRW_brand.SetText(self.data['brand'], self.data['pk_brand']) 1019 self._PRW_preparation.SetText(self.data['preparation'], self.data['preparation']) 1020 self._CHBOX_is_fake.SetValue(self.data['is_fake_brand']) 1021 comps = u'' 1022 if self.data['components'] is not None: 1023 comps = u'- %s' % u'\n- '.join(self.data['components']) 1024 self._TCTRL_components.SetValue(comps) 1025 self._PRW_atc.SetText(gmTools.coalesce(self.data['atc'], u''), self.data['atc']) 1026 self._TCTRL_external_code.SetValue(gmTools.coalesce(self.data['external_code'], u'')) 1027 t = gmTools.coalesce(self.data['external_code_type'], u'') 1028 self._PRW_external_code_type.SetText(t, t) 1029 1030 self._PRW_brand.SetFocus() 1031 1032 self.__component_substances = self.data.components_as_substances
1033 #---------------------------------------------------------------- 1034 # event handler 1035 #----------------------------------------------------------------
1036 - def _on_manage_components_button_pressed(self, event):
1037 event.Skip() 1038 OKed, substs = manage_components_of_branded_drug(parent = self, brand = self.data) 1039 if OKed is True: 1040 self.__component_substances = substs 1041 comps = u'' 1042 if len(substs) > 0: 1043 comps = u'- %s' % u'\n- '.join([ u'%s %s%s' % (s['description'], s['amount'], s['unit']) for s in substs ]) 1044 self._TCTRL_components.SetValue(comps)
1045 #============================================================
1046 -class cBrandedDrugPhraseWheel(gmPhraseWheel.cPhraseWheel):
1047
1048 - def __init__(self, *args, **kwargs):
1049 1050 query = u""" 1051 SELECT 1052 pk, 1053 (description || ' (' || preparation || ')' || coalesce(' [' || atc_code || ']', '')) 1054 AS brand 1055 FROM ref.branded_drug 1056 WHERE description %(fragment_condition)s 1057 ORDER BY brand 1058 LIMIT 50""" 1059 1060 mp = gmMatchProvider.cMatchProvider_SQL2(queries = query) 1061 mp.setThresholds(2, 3, 4) 1062 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 1063 self.SetToolTipString(_('The brand name of the drug.')) 1064 self.matcher = mp 1065 self.selection_only = False
1066 1067 #============================================================ 1068 # current substance intake widgets 1069 #------------------------------------------------------------
1070 -class cSubstanceSchedulePhraseWheel(gmPhraseWheel.cPhraseWheel):
1071
1072 - def __init__(self, *args, **kwargs):
1073 1074 query = u""" 1075 SELECT DISTINCT ON (sched) 1076 schedule as sched, 1077 schedule 1078 FROM clin.substance_intake 1079 WHERE schedule %(fragment_condition)s 1080 ORDER BY sched 1081 LIMIT 50""" 1082 1083 mp = gmMatchProvider.cMatchProvider_SQL2(queries = query) 1084 mp.setThresholds(1, 2, 4) 1085 mp.word_separators = '[ \t=+&:@]+' 1086 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 1087 self.SetToolTipString(_('The schedule for taking this substance.')) 1088 self.matcher = mp 1089 self.selection_only = False
1090 #============================================================
1091 -def turn_substance_intake_into_allergy(parent=None, intake=None, emr=None):
1092 1093 if intake['is_currently_active']: 1094 intake['discontinued'] = gmDateTime.pydt_now_here() 1095 if intake['discontinue_reason'] is None: 1096 intake['discontinue_reason'] = u'%s %s' % (_('not tolerated:'), _('discontinued due to allergy or intolerance')) 1097 else: 1098 if not intake['discontinue_reason'].startswith(_('not tolerated:')): 1099 intake['discontinue_reason'] = u'%s %s' % (_('not tolerated:'), intake['discontinue_reason']) 1100 if not intake.save(): 1101 return False 1102 1103 allg = intake.turn_into_allergy(encounter_id = emr.active_encounter['pk_encounter']) 1104 1105 brand = intake.containing_drug 1106 if brand is not None: 1107 comps = [ c['substance'] for c in brand.components ] 1108 if len(comps) > 1: 1109 gmGuiHelpers.gm_show_info ( 1110 aTitle = _(u'Documented an allergy'), 1111 aMessage = _( 1112 u'An allergy was documented against the substance:\n' 1113 u'\n' 1114 u' [%s]\n' 1115 u'\n' 1116 u'This substance was taken with the multi-component brand:\n' 1117 u'\n' 1118 u' [%s (%s)]\n' 1119 u'\n' 1120 u'Note that ALL components of this brand were discontinued.' 1121 ) % ( 1122 intake['substance'], 1123 intake['brand'], 1124 u' & '.join(comps) 1125 ) 1126 ) 1127 1128 if parent is None: 1129 parent = wx.GetApp().GetTopWindow() 1130 1131 dlg = gmAllergyWidgets.cAllergyManagerDlg(parent = parent, id = -1) 1132 dlg.ShowModal() 1133 1134 return True
1135 #============================================================ 1136 from Gnumed.wxGladeWidgets import wxgCurrentMedicationEAPnl 1137
1138 -class cSubstanceIntakeEAPnl(wxgCurrentMedicationEAPnl.wxgCurrentMedicationEAPnl, gmEditArea.cGenericEditAreaMixin):
1139
1140 - def __init__(self, *args, **kwargs):
1141 1142 try: 1143 data = kwargs['substance'] 1144 del kwargs['substance'] 1145 except KeyError: 1146 data = None 1147 1148 wxgCurrentMedicationEAPnl.wxgCurrentMedicationEAPnl.__init__(self, *args, **kwargs) 1149 gmEditArea.cGenericEditAreaMixin.__init__(self) 1150 1151 self.mode = 'new' 1152 self.data = data 1153 if data is not None: 1154 self.mode = 'edit' 1155 1156 self.__init_ui()
1157 #----------------------------------------------------------------
1158 - def __init_ui(self):
1159 1160 self._PRW_component.add_callback_on_lose_focus(callback = self._on_leave_component) 1161 self._PRW_component.selection_only = True 1162 1163 self._PRW_substance.add_callback_on_lose_focus(callback = self._on_leave_substance) 1164 self._PRW_substance.selection_only = True
1165 #----------------------------------------------------------------
1166 - def __refresh_allergies(self):
1167 emr = gmPerson.gmCurrentPatient().get_emr() 1168 1169 state = emr.allergy_state 1170 if state['last_confirmed'] is None: 1171 confirmed = _('never') 1172 else: 1173 confirmed = state['last_confirmed'].strftime('%Y %B %d').decode(gmI18N.get_encoding()) 1174 msg = _(u'%s, last confirmed %s\n') % (state.state_string, confirmed) 1175 msg += gmTools.coalesce(state['comment'], u'', _('Comment (%s): %%s\n') % state['modified_by']) 1176 msg += u'\n' 1177 1178 for allergy in emr.get_allergies(): 1179 msg += u'%s (%s, %s): %s\n' % ( 1180 allergy['descriptor'], 1181 allergy['l10n_type'], 1182 gmTools.bool2subst(allergy['definite'], _('definite'), _('suspected'), u'?'), 1183 gmTools.coalesce(allergy['reaction'], _('reaction not recorded')) 1184 ) 1185 1186 self._LBL_allergies.SetLabel(msg)
1187 #---------------------------------------------------------------- 1188 # generic Edit Area mixin API 1189 #----------------------------------------------------------------
1190 - def _valid_for_save(self):
1191 1192 validity = True 1193 1194 has_component = (self._PRW_component.GetData() is not None) 1195 has_substance = (self._PRW_substance.GetValue().strip() != u'') 1196 1197 # must have either brand or substance 1198 if (has_component is False) and (has_substance is False): 1199 self._PRW_substance.display_as_valid(False) 1200 self._PRW_component.display_as_valid(False) 1201 validity = False 1202 else: 1203 self._PRW_substance.display_as_valid(True) 1204 self._PRW_component.display_as_valid(True) 1205 1206 # brands already have a preparation, so only required for substances 1207 if not has_component: 1208 if self._PRW_preparation.GetValue().strip() == u'': 1209 self._PRW_preparation.display_as_valid(False) 1210 validity = False 1211 else: 1212 self._PRW_preparation.display_as_valid(True) 1213 1214 # episode must be set if intake is to be approved of 1215 if self._CHBOX_approved.IsChecked(): 1216 if self._PRW_episode.GetValue().strip() == u'': 1217 self._PRW_episode.display_as_valid(False) 1218 validity = False 1219 else: 1220 self._PRW_episode.display_as_valid(True) 1221 1222 # # huh ? 1223 # if self._CHBOX_approved.IsChecked() is True: 1224 # self._PRW_duration.display_as_valid(True) 1225 # else: 1226 # if self._PRW_duration.GetValue().strip() in [u'', gmTools.u_infinity]: 1227 # self._PRW_duration.display_as_valid(True) 1228 # else: 1229 # if gmDateTime.str2interval(self._PRW_duration.GetValue()) is None: 1230 # self._PRW_duration.display_as_valid(False) 1231 # validity = False 1232 # else: 1233 # self._PRW_duration.display_as_valid(True) 1234 1235 # end must be > start if at all 1236 end = self._DP_discontinued.GetData() 1237 if end is not None: 1238 start = self._DP_started.GetData() 1239 if start > end: 1240 self._DP_started.display_as_valid(False) 1241 self._DP_discontinued.display_as_valid(False) 1242 validity = False 1243 else: 1244 self._DP_started.display_as_valid(True) 1245 self._DP_discontinued.display_as_valid(True) 1246 1247 if validity is False: 1248 gmDispatcher.send(signal = 'statustext', msg = _('Cannot save substance intake. Invalid or missing essential input.')) 1249 1250 return validity
1251 #----------------------------------------------------------------
1252 - def _save_as_new(self):
1253 1254 emr = gmPerson.gmCurrentPatient().get_emr() 1255 epi = self._PRW_episode.GetData(can_create = True) 1256 1257 if self._PRW_substance.GetData() is None: 1258 # auto-creates all components as intakes 1259 intake = emr.add_substance_intake ( 1260 pk_component = self._PRW_component.GetData(), 1261 episode = epi 1262 ) 1263 else: 1264 intake = emr.add_substance_intake ( 1265 pk_substance = self._PRW_substance.GetData(), 1266 episode = epi, 1267 preparation = self._PRW_preparation.GetValue().strip() 1268 ) 1269 1270 if intake is None: 1271 gmDispatcher.send('statustext', msg = _('Cannot add duplicate of (maybe inactive) substance intake.'), beep = True) 1272 return False 1273 1274 intake['started'] = self._DP_started.GetData() 1275 intake['discontinued'] = self._DP_discontinued.GetData() 1276 if intake['discontinued'] is None: 1277 intake['discontinue_reason'] = None 1278 else: 1279 intake['discontinue_reason'] = self._PRW_discontinue_reason.GetValue().strip() 1280 intake['schedule'] = self._PRW_schedule.GetValue().strip() 1281 intake['aim'] = self._PRW_aim.GetValue().strip() 1282 intake['notes'] = self._PRW_notes.GetValue().strip() 1283 intake['is_long_term'] = self._CHBOX_long_term.IsChecked() 1284 intake['intake_is_approved_of'] = self._CHBOX_approved.IsChecked() 1285 if self._PRW_duration.GetValue().strip() in [u'', gmTools.u_infinity]: 1286 intake['duration'] = None 1287 else: 1288 intake['duration'] = gmDateTime.str2interval(self._PRW_duration.GetValue()) 1289 intake.save() 1290 1291 self.data = intake 1292 1293 return True
1294 #----------------------------------------------------------------
1295 - def _save_as_update(self):
1296 1297 # auto-applies to all components of drug if any: 1298 self.data['started'] = self._DP_started.GetData() 1299 self.data['discontinued'] = self._DP_discontinued.GetData() 1300 if self.data['discontinued'] is None: 1301 self.data['discontinue_reason'] = None 1302 else: 1303 self.data['discontinue_reason'] = self._PRW_discontinue_reason.GetValue().strip() 1304 self.data['schedule'] = self._PRW_schedule.GetValue() 1305 self.data['is_long_term'] = self._CHBOX_long_term.IsChecked() 1306 self.data['intake_is_approved_of'] = self._CHBOX_approved.IsChecked() 1307 if self._PRW_duration.GetValue().strip() in [u'', gmTools.u_infinity]: 1308 self.data['duration'] = None 1309 else: 1310 self.data['duration'] = gmDateTime.str2interval(self._PRW_duration.GetValue()) 1311 1312 # applies to non-component substances only 1313 self.data['preparation'] = self._PRW_preparation.GetValue() 1314 1315 # per-component 1316 self.data['aim'] = self._PRW_aim.GetValue() 1317 self.data['notes'] = self._PRW_notes.GetValue() 1318 self.data['pk_episode'] = self._PRW_episode.GetData(can_create = True) 1319 1320 self.data.save() 1321 1322 return True
1323 #----------------------------------------------------------------
1324 - def _refresh_as_new(self):
1325 self._PRW_component.SetText(u'', None) 1326 self._TCTRL_brand_ingredients.SetValue(u'') 1327 self._TCTRL_brand_ingredients.SetToolTipString(u'') 1328 1329 self._PRW_substance.SetText(u'', None) 1330 self._PRW_substance.Enable(True) 1331 1332 self._PRW_preparation.SetText(u'', None) 1333 self._PRW_preparation.Enable(True) 1334 1335 self._PRW_schedule.SetText(u'', None) 1336 self._PRW_duration.SetText(u'', None) 1337 self._PRW_aim.SetText(u'', None) 1338 self._PRW_notes.SetText(u'', None) 1339 self._PRW_episode.SetText(u'', None) 1340 1341 self._CHBOX_long_term.SetValue(False) 1342 self._CHBOX_approved.SetValue(True) 1343 1344 self._DP_started.SetData(gmDateTime.pydt_now_here()) 1345 self._DP_discontinued.SetData(None) 1346 self._PRW_discontinue_reason.SetValue(u'') 1347 1348 self.__refresh_allergies() 1349 1350 self._PRW_component.SetFocus()
1351 #----------------------------------------------------------------
1352 - def _refresh_from_existing(self):
1353 1354 self._TCTRL_brand_ingredients.SetValue(u'') 1355 self._TCTRL_brand_ingredients.SetToolTipString(u'') 1356 1357 if self.data['pk_brand'] is None: 1358 self.__refresh_from_existing_substance() 1359 else: 1360 self.__refresh_from_existing_component() 1361 1362 self._PRW_component.Enable(False) 1363 self._PRW_substance.Enable(False) 1364 1365 if self.data['is_long_term']: 1366 self._CHBOX_long_term.SetValue(True) 1367 self._PRW_duration.Enable(False) 1368 self._PRW_duration.SetText(gmTools.u_infinity, None) 1369 self._BTN_discontinued_as_planned.Enable(False) 1370 else: 1371 self._CHBOX_long_term.SetValue(False) 1372 self._PRW_duration.Enable(True) 1373 self._BTN_discontinued_as_planned.Enable(True) 1374 if self.data['duration'] is None: 1375 self._PRW_duration.SetText(u'', None) 1376 else: 1377 self._PRW_duration.SetText(gmDateTime.format_interval(self.data['duration'], gmDateTime.acc_days), self.data['duration']) 1378 self._PRW_aim.SetText(gmTools.coalesce(self.data['aim'], u''), self.data['aim']) 1379 self._PRW_notes.SetText(gmTools.coalesce(self.data['notes'], u''), self.data['notes']) 1380 self._PRW_episode.SetData(self.data['pk_episode']) 1381 self._PRW_schedule.SetText(gmTools.coalesce(self.data['schedule'], u''), self.data['schedule']) 1382 1383 self._CHBOX_approved.SetValue(self.data['intake_is_approved_of']) 1384 1385 self._DP_started.SetData(self.data['started']) 1386 self._DP_discontinued.SetData(self.data['discontinued']) 1387 self._PRW_discontinue_reason.SetValue(gmTools.coalesce(self.data['discontinue_reason'], u'')) 1388 if self.data['discontinued'] is not None: 1389 self._PRW_discontinue_reason.Enable() 1390 1391 self.__refresh_allergies() 1392 1393 self._PRW_schedule.SetFocus()
1394 #----------------------------------------------------------------
1396 self._LBL_component.Enable(False) 1397 self._PRW_component.SetText(u'', None) 1398 self._PRW_component.display_as_valid(True) 1399 1400 self._PRW_substance.SetText ( 1401 u'%s %s%s' % (self.data['substance'], self.data['amount'], self.data['unit']), 1402 self.data['pk_substance'] 1403 ) 1404 1405 self._PRW_preparation.SetText(gmTools.coalesce(self.data['preparation'], u''), self.data['preparation']) 1406 self._PRW_preparation.Enable(True)
1407 #----------------------------------------------------------------
1409 self._PRW_component.SetText ( 1410 u'%s %s%s (%s)' % (self.data['substance'], self.data['amount'], self.data['unit'], self.data['brand']), 1411 self.data['pk_drug_component'] 1412 ) 1413 1414 brand = gmMedication.cBrandedDrug(aPK_obj = self.data['pk_brand']) 1415 if brand['components'] is not None: 1416 self._TCTRL_brand_ingredients.SetValue(u'; '.join(brand['components'])) 1417 tt = u'%s:\n\n- %s' % ( 1418 self.data['brand'], 1419 u'\n- '.join(brand['components']) 1420 ) 1421 self._TCTRL_brand_ingredients.SetToolTipString(tt) 1422 1423 self._LBL_or.Enable(False) 1424 self._LBL_substance.Enable(False) 1425 self._PRW_substance.SetText(u'', None) 1426 self._PRW_substance.display_as_valid(True) 1427 1428 self._PRW_preparation.SetText(self.data['preparation'], self.data['preparation']) 1429 self._PRW_preparation.Enable(False)
1430 #----------------------------------------------------------------
1432 self._refresh_as_new()
1433 #---------------------------------------------------------------- 1434 # event handlers 1435 #----------------------------------------------------------------
1436 - def _on_leave_component(self):
1437 if self._PRW_component.GetData() is None: 1438 self._LBL_or.Enable(True) 1439 self._PRW_component.SetText(u'', None) 1440 self._LBL_substance.Enable(True) 1441 self._PRW_substance.Enable(True) 1442 self._LBL_preparation.Enable(True) 1443 self._PRW_preparation.Enable(True) 1444 self._PRW_preparation.SetText(u'', None) 1445 self._TCTRL_brand_ingredients.SetValue(u'') 1446 self._TCTRL_brand_ingredients.SetToolTipString(u'') 1447 else: 1448 self._LBL_or.Enable(False) 1449 self._LBL_substance.Enable(False) 1450 self._PRW_substance.SetText(u'', None) 1451 self._PRW_substance.display_as_valid(True) 1452 self._PRW_substance.Enable(False) 1453 self._LBL_preparation.Enable(False) 1454 self._PRW_preparation.Enable(False) 1455 comp = gmMedication.cDrugComponent(aPK_obj = self._PRW_component.GetData()) 1456 self._PRW_preparation.SetText(comp['preparation'], comp['preparation']) 1457 brand = comp.containing_drug 1458 if brand['components'] is not None: 1459 self._TCTRL_brand_ingredients.SetValue(u'; '.join(brand['components'])) 1460 tt = u'%s:\n\n- %s' % ( 1461 self.data['brand'], 1462 u'\n- '.join(brand['components']) 1463 ) 1464 self._TCTRL_brand_ingredients.SetToolTipString(tt)
1465 #----------------------------------------------------------------
1466 - def _on_leave_substance(self):
1467 if self._PRW_substance.GetData() is None: 1468 self._LBL_or.Enable(True) 1469 self._LBL_component.Enable(True) 1470 self._PRW_component.Enable(True) 1471 self._PRW_substance.SetText(u'', None) 1472 else: 1473 self._LBL_or.Enable(False) 1474 self._LBL_component.Enable(False) 1475 self._PRW_component.SetText(u'', None) 1476 self._PRW_component.display_as_valid(True) 1477 self._PRW_component.Enable(False) 1478 self._LBL_preparation.Enable(True) 1479 self._PRW_preparation.Enable(True) 1480 self._TCTRL_brand_ingredients.SetValue(u'') 1481 self._TCTRL_brand_ingredients.SetToolTipString(u'')
1482 #----------------------------------------------------------------
1483 - def _on_discontinued_date_changed(self, event):
1484 if self._DP_discontinued.GetData() is None: 1485 self._PRW_discontinue_reason.Enable(False) 1486 else: 1487 self._PRW_discontinue_reason.Enable(True)
1488 #----------------------------------------------------------------
1489 - def _on_manage_brands_button_pressed(self, event):
1490 manage_branded_drugs(parent = self, ignore_OK_button = True)
1491 #----------------------------------------------------------------
1492 - def _on_manage_substances_button_pressed(self, event):
1493 manage_consumable_substances(parent = self)
1494 #----------------------------------------------------------------
1496 1497 now = gmDateTime.pydt_now_here() 1498 1499 self.__refresh_allergies() 1500 1501 if self.data is None: 1502 return 1503 1504 # do we have a (full) plan ? 1505 if None not in [self.data['started'], self.data['duration']]: 1506 planned_end = self.data['started'] + self.data['duration'] 1507 # the plan hasn't ended so [Per plan] can't apply ;-) 1508 if planned_end > now: 1509 return 1510 self._DP_discontinued.SetData(planned_end) 1511 self._PRW_discontinue_reason.Enable(True) 1512 self._PRW_discontinue_reason.SetValue(u'') 1513 return 1514 1515 # we know started but not duration: apparently the plan is to stop today 1516 if self.data['started'] is not None: 1517 # but we haven't started yet so we can't stop 1518 if self.data['started'] > now: 1519 return 1520 1521 self._DP_discontinued.SetData(now) 1522 self._PRW_discontinue_reason.Enable(True) 1523 self._PRW_discontinue_reason.SetValue(u'')
1524 #----------------------------------------------------------------
1525 - def _on_chbox_long_term_checked(self, event):
1526 if self._CHBOX_long_term.IsChecked() is True: 1527 self._PRW_duration.Enable(False) 1528 self._BTN_discontinued_as_planned.Enable(False) 1529 self._PRW_discontinue_reason.Enable(False) 1530 else: 1531 self._PRW_duration.Enable(True) 1532 self._BTN_discontinued_as_planned.Enable(True) 1533 self._PRW_discontinue_reason.Enable(True) 1534 1535 self.__refresh_allergies()
1536 #----------------------------------------------------------------
1537 - def turn_into_allergy(self, data=None):
1538 if not self.save(): 1539 return False 1540 1541 return turn_substance_intake_into_allergy ( 1542 parent = self, 1543 intake = self.data, 1544 emr = gmPerson.gmCurrentPatient().get_emr() 1545 )
1546 #============================================================
1547 -def delete_substance_intake(parent=None, substance=None):
1548 1549 subst = gmMedication.cSubstanceIntakeEntry(aPK_obj = substance) 1550 msg = _( 1551 '\n' 1552 '[%s]\n' 1553 '\n' 1554 'It may be prudent to edit (before deletion) the details\n' 1555 'of this substance intake entry so as to leave behind\n' 1556 'some indication of why it was deleted.\n' 1557 ) % subst.format() 1558 1559 dlg = gmGuiHelpers.c3ButtonQuestionDlg ( 1560 parent, 1561 -1, 1562 caption = _('Deleting medication / substance intake'), 1563 question = msg, 1564 button_defs = [ 1565 {'label': _('&Edit'), 'tooltip': _('Allow editing of substance intake entry before deletion.'), 'default': True}, 1566 {'label': _('&Delete'), 'tooltip': _('Delete immediately without editing first.')}, 1567 {'label': _('&Cancel'), 'tooltip': _('Abort. Do not delete or edit substance intake entry.')} 1568 ] 1569 ) 1570 1571 edit_first = dlg.ShowModal() 1572 dlg.Destroy() 1573 1574 if edit_first == wx.ID_CANCEL: 1575 return 1576 1577 if edit_first == wx.ID_YES: 1578 edit_intake_of_substance(parent = parent, substance = subst) 1579 delete_it = gmGuiHelpers.gm_show_question ( 1580 aMessage = _('Now delete substance intake entry ?'), 1581 aTitle = _('Deleting medication / substance intake') 1582 ) 1583 else: 1584 delete_it = True 1585 1586 if not delete_it: 1587 return 1588 1589 gmMedication.delete_substance_intake(substance = substance)
1590 #------------------------------------------------------------
1591 -def edit_intake_of_substance(parent = None, substance=None):
1592 ea = cSubstanceIntakeEAPnl(parent = parent, id = -1, substance = substance) 1593 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = (substance is not None)) 1594 dlg.SetTitle(gmTools.coalesce(substance, _('Adding medication/non-medication substance intake'), _('Editing medication/non-medication substance intake'))) 1595 dlg.left_extra_button = ( 1596 _('Allergy'), 1597 _('Document an allergy against this substance.'), 1598 ea.turn_into_allergy 1599 ) 1600 if dlg.ShowModal() == wx.ID_OK: 1601 dlg.Destroy() 1602 return True 1603 dlg.Destroy() 1604 return False
1605 1606 #============================================================ 1607 # current substances grid 1608 #------------------------------------------------------------
1609 -def configure_medication_list_template(parent=None):
1610 1611 if parent is None: 1612 parent = wx.GetApp().GetTopWindow() 1613 1614 template = gmFormWidgets.manage_form_templates ( 1615 parent = parent, 1616 template_types = ['current medication list'] 1617 ) 1618 option = u'form_templates.medication_list' 1619 1620 if template is None: 1621 gmDispatcher.send(signal = 'statustext', msg = _('No medication list template configured.'), beep = True) 1622 return None 1623 1624 if template['engine'] != u'L': 1625 gmDispatcher.send(signal = 'statustext', msg = _('No medication list template configured.'), beep = True) 1626 return None 1627 1628 dbcfg = gmCfg.cCfgSQL() 1629 dbcfg.set ( 1630 workplace = gmSurgery.gmCurrentPractice().active_workplace, 1631 option = option, 1632 value = u'%s - %s' % (template['name_long'], template['external_version']) 1633 ) 1634 1635 return template
1636 #------------------------------------------------------------ 1716 #------------------------------------------------------------
1717 -def update_substance_intake_list_from_prescription(parent=None, prescribed_drugs=None, emr=None):
1718 1719 if len(prescribed_drugs) == 0: 1720 return 1721 1722 curr_brands = [ i['pk_brand'] for i in emr.get_current_substance_intake() if i['pk_brand'] is not None ] 1723 new_drugs = [] 1724 for drug in prescribed_drugs: 1725 if drug['pk_brand'] not in curr_brands: 1726 new_drugs.append(drug) 1727 1728 if len(new_drugs) == 0: 1729 return 1730 1731 if parent is None: 1732 parent = wx.GetApp().GetTopWindow() 1733 1734 dlg = gmListWidgets.cItemPickerDlg ( 1735 parent, 1736 -1, 1737 msg = _( 1738 'These brands have been prescribed but are not listed\n' 1739 'in the current medication list of this patient.\n' 1740 '\n' 1741 'Please select those you want added to the medication list.' 1742 ) 1743 ) 1744 dlg.set_columns ( 1745 columns = [_('Newly prescribed drugs')], 1746 columns_right = [_('Add to medication list')] 1747 ) 1748 choices = [ (u'%s %s (%s)' % (d['brand'], d['preparation'], u'; '.join(d['components']))) for d in new_drugs ] 1749 dlg.set_choices ( 1750 choices = choices, 1751 data = new_drugs 1752 ) 1753 dlg.ShowModal() 1754 drugs2add = dlg.get_picks() 1755 dlg.Destroy() 1756 1757 if drugs2add is None: 1758 return 1759 1760 if len(drugs2add) == 0: 1761 return 1762 1763 for drug in drugs2add: 1764 # only add first component since all other components get added by a trigger ... 1765 intake = emr.add_substance_intake ( 1766 pk_component = drug['pk_components'][0], 1767 episode = emr.add_episode(episode_name = gmMedication.DEFAULT_MEDICATION_HISTORY_EPISODE)['pk_episode'], 1768 ) 1769 if intake is None: 1770 continue 1771 intake['intake_is_approved_of'] = True 1772 intake.save() 1773 1774 return
1775 #------------------------------------------------------------
1776 -class cCurrentSubstancesGrid(wx.grid.Grid):
1777 """A grid class for displaying current substance intake. 1778 1779 - does NOT listen to the currently active patient 1780 - thereby it can display any patient at any time 1781 """
1782 - def __init__(self, *args, **kwargs):
1783 1784 wx.grid.Grid.__init__(self, *args, **kwargs) 1785 1786 self.__patient = None 1787 self.__row_data = {} 1788 self.__prev_row = None 1789 self.__prev_tooltip_row = None 1790 self.__prev_cell_0 = None 1791 self.__grouping_mode = u'episode' 1792 self.__filter_show_unapproved = True 1793 self.__filter_show_inactive = False 1794 1795 self.__grouping2col_labels = { 1796 u'episode': [ 1797 _('Episode'), 1798 _('Substance'), 1799 _('Dose'), 1800 _('Schedule'), 1801 _('Started'), 1802 _('Duration / Until'), 1803 _('Brand'), 1804 _('Advice') 1805 ], 1806 u'brand': [ 1807 _('Brand'), 1808 _('Schedule'), 1809 _('Substance'), 1810 _('Dose'), 1811 _('Started'), 1812 _('Duration / Until'), 1813 _('Episode'), 1814 _('Advice') 1815 ] 1816 } 1817 1818 self.__grouping2order_by_clauses = { 1819 u'episode': u'pk_health_issue nulls first, episode, substance, started', 1820 u'brand': u'brand nulls last, substance, started' 1821 } 1822 1823 self.__init_ui() 1824 self.__register_events()
1825 #------------------------------------------------------------ 1826 # external API 1827 #------------------------------------------------------------
1828 - def get_selected_cells(self):
1829 1830 sel_block_top_left = self.GetSelectionBlockTopLeft() 1831 sel_block_bottom_right = self.GetSelectionBlockBottomRight() 1832 sel_cols = self.GetSelectedCols() 1833 sel_rows = self.GetSelectedRows() 1834 1835 selected_cells = [] 1836 1837 # individually selected cells (ctrl-click) 1838 selected_cells += self.GetSelectedCells() 1839 1840 # selected rows 1841 selected_cells += list ( 1842 (row, col) 1843 for row in sel_rows 1844 for col in xrange(self.GetNumberCols()) 1845 ) 1846 1847 # selected columns 1848 selected_cells += list ( 1849 (row, col) 1850 for row in xrange(self.GetNumberRows()) 1851 for col in sel_cols 1852 ) 1853 1854 # selection blocks 1855 for top_left, bottom_right in zip(self.GetSelectionBlockTopLeft(), self.GetSelectionBlockBottomRight()): 1856 selected_cells += [ 1857 (row, col) 1858 for row in xrange(top_left[0], bottom_right[0] + 1) 1859 for col in xrange(top_left[1], bottom_right[1] + 1) 1860 ] 1861 1862 return set(selected_cells)
1863 #------------------------------------------------------------
1864 - def get_selected_rows(self):
1865 rows = {} 1866 1867 for row, col in self.get_selected_cells(): 1868 rows[row] = True 1869 1870 return rows.keys()
1871 #------------------------------------------------------------
1872 - def get_selected_data(self):
1873 return [ self.__row_data[row] for row in self.get_selected_rows() ]
1874 #------------------------------------------------------------
1875 - def repopulate_grid(self):
1876 1877 self.empty_grid() 1878 1879 if self.__patient is None: 1880 return 1881 1882 emr = self.__patient.get_emr() 1883 meds = emr.get_current_substance_intake ( 1884 order_by = self.__grouping2order_by_clauses[self.__grouping_mode], 1885 include_unapproved = self.__filter_show_unapproved, 1886 include_inactive = self.__filter_show_inactive 1887 ) 1888 if not meds: 1889 return 1890 1891 self.BeginBatch() 1892 1893 # columns 1894 labels = self.__grouping2col_labels[self.__grouping_mode] 1895 if self.__filter_show_unapproved: 1896 self.AppendCols(numCols = len(labels) + 1) 1897 else: 1898 self.AppendCols(numCols = len(labels)) 1899 for col_idx in range(len(labels)): 1900 self.SetColLabelValue(col_idx, labels[col_idx]) 1901 if self.__filter_show_unapproved: 1902 self.SetColLabelValue(len(labels), u'OK?') 1903 self.SetColSize(len(labels), 40) 1904 1905 self.AppendRows(numRows = len(meds)) 1906 1907 # loop over data 1908 for row_idx in range(len(meds)): 1909 med = meds[row_idx] 1910 self.__row_data[row_idx] = med 1911 1912 if med['is_currently_active'] is True: 1913 atcs = [] 1914 if med['atc_substance'] is not None: 1915 atcs.append(med['atc_substance']) 1916 # if med['atc_brand'] is not None: 1917 # atcs.append(med['atc_brand']) 1918 # allg = emr.is_allergic_to(atcs = tuple(atcs), inns = (med['substance'],), brand = med['brand']) 1919 allg = emr.is_allergic_to(atcs = tuple(atcs), inns = (med['substance'],)) 1920 if allg not in [None, False]: 1921 attr = self.GetOrCreateCellAttr(row_idx, 0) 1922 if allg['type'] == u'allergy': 1923 attr.SetTextColour('red') 1924 else: 1925 attr.SetTextColour('yellow') 1926 self.SetRowAttr(row_idx, attr) 1927 else: 1928 attr = self.GetOrCreateCellAttr(row_idx, 0) 1929 attr.SetTextColour('grey') 1930 self.SetRowAttr(row_idx, attr) 1931 1932 if self.__grouping_mode == u'episode': 1933 if med['pk_episode'] is None: 1934 self.__prev_cell_0 = None 1935 epi = gmTools.u_diameter 1936 else: 1937 if self.__prev_cell_0 == med['episode']: 1938 epi = u'' 1939 else: 1940 self.__prev_cell_0 = med['episode'] 1941 epi = gmTools.coalesce(med['episode'], u'') 1942 self.SetCellValue(row_idx, 0, gmTools.wrap(text = epi, width = 40)) 1943 1944 self.SetCellValue(row_idx, 1, med['substance']) 1945 self.SetCellValue(row_idx, 2, u'%s%s' % (med['amount'], med['unit'])) 1946 self.SetCellValue(row_idx, 3, gmTools.coalesce(med['schedule'], u'')) 1947 self.SetCellValue(row_idx, 4, med['started'].strftime('%Y-%m-%d')) 1948 1949 if med['is_long_term']: 1950 self.SetCellValue(row_idx, 5, gmTools.u_infinity) 1951 else: 1952 if med['discontinued'] is None: 1953 if med['duration'] is None: 1954 self.SetCellValue(row_idx, 5, u'') 1955 else: 1956 self.SetCellValue(row_idx, 5, gmDateTime.format_interval(med['duration'], gmDateTime.acc_days)) 1957 else: 1958 self.SetCellValue(row_idx, 5, med['discontinued'].strftime('%Y-%m-%d')) 1959 1960 if med['pk_brand'] is None: 1961 brand = u'' 1962 else: 1963 if med['fake_brand']: 1964 brand = gmTools.coalesce(med['brand'], u'', _('%s (fake)')) 1965 else: 1966 brand = gmTools.coalesce(med['brand'], u'') 1967 self.SetCellValue(row_idx, 6, gmTools.wrap(text = brand, width = 35)) 1968 1969 elif self.__grouping_mode == u'brand': 1970 1971 if med['pk_brand'] is None: 1972 self.__prev_cell_0 = None 1973 brand = gmTools.u_diameter 1974 else: 1975 if self.__prev_cell_0 == med['brand']: 1976 brand = u'' 1977 else: 1978 self.__prev_cell_0 = med['brand'] 1979 if med['fake_brand']: 1980 brand = gmTools.coalesce(med['brand'], u'', _('%s (fake)')) 1981 else: 1982 brand = gmTools.coalesce(med['brand'], u'') 1983 self.SetCellValue(row_idx, 0, gmTools.wrap(text = brand, width = 35)) 1984 1985 self.SetCellValue(row_idx, 1, gmTools.coalesce(med['schedule'], u'')) 1986 self.SetCellValue(row_idx, 2, med['substance']) 1987 self.SetCellValue(row_idx, 3, u'%s%s' % (med['amount'], med['unit'])) 1988 self.SetCellValue(row_idx, 4, med['started'].strftime('%Y-%m-%d')) 1989 1990 if med['is_long_term']: 1991 self.SetCellValue(row_idx, 5, gmTools.u_infinity) 1992 else: 1993 if med['discontinued'] is None: 1994 if med['duration'] is None: 1995 self.SetCellValue(row_idx, 5, u'') 1996 else: 1997 self.SetCellValue(row_idx, 5, gmDateTime.format_interval(med['duration'], gmDateTime.acc_days)) 1998 else: 1999 self.SetCellValue(row_idx, 5, med['discontinued'].strftime('%Y-%m-%d')) 2000 2001 if med['pk_episode'] is None: 2002 epi = u'' 2003 else: 2004 epi = gmTools.coalesce(med['episode'], u'') 2005 self.SetCellValue(row_idx, 6, gmTools.wrap(text = epi, width = 40)) 2006 2007 else: 2008 raise ValueError('unknown grouping mode [%s]' % self.__grouping_mode) 2009 2010 if med['notes'] is not None: 2011 self.SetCellValue(row_idx, 7, gmTools.wrap(text = med['notes'], width = 50)) 2012 2013 if self.__filter_show_unapproved: 2014 self.SetCellValue ( 2015 row_idx, 2016 len(labels), 2017 gmTools.bool2subst(med['intake_is_approved_of'], gmTools.u_checkmark_thin, u'', u'?') 2018 ) 2019 2020 #self.SetCellAlignment(row, col, horiz = wx.ALIGN_RIGHT, vert = wx.ALIGN_CENTRE) 2021 2022 self.AutoSize() 2023 self.EndBatch()
2024 #------------------------------------------------------------
2025 - def empty_grid(self):
2026 self.BeginBatch() 2027 self.ClearGrid() 2028 # Windows cannot do "nothing", it rather decides to assert() 2029 # on thinking it is supposed to do nothing 2030 if self.GetNumberRows() > 0: 2031 self.DeleteRows(pos = 0, numRows = self.GetNumberRows()) 2032 if self.GetNumberCols() > 0: 2033 self.DeleteCols(pos = 0, numCols = self.GetNumberCols()) 2034 self.EndBatch() 2035 self.__row_data = {} 2036 self.__prev_cell_0 = None
2037 #------------------------------------------------------------
2038 - def show_info_on_entry(self):
2039 2040 if len(self.__row_data) == 0: 2041 return 2042 2043 sel_rows = self.get_selected_rows() 2044 if len(sel_rows) != 1: 2045 return 2046 2047 drug_db = get_drug_database() 2048 if drug_db is None: 2049 return 2050 2051 intake = self.get_selected_data()[0] # just in case 2052 if intake['brand'] is None: 2053 drug_db.show_info_on_substance(substance_intake = intake) 2054 else: 2055 drug_db.show_info_on_drug(substance_intake = intake)
2056 #------------------------------------------------------------
2058 2059 if len(self.__row_data) == 0: 2060 return 2061 2062 sel_rows = self.get_selected_rows() 2063 2064 if len(sel_rows) != 1: 2065 return 2066 2067 webbrowser.open ( 2068 url = gmMedication.drug2renal_insufficiency_url(search_term = self.get_selected_data()[0]), 2069 new = False, 2070 autoraise = True 2071 )
2072 #------------------------------------------------------------
2073 - def report_ADR(self):
2074 2075 dbcfg = gmCfg.cCfgSQL() 2076 2077 url = dbcfg.get2 ( 2078 option = u'external.urls.report_ADR', 2079 workplace = gmSurgery.gmCurrentPractice().active_workplace, 2080 bias = u'user', 2081 default = u'https://dcgma.org/uaw/meldung.php' # http://www.akdae.de/Arzneimittelsicherheit/UAW-Meldung/UAW-Meldung-online.html 2082 ) 2083 2084 webbrowser.open(url = url, new = False, autoraise = True)
2085 #------------------------------------------------------------
2086 - def prescribe(self):
2087 drug_db = get_drug_database() 2088 if drug_db is None: 2089 return 2090 2091 drug_db.reviewer = gmPerson.gmCurrentProvider() 2092 update_substance_intake_list_from_prescription ( 2093 parent = self, 2094 prescribed_drugs = drug_db.prescribe(), 2095 emr = self.__patient.get_emr() 2096 )
2097 #------------------------------------------------------------
2098 - def check_interactions(self):
2099 2100 if len(self.__row_data) == 0: 2101 return 2102 2103 drug_db = get_drug_database() 2104 if drug_db is None: 2105 return 2106 2107 if len(self.get_selected_rows()) > 1: 2108 drug_db.check_interactions(substance_intakes = self.get_selected_data()) 2109 else: 2110 drug_db.check_interactions(substance_intakes = self.__row_data.values())
2111 #------------------------------------------------------------
2112 - def add_substance(self):
2113 edit_intake_of_substance(parent = self, substance = None)
2114 #------------------------------------------------------------
2115 - def edit_substance(self):
2116 2117 rows = self.get_selected_rows() 2118 2119 if len(rows) == 0: 2120 return 2121 2122 if len(rows) > 1: 2123 gmDispatcher.send(signal = 'statustext', msg = _('Cannot edit more than one substance at once.'), beep = True) 2124 return 2125 2126 subst = self.get_selected_data()[0] 2127 edit_intake_of_substance(parent = self, substance = subst)
2128 #------------------------------------------------------------
2129 - def delete_substance(self):
2130 2131 rows = self.get_selected_rows() 2132 2133 if len(rows) == 0: 2134 return 2135 2136 if len(rows) > 1: 2137 gmDispatcher.send(signal = 'statustext', msg = _('Cannot delete more than one substance at once.'), beep = True) 2138 return 2139 2140 subst = self.get_selected_data()[0] 2141 delete_substance_intake(parent = self, substance = subst['pk_substance_intake'])
2142 #------------------------------------------------------------
2144 rows = self.get_selected_rows() 2145 2146 if len(rows) == 0: 2147 return 2148 2149 if len(rows) > 1: 2150 gmDispatcher.send(signal = 'statustext', msg = _('Cannot create allergy from more than one substance at once.'), beep = True) 2151 return 2152 2153 return turn_substance_intake_into_allergy ( 2154 parent = self, 2155 intake = self.get_selected_data()[0], 2156 emr = self.__patient.get_emr() 2157 )
2158 #------------------------------------------------------------
2159 - def print_medication_list(self):
2160 # there could be some filtering/user interaction going on here 2161 print_medication_list(parent = self)
2162 #------------------------------------------------------------
2163 - def get_row_tooltip(self, row=None):
2164 2165 try: 2166 entry = self.__row_data[row] 2167 except KeyError: 2168 return u' ' 2169 2170 emr = self.__patient.get_emr() 2171 atcs = [] 2172 if entry['atc_substance'] is not None: 2173 atcs.append(entry['atc_substance']) 2174 # if entry['atc_brand'] is not None: 2175 # atcs.append(entry['atc_brand']) 2176 # allg = emr.is_allergic_to(atcs = tuple(atcs), inns = (entry['substance'],), brand = entry['brand']) 2177 allg = emr.is_allergic_to(atcs = tuple(atcs), inns = (entry['substance'],)) 2178 2179 tt = _('Substance intake entry (%s, %s) [#%s] \n') % ( 2180 gmTools.bool2subst ( 2181 boolean = entry['is_currently_active'], 2182 true_return = gmTools.bool2subst ( 2183 boolean = entry['seems_inactive'], 2184 true_return = _('active, needs check'), 2185 false_return = _('active'), 2186 none_return = _('assumed active') 2187 ), 2188 false_return = _('inactive') 2189 ), 2190 gmTools.bool2subst ( 2191 boolean = entry['intake_is_approved_of'], 2192 true_return = _('approved'), 2193 false_return = _('unapproved') 2194 ), 2195 entry['pk_substance_intake'] 2196 ) 2197 2198 if allg not in [None, False]: 2199 certainty = gmTools.bool2subst(allg['definite'], _('definite'), _('suspected')) 2200 tt += u'\n' 2201 tt += u' !! ---- Cave ---- !!\n' 2202 tt += u' %s (%s): %s (%s)\n' % ( 2203 allg['l10n_type'], 2204 certainty, 2205 allg['descriptor'], 2206 gmTools.coalesce(allg['reaction'], u'')[:40] 2207 ) 2208 tt += u'\n' 2209 2210 tt += u' ' + _('Substance: %s [#%s]\n') % (entry['substance'], entry['pk_substance']) 2211 tt += u' ' + _('Preparation: %s\n') % entry['preparation'] 2212 tt += u' ' + _('Amount per dose: %s%s') % (entry['amount'], entry['unit']) 2213 if entry.ddd is not None: 2214 tt += u' (DDD: %s %s)' % (entry.ddd['ddd'], entry.ddd['unit']) 2215 tt += u'\n' 2216 tt += gmTools.coalesce(entry['atc_substance'], u'', _(' ATC (substance): %s\n')) 2217 2218 tt += u'\n' 2219 2220 tt += gmTools.coalesce ( 2221 entry['brand'], 2222 u'', 2223 _(' Brand name: %%s [#%s]\n') % entry['pk_brand'] 2224 ) 2225 tt += gmTools.coalesce(entry['atc_brand'], u'', _(' ATC (brand): %s\n')) 2226 2227 tt += u'\n' 2228 2229 tt += gmTools.coalesce(entry['schedule'], u'', _(' Regimen: %s\n')) 2230 2231 if entry['is_long_term']: 2232 duration = u' %s %s' % (gmTools.u_right_arrow, gmTools.u_infinity) 2233 else: 2234 if entry['duration'] is None: 2235 duration = u'' 2236 else: 2237 duration = u' %s %s' % (gmTools.u_right_arrow, gmDateTime.format_interval(entry['duration'], gmDateTime.acc_days)) 2238 2239 tt += _(' Started %s%s%s\n') % ( 2240 entry['started'].strftime('%Y %B %d').decode(gmI18N.get_encoding()), 2241 duration, 2242 gmTools.bool2subst(entry['is_long_term'], _(' (long-term)'), _(' (short-term)'), u'') 2243 ) 2244 2245 if entry['discontinued'] is not None: 2246 tt += _(' Discontinued %s\n') % ( 2247 entry['discontinued'].strftime('%Y %B %d').decode(gmI18N.get_encoding()), 2248 ) 2249 tt += _(' Reason: %s\n') % entry['discontinue_reason'] 2250 2251 tt += u'\n' 2252 2253 tt += gmTools.coalesce(entry['aim'], u'', _(' Aim: %s\n')) 2254 tt += gmTools.coalesce(entry['episode'], u'', _(' Episode: %s\n')) 2255 tt += gmTools.coalesce(entry['notes'], u'', _(' Advice: %s\n')) 2256 2257 tt += u'\n' 2258 2259 tt += _(u'Revision: #%(row_ver)s, %(mod_when)s by %(mod_by)s.') % ({ 2260 'row_ver': entry['row_version'], 2261 'mod_when': entry['modified_when'].strftime('%c').decode(gmI18N.get_encoding()), 2262 'mod_by': entry['modified_by'] 2263 }) 2264 2265 return tt
2266 #------------------------------------------------------------ 2267 # internal helpers 2268 #------------------------------------------------------------
2269 - def __init_ui(self):
2270 self.CreateGrid(0, 1) 2271 self.EnableEditing(0) 2272 self.EnableDragGridSize(1) 2273 self.SetSelectionMode(wx.grid.Grid.wxGridSelectRows) 2274 2275 self.SetColLabelAlignment(wx.ALIGN_LEFT, wx.ALIGN_CENTER) 2276 2277 self.SetRowLabelSize(0) 2278 self.SetRowLabelAlignment(horiz = wx.ALIGN_RIGHT, vert = wx.ALIGN_CENTRE)
2279 #------------------------------------------------------------ 2280 # properties 2281 #------------------------------------------------------------
2282 - def _get_patient(self):
2283 return self.__patient
2284
2285 - def _set_patient(self, patient):
2286 self.__patient = patient 2287 self.repopulate_grid()
2288 2289 patient = property(_get_patient, _set_patient) 2290 #------------------------------------------------------------
2291 - def _get_grouping_mode(self):
2292 return self.__grouping_mode
2293
2294 - def _set_grouping_mode(self, mode):
2295 self.__grouping_mode = mode 2296 self.repopulate_grid()
2297 2298 grouping_mode = property(_get_grouping_mode, _set_grouping_mode) 2299 #------------------------------------------------------------
2301 return self.__filter_show_unapproved
2302
2303 - def _set_filter_show_unapproved(self, val):
2304 self.__filter_show_unapproved = val 2305 self.repopulate_grid()
2306 2307 filter_show_unapproved = property(_get_filter_show_unapproved, _set_filter_show_unapproved) 2308 #------------------------------------------------------------
2309 - def _get_filter_show_inactive(self):
2310 return self.__filter_show_inactive
2311
2312 - def _set_filter_show_inactive(self, val):
2313 self.__filter_show_inactive = val 2314 self.repopulate_grid()
2315 2316 filter_show_inactive = property(_get_filter_show_inactive, _set_filter_show_inactive) 2317 #------------------------------------------------------------ 2318 # event handling 2319 #------------------------------------------------------------
2320 - def __register_events(self):
2321 # dynamic tooltips: GridWindow, GridRowLabelWindow, GridColLabelWindow, GridCornerLabelWindow 2322 self.GetGridWindow().Bind(wx.EVT_MOTION, self.__on_mouse_over_cells) 2323 #self.GetGridRowLabelWindow().Bind(wx.EVT_MOTION, self.__on_mouse_over_row_labels) 2324 #self.GetGridColLabelWindow().Bind(wx.EVT_MOTION, self.__on_mouse_over_col_labels) 2325 2326 # editing cells 2327 self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.__on_cell_left_dclicked)
2328 #------------------------------------------------------------
2329 - def __on_mouse_over_cells(self, evt):
2330 """Calculate where the mouse is and set the tooltip dynamically.""" 2331 2332 # Use CalcUnscrolledPosition() to get the mouse position within the 2333 # entire grid including what's offscreen 2334 x, y = self.CalcUnscrolledPosition(evt.GetX(), evt.GetY()) 2335 2336 # use this logic to prevent tooltips outside the actual cells 2337 # apply to GetRowSize, too 2338 # tot = 0 2339 # for col in xrange(self.NumberCols): 2340 # tot += self.GetColSize(col) 2341 # if xpos <= tot: 2342 # self.tool_tip.Tip = 'Tool tip for Column %s' % ( 2343 # self.GetColLabelValue(col)) 2344 # break 2345 # else: # mouse is in label area beyond the right-most column 2346 # self.tool_tip.Tip = '' 2347 2348 row, col = self.XYToCell(x, y) 2349 2350 if row == self.__prev_tooltip_row: 2351 return 2352 2353 self.__prev_tooltip_row = row 2354 2355 try: 2356 evt.GetEventObject().SetToolTipString(self.get_row_tooltip(row = row)) 2357 except KeyError: 2358 pass
2359 #------------------------------------------------------------
2360 - def __on_cell_left_dclicked(self, evt):
2361 row = evt.GetRow() 2362 data = self.__row_data[row] 2363 edit_intake_of_substance(parent = self, substance = data)
2364 #============================================================ 2365 from Gnumed.wxGladeWidgets import wxgCurrentSubstancesPnl 2366
2367 -class cCurrentSubstancesPnl(wxgCurrentSubstancesPnl.wxgCurrentSubstancesPnl, gmRegetMixin.cRegetOnPaintMixin):
2368 2369 """Panel holding a grid with current substances. Used as notebook page.""" 2370
2371 - def __init__(self, *args, **kwargs):
2372 2373 wxgCurrentSubstancesPnl.wxgCurrentSubstancesPnl.__init__(self, *args, **kwargs) 2374 gmRegetMixin.cRegetOnPaintMixin.__init__(self) 2375 2376 self.__register_interests()
2377 #----------------------------------------------------- 2378 # reget-on-paint mixin API 2379 #-----------------------------------------------------
2380 - def _populate_with_data(self):
2381 """Populate cells with data from model.""" 2382 pat = gmPerson.gmCurrentPatient() 2383 if pat.connected: 2384 self._grid_substances.patient = pat 2385 else: 2386 self._grid_substances.patient = None 2387 return True
2388 #-------------------------------------------------------- 2389 # event handling 2390 #--------------------------------------------------------
2391 - def __register_interests(self):
2392 gmDispatcher.connect(signal = u'pre_patient_selection', receiver = self._on_pre_patient_selection) 2393 gmDispatcher.connect(signal = u'post_patient_selection', receiver = self._schedule_data_reget) 2394 gmDispatcher.connect(signal = u'substance_intake_mod_db', receiver = self._schedule_data_reget)
2395 # active_substance_mod_db 2396 # substance_brand_mod_db 2397 #--------------------------------------------------------
2398 - def _on_pre_patient_selection(self):
2399 wx.CallAfter(self.__on_pre_patient_selection)
2400 #--------------------------------------------------------
2401 - def __on_pre_patient_selection(self):
2402 self._grid_substances.patient = None
2403 #--------------------------------------------------------
2404 - def _on_add_button_pressed(self, event):
2405 self._grid_substances.add_substance()
2406 #--------------------------------------------------------
2407 - def _on_edit_button_pressed(self, event):
2408 self._grid_substances.edit_substance()
2409 #--------------------------------------------------------
2410 - def _on_delete_button_pressed(self, event):
2411 self._grid_substances.delete_substance()
2412 #--------------------------------------------------------
2413 - def _on_info_button_pressed(self, event):
2414 self._grid_substances.show_info_on_entry()
2415 #--------------------------------------------------------
2416 - def _on_interactions_button_pressed(self, event):
2417 self._grid_substances.check_interactions()
2418 #--------------------------------------------------------
2419 - def _on_episode_grouping_selected(self, event):
2420 self._grid_substances.grouping_mode = 'episode'
2421 #--------------------------------------------------------
2422 - def _on_brand_grouping_selected(self, event):
2423 self._grid_substances.grouping_mode = 'brand'
2424 #--------------------------------------------------------
2425 - def _on_show_unapproved_checked(self, event):
2426 self._grid_substances.filter_show_unapproved = self._CHBOX_show_unapproved.GetValue()
2427 #--------------------------------------------------------
2428 - def _on_show_inactive_checked(self, event):
2429 self._grid_substances.filter_show_inactive = self._CHBOX_show_inactive.GetValue()
2430 #--------------------------------------------------------
2431 - def _on_print_button_pressed(self, event):
2432 self._grid_substances.print_medication_list()
2433 #--------------------------------------------------------
2434 - def _on_allergy_button_pressed(self, event):
2435 self._grid_substances.create_allergy_from_substance()
2436 #--------------------------------------------------------
2437 - def _on_button_kidneys_pressed(self, event):
2438 self._grid_substances.show_renal_insufficiency_info()
2439 #--------------------------------------------------------
2440 - def _on_adr_button_pressed(self, event):
2441 self._grid_substances.report_ADR()
2442 #--------------------------------------------------------
2443 - def _on_rx_button_pressed(self, event):
2444 self._grid_substances.prescribe()
2445 #============================================================ 2446 # main 2447 #------------------------------------------------------------ 2448 if __name__ == '__main__': 2449 2450 if len(sys.argv) < 2: 2451 sys.exit() 2452 2453 if sys.argv[1] != 'test': 2454 sys.exit() 2455 2456 from Gnumed.pycommon import gmI18N 2457 2458 gmI18N.activate_locale() 2459 gmI18N.install_domain(domain = 'gnumed') 2460 2461 #---------------------------------------- 2462 app = wx.PyWidgetTester(size = (600, 600)) 2463 #app.SetWidget(cATCPhraseWheel, -1) 2464 app.SetWidget(cSubstancePhraseWheel, -1) 2465 app.MainLoop() 2466 2467 #============================================================ 2468