Gnumed.wxpython.gmHabitWidgets

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

# Source Code for Module Gnumed.wxpython.gmHabitWidgets

      1  """GNUmed habits widgets."""
      2  #================================================================
      3  __author__ = "Karsten Hilbert <Karsten.Hilbert@gmx.net>"
      4  __license__ = "GPL v2 or later"
      5
      6  import sys
      7  import logging
      8
      9
     10  import wx
     11
     12
     13  if __name__ == '__main__':
     14          sys.path.insert(0, '../../')
     15  from Gnumed.pycommon import gmTools
     16  from Gnumed.pycommon import gmDispatcher
     17  from Gnumed.pycommon import gmDateTime
     18
     19  from Gnumed.business import gmATC
     20  from Gnumed.business import gmMedication
     21
     22  from Gnumed.wxpython import gmEditArea
     23  from Gnumed.wxpython import gmListWidgets
     24
     25  _log = logging.getLogger('gm.ui')
     26
     27  #================================================================
     28 -def edit_substance_abuse(parent=None, intake=None, patient=None):
     29
     30          if parent is None:
     31                  parent = wx.GetApp().GetTopWindow()
     32
     33          ea = cSubstanceAbuseEAPnl(parent, -1, intake = intake, patient = patient)
     34          dlg = gmEditArea.cGenericEditAreaDlg2(parent, -1, edit_area = ea, single_entry = (intake is not None))
     35          dlg.SetTitle(gmTools.coalesce(intake, _('Adding substance abuse'), _('Editing substance abuse')))
     36          if dlg.ShowModal() == wx.ID_OK:
     37                  dlg.Destroy()
     38                  return True
     39          dlg.Destroy()
     40          return False
     41
     42  #----------------------------------------------------------------
     43 -def manage_substance_abuse(parent=None, patient=None):
     44
     45          if parent is None:
     46                  parent = wx.GetApp().GetTopWindow()
     47
     48  #       #------------------------------------------------------------
     49  #       def add_from_db(substance):
     50  #               drug_db = get_drug_database(parent = parent, patient = gmPerson.gmCurrentPatient())
     51  #               if drug_db is None:
     52  #                       return False
     53  #               drug_db.import_drugs()
     54  #               return True
     55  #       #------------------------------------------------------------
     56          def edit(intake=None):
     57                  return edit_substance_abuse(parent = parent, intake = intake, patient = patient)
     58
     59          #------------------------------------------------------------
     60          def delete(intake):
     61                  return intake.delete()
     62
     63          #------------------------------------------------------------
     64          def get_tooltip(intake=None):
     65                  return intake.format(single_line = False)
     66
     67          #------------------------------------------------------------
     68          def refresh(lctrl):
     69                  intakes = patient.emr.abused_substances
     70                  items = []
     71                  for i in intakes:
     72                          items.append ([
     73                                  i['substance'],
     74                                  i.harmful_use_type_string,
     75                                  gmDateTime.pydt_strftime(i['last_checked_when'], '%b %Y', none_str = '')
     76                          ])
     77                  lctrl.set_string_items(items)
     78                  lctrl.set_data(intakes)
     79
     80          #------------------------------------------------------------
     81          if len(patient.emr.abused_substances) == 0:
     82                  edit()
     83
     84          msg = _('Substances abused by the patient:')
     85
     86          return gmListWidgets.get_choices_from_list (
     87                  parent = parent,
     88                  msg = msg,
     89                  caption = _('Showing abused substances.'),
     90                  columns = [ _('Intake'), _('Status'), _('Last confirmed') ],
     91                  single_selection = False,
     92                  new_callback = edit,
     93                  edit_callback = edit,
     94                  delete_callback = delete,
     95                  refresh_callback = refresh,
     96                  list_tooltip_callback = get_tooltip
     97          )
     98
     99  #----------------------------------------------------------------
    100  from Gnumed.wxGladeWidgets import wxgSubstanceAbuseEAPnl
    101
    102 -class cSubstanceAbuseEAPnl(wxgSubstanceAbuseEAPnl.wxgSubstanceAbuseEAPnl, gmEditArea.cGenericEditAreaMixin):
    103
    104 -        def __init__(self, *args, **kwargs):
    105                  try:
    106                          data = kwargs['intake']
    107                          del kwargs['intake']
    108                  except KeyError:
    109                          data = None
    110
    111                  self.__patient = kwargs['patient']
    112                  del kwargs['patient']
    113
    114                  if data is not None:
    115                          if data['pk_patient'] != self.__patient.ID:
    116                                  _log.error('intake: %s', data)
    117                                  _log.error('patient: %s', self.__patient)
    118                                  raise ArgumentError('<intake> does not belong to <patient>')
    119
    120                  wxgSubstanceAbuseEAPnl.wxgSubstanceAbuseEAPnl.__init__(self, *args, **kwargs)
    121                  gmEditArea.cGenericEditAreaMixin.__init__(self)
    122
    123                  self.mode = 'new'
    124                  self.data = data
    125                  if data is not None:
    126                          self.mode = 'edit'
    127
    128                  #self.__init_ui()
    129
    130  #       #----------------------------------------------------------------
    131  #       def __init_ui(self):
    132  #               if self.mode == 'new':
    133          #----------------------------------------------------------------
    134          # generic Edit Area mixin API
    135          #----------------------------------------------------------------
    136 -        def _valid_for_save(self):
    137                  validity = True
    138
    139                  if not self._DPRW_quit_when.is_valid_timestamp(empty_is_valid = True):
    140                          validity = False
    141                          self._DPRW_quit_when.SetFocus()
    142
    143                  if self._RBTN_other_substance.GetValue() is True:
    144                          if self._PRW_substance.GetValue().strip() == '':
    145                                  validity = False
    146                                  self._PRW_substance.display_as_valid(valid = False)
    147                                  self._PRW_substance.SetFocus()
    148                          else:
    149                                  self._PRW_substance.display_as_valid(valid = True)
    150
    151                  return validity
    152
    153          #----------------------------------------------------------------
    154 -        def _save_as_new(self):
    155
    156                  if self._RBTN_tobacco.GetValue() is True:
    157                          pk_drug_product = gmMedication.get_tobacco()['pk_drug_product']
    158
    159                  elif self._RBTN_c2.GetValue() is True:
    160                          pk_drug_product = gmMedication.get_alcohol()['pk_drug_product']
    161
    162                  elif self._RBTN_other_substance.GetValue() is True:
    163                          #xxxxxxxxx
    164                          #PRW_substance -> _dose
    165                          pk_drug_product = gmMedication.get_other_drug (
    166                                  name = self._PRW_substance.GetValue().strip(),
    167                                  pk_dose = self._PRW_substance.GetData()
    168                          )['pk_drug_product']
    169
    170                  pk_encounter = self.__patient.emr.active_encounter['pk_encounter']
    171                  intake = gmMedication.create_substance_intake (
    172                          pk_drug_product = pk_drug_product,
    173                          pk_encounter = pk_encounter,
    174                          pk_episode = gmMedication.create_default_medication_history_episode(encounter = pk_encounter)['pk_episode']
    175                  )
    176
    177                  if self._RBTN_nonharmful_use.GetValue() is True:
    178                          intake['harmful_use_type'] = 0
    179                  elif self._RBTN_harmful_use.GetValue() is True:
    180                          intake['harmful_use_type'] = 1
    181                  elif self._RBTN_presently_addicted.GetValue() is True:
    182                          intake['harmful_use_type'] = 2
    183                  elif self._RBTN_previously_addicted.GetValue() is True:
    184                          intake['harmful_use_type'] = 3
    185                  intake['notes'] = self._TCTRL_comment.GetValue().strip()
    186                  if self._DPRW_quit_when.is_valid_timestamp(empty_is_valid = False):
    187                          intake['discontinued'] = self._DPRW_quit_when.date
    188                  intake.save()
    189
    190                  self.data = intake
    191
    192                  return True
    193
    194          #----------------------------------------------------------------
    195 -        def _save_as_update(self):
    196
    197                  if self._RBTN_nonharmful_use.GetValue() is True:
    198                          self.data['harmful_use_type'] = 0
    199                  elif self._RBTN_harmful_use.GetValue() is True:
    200                          self.data['harmful_use_type'] = 1
    201                  elif self._RBTN_presently_addicted.GetValue() is True:
    202                          self.data['harmful_use_type'] = 2
    203                  elif self._RBTN_previously_addicted.GetValue() is True:
    204                          self.data['harmful_use_type'] = 3
    205                  self.data['notes'] = self._TCTRL_comment.GetValue().strip()
    206                  if self._DPRW_quit_when.is_valid_timestamp(empty_is_valid = False):
    207                          self.data['discontinued'] = self._DPRW_quit_when.date
    208                  if self._CHBOX_confirm.GetValue() is True:
    209                          self.data['pk_encounter'] = self.__patient.emr.active_encounter['pk_encounter']
    210
    211                  self.data.save()
    212
    213                  return True
    214
    215          #----------------------------------------------------------------
    216 -        def _refresh_as_new(self):
    217
    218                  self._RBTN_tobacco.SetValue(False)
    219                  self._RBTN_c2.SetValue(False)
    220                  self._RBTN_other_substance.SetValue(True)
    221                  self._PRW_substance.SetText('', None)
    222                  self._PRW_substance.Enable()
    223                  self._RBTN_nonharmful_use.SetValue(True)
    224                  self._RBTN_harmful_use.SetValue(False)
    225                  self._RBTN_presently_addicted.SetValue(False)
    226                  self._RBTN_previously_addicted.SetValue(False)
    227                  self._TCTRL_comment.SetValue('')
    228                  self._DPRW_quit_when.SetText('', None)
    229                  self._LBL_confirm_date.SetLabel('<%s>' % _('today'))
    230                  self._CHBOX_confirm.SetValue(True)
    231                  self._CHBOX_confirm.Disable()
    232
    233                  if gmMedication.substance_intake_exists_by_atc(pk_identity = self.__patient.ID, atc = gmATC.ATC_NICOTINE):
    234                          self._RBTN_tobacco.Disable()
    235                  else:
    236                          self._RBTN_tobacco.Enable()
    237
    238                  if gmMedication.substance_intake_exists_by_atc(pk_identity = self.__patient.ID, atc = gmATC.ATC_ETHANOL):
    239                          self._RBTN_c2.Disable()
    240                  else:
    241                          self._RBTN_c2.Enable()
    242
    243                  self._PRW_substance.SetFocus()
    244
    245          #----------------------------------------------------------------
    246 -        def _refresh_as_new_from_existing(self):
    247                  self._refresh_as_new()
    248
    249          #----------------------------------------------------------------
    250 -        def _refresh_from_existing(self):
    251
    252                  self._RBTN_tobacco.Disable()
    253                  self._RBTN_c2.Disable()
    254                  self._RBTN_other_substance.Disable()
    255                  self._PRW_substance.Disable()
    256
    257                  if self.data['atc_substance'] == gmATC.ATC_NICOTINE:
    258                          self._RBTN_tobacco.SetValue(True)
    259                          self._RBTN_c2.SetValue(False)
    260                          self._RBTN_other_substance.SetValue(False)
    261                          self._PRW_substance.SetText('', None)
    262                  elif self.data['atc_substance'] == gmATC.ATC_ETHANOL:
    263                          self._RBTN_tobacco.SetValue(False)
    264                          self._RBTN_c2.SetValue(True)
    265                          self._RBTN_other_substance.SetValue(False)
    266                          self._PRW_substance.SetText('', None)
    267                  else:
    268                          self._RBTN_tobacco.SetValue(False)
    269                          self._RBTN_c2.SetValue(False)
    270                          self._RBTN_other_substance.SetValue(True)
    271                          self._PRW_substance.SetText(self.data['substance'], self.data['pk_substance'])
    272
    273                  if self.data['harmful_use_type'] == 0:
    274                          self._RBTN_nonharmful_use.SetValue(True)
    275                          self._RBTN_harmful_use.SetValue(False)
    276                          self._RBTN_presently_addicted.SetValue(False)
    277                          self._RBTN_previously_addicted.SetValue(False)
    278                  elif self.data['harmful_use_type'] == 1:
    279                          self._RBTN_nonharmful_use.SetValue(False)
    280                          self._RBTN_harmful_use.SetValue(True)
    281                          self._RBTN_presently_addicted.SetValue(False)
    282                          self._RBTN_previously_addicted.SetValue(False)
    283                  elif self.data['harmful_use_type'] == 2:
    284                          self._RBTN_nonharmful_use.SetValue(False)
    285                          self._RBTN_harmful_use.SetValue(False)
    286                          self._RBTN_presently_addicted.SetValue(True)
    287                          self._RBTN_previously_addicted.SetValue(False)
    288                  elif self.data['harmful_use_type'] == 3:
    289                          self._RBTN_nonharmful_use.SetValue(False)
    290                          self._RBTN_harmful_use.SetValue(False)
    291                          self._RBTN_presently_addicted.SetValue(False)
    292                          self._RBTN_previously_addicted.SetValue(True)
    293
    294                  self._TCTRL_comment.SetValue(gmTools.coalesce(self.data['notes'], ''))
    295                  self._DPRW_quit_when.SetText(data = self.data['discontinued'])
    296                  self._LBL_confirm_date.SetLabel(gmDateTime.pydt_strftime(self.data['last_checked_when'], '%Y %b %d', none_str = ''))
    297                  self._CHBOX_confirm.Enable()
    298                  self._CHBOX_confirm.SetValue(True)
    299
    300                  self._TCTRL_comment.SetFocus()
    301
    302          #----------------------------------------------------------------
    303 -        def _on_substance_rbutton_selected(self, event):
    304                  event.Skip()
    305                  if self._RBTN_other_substance.GetValue() is True:
    306                          self._PRW_substance.Enable(True)
    307                          return
    308                  self._PRW_substance.Disable()
    309
    310  #================================================================
    311  # main
    312  #================================================================
    313  if __name__ == '__main__':
    314
    315          if len(sys.argv) < 2:
    316                  sys.exit()
    317
    318          if sys.argv[1] != 'test':
    319                  sys.exit()
    320
    321          gmI18N.activate_locale()
    322          gmI18N.install_domain(domain = 'gnumed')
    323
    324  #       def test_message_inbox():
    325  #               app = wx.PyWidgetTester(size = (800, 600))
    326  #               app.SetWidget(cProviderInboxPnl, -1)
    327  #               app.MainLoop()
    328
    329  #       def test_msg_ea():
    330  #               app = wx.PyWidgetTester(size = (800, 600))
    331  #               app.SetWidget(cInboxMessageEAPnl, -1)
    332  #               app.MainLoop()
    333
    334
    335          #test_message_inbox()
    336          #test_msg_ea()
    337