Gnumed.wxpython.gmExternalCareWidgets

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

# Source Code for Module Gnumed.wxpython.gmExternalCareWidgets

      1  """GNUmed external patient care widgets."""
      2  #================================================================
      3  __author__ = "Karsten.Hilbert@gmx.net"
      4  __license__ = "GPL v2 or later"
      5
      6  # std lib
      7  import sys
      8  import logging
      9
     10
     11  # 3rd party
     12  import wx
     13
     14
     15  # GNUmed libs
     16  if __name__ == '__main__':
     17          sys.path.insert(0, '../../')
     18
     19  from Gnumed.pycommon import gmTools
     20  from Gnumed.pycommon import gmDispatcher
     21
     22  from Gnumed.business import gmExternalCare
     23  from Gnumed.business import gmPerson
     24
     25  from Gnumed.wxpython import gmListWidgets
     26  from Gnumed.wxpython import gmEditArea
     27  from Gnumed.wxpython import gmGuiHelpers
     28
     29
     30  _log = logging.getLogger('gm.ui')
     31
     32  #============================================================
     33 -def manage_external_care(parent=None):
     34
     35          pat = gmPerson.gmCurrentPatient()
     36          emr = pat.emr
     37
     38          if parent is None:
     39                  parent = wx.GetApp().GetTopWindow()
     40
     41          #-----------------------------------------
     42          def edit(external_care_item=None):
     43                  return edit_external_care_item(parent = parent, external_care_item = external_care_item)
     44
     45          #-----------------------------------------
     46          def delete(external_care_item=None):
     47                  if gmExternalCare.delete_external_care_item(pk_external_care = external_care_item['pk_external_care']):
     48                          return True
     49
     50                  gmDispatcher.send (
     51                          signal = 'statustext',
     52                          msg = _('Cannot delete external care item.'),
     53                          beep = True
     54                  )
     55                  return False
     56
     57          #------------------------------------------------------------
     58          def get_tooltip(data):
     59                  if data is None:
     60                          return None
     61                  return '\n'.join(data.format(with_health_issue = True, with_address = True, with_comms = True))
     62
     63          #------------------------------------------------------------
     64          def refresh(lctrl):
     65                  care = emr.get_external_care_items(order_by = 'inactive, issue, provider, unit, organization')
     66                  items = [ [
     67                          '%s @ %s' % (
     68                                  c['unit'],
     69                                  c['organization']
     70                          ),
     71                          gmTools.coalesce(c['provider'], ''),
     72                          c['issue'],
     73                          gmTools.bool2subst(c['inactive'], _('inactive'), '', '<ERROR: .inactive IS NULL>'),
     74                          gmTools.coalesce(c['comment'], '')
     75                  ] for c in care ]
     76                  lctrl.set_string_items(items)
     77                  lctrl.set_data(care)
     78
     79          #------------------------------------------------------------
     80          return gmListWidgets.get_choices_from_list (
     81                  parent = parent,
     82                  msg = _('External care of this patient.'),
     83                  caption = _('Showing external care network.'),
     84                  columns = [ _('Location'), _('Provider'), _('Reason for care'), _('Status'), _('Comment') ],
     85                  single_selection = False,
     86                  can_return_empty = True,
     87                  ignore_OK_button = False,
     88                  refresh_callback = refresh,
     89                  edit_callback = edit,
     90                  new_callback = edit,
     91                  delete_callback = delete,
     92                  list_tooltip_callback = get_tooltip
     93  #               left_extra_button=None,         # manage orgs
     94  #               middle_extra_button=None,       # manage issues
     95  #               right_extra_button=None
     96          )
     97
     98  #----------------------------------------------------------------
     99 -def edit_external_care_item(parent=None, external_care_item=None, single_entry=True):
    100          ea = cExternalCareEAPnl(parent, -1)
    101          ea.data = external_care_item
    102          ea.mode = gmTools.coalesce(external_care_item, 'new', 'edit')
    103          dlg = gmEditArea.cGenericEditAreaDlg2(parent, -1, edit_area = ea, single_entry = single_entry)
    104          dlg.SetTitle(gmTools.coalesce(external_care_item, _('Adding external care'), _('Editing external care')))
    105          if dlg.ShowModal() == wx.ID_OK:
    106                  dlg.DestroyLater()
    107                  return True
    108          dlg.DestroyLater()
    109          return False
    110
    111  #====================================================================
    112  from Gnumed.wxGladeWidgets import wxgExternalCareEAPnl
    113
    114 -class cExternalCareEAPnl(wxgExternalCareEAPnl.wxgExternalCareEAPnl, gmEditArea.cGenericEditAreaMixin):
    115
    116 -        def __init__(self, *args, **kwargs):
    117
    118                  try:
    119                          data = kwargs['care']
    120                          del kwargs['care']
    121                  except KeyError:
    122                          data = None
    123
    124                  wxgExternalCareEAPnl.wxgExternalCareEAPnl.__init__(self, *args, **kwargs)
    125                  gmEditArea.cGenericEditAreaMixin.__init__(self)
    126
    127                  self.mode = 'new'
    128                  self.data = data
    129                  if data is not None:
    130                          self.mode = 'edit'
    131
    132                  #self.__init_ui()
    133          #----------------------------------------------------------------
    134  #       def __init_ui(self):
    135  #               # adjust phrasewheels etc
    136          #----------------------------------------------------------------
    137          # generic Edit Area mixin API
    138          #----------------------------------------------------------------
    139 -        def _valid_for_save(self):
    140                  validity = True
    141
    142                  if self._PRW_care_location.GetData() is None:
    143                          validity = False
    144                          self._PRW_care_location.display_as_valid(False)
    145                          self.StatusText = _('No entry in field "Care Location".')
    146                          self._PRW_care_location.SetFocus()
    147                  else:
    148                          self._PRW_care_location.display_as_valid(True)
    149
    150                  if self._PRW_issue.GetData() is not None:
    151                          self._PRW_issue.display_as_valid(True)
    152                  else:
    153                          if self._PRW_issue.GetValue().strip() != '':
    154                                  self._PRW_issue.display_as_valid(True)
    155                          else:
    156                                  validity = False
    157                                  self._PRW_issue.display_as_valid(False)
    158                                  self.StatusText = _('No entry in field [Care Target].')
    159                                  self._PRW_issue.SetFocus()
    160
    161                  return validity
    162
    163          #----------------------------------------------------------------
    164 -        def _save_as_new(self):
    165                  data = gmExternalCare.create_external_care_item (
    166                          pk_encounter = gmPerson.gmCurrentPatient().emr.current_encounter['pk_encounter'],
    167                          pk_health_issue = self._PRW_issue.GetData(),
    168                          issue = self._PRW_issue.GetValue().strip(),
    169                          pk_org_unit = self._PRW_care_location.GetData()
    170                  )
    171                  data['provider'] = self._TCTRL_provider.GetValue().strip()
    172                  data['comment'] = self._TCTRL_comment.GetValue().strip()
    173                  data['inactive'] = self._CHBOX_inactive.IsChecked()
    174                  data.save()
    175                  self.data = data
    176                  return True
    177
    178          #----------------------------------------------------------------
    179 -        def _save_as_update(self):
    180                  self.data['pk_encounter'] = gmPerson.gmCurrentPatient().emr.current_encounter['pk_encounter']
    181                  self.data['pk_health_issue'] = self._PRW_issue.GetData()
    182                  self.data['issue'] = self._PRW_issue.GetValue().strip()
    183                  self.data['pk_org_unit'] = self._PRW_care_location.GetData()
    184                  self.data['provider'] = self._TCTRL_provider.GetValue().strip()
    185                  self.data['comment'] = self._TCTRL_comment.GetValue().strip()
    186                  self.data['inactive'] = self._CHBOX_inactive.IsChecked()
    187                  self.data.save()
    188                  return True
    189
    190          #----------------------------------------------------------------
    191 -        def _refresh_as_new(self):
    192                  self._PRW_issue.SetText('', None)
    193                  self._PRW_care_location.SetText('', None)
    194                  self._TCTRL_provider.SetValue('')
    195                  self._TCTRL_comment.SetValue('')
    196                  self._CHBOX_inactive.Value = False
    197
    198                  self._PRW_issue.SetFocus()
    199
    200          #----------------------------------------------------------------
    201 -        def _refresh_as_new_from_existing(self):
    202                  self._refresh_as_new()
    203
    204          #----------------------------------------------------------------
    205 -        def _refresh_from_existing(self):
    206                  self._PRW_issue.SetText(value = self.data['issue'], data = self.data['pk_health_issue'], suppress_smarts = True)
    207                  self._PRW_care_location.SetText(value = '%s @ %s' % (self.data['unit'], self.data['organization']), data = self.data['pk_org_unit'])
    208                  self._TCTRL_provider.SetValue(gmTools.coalesce(self.data['provider'], ''))
    209                  self._TCTRL_comment.SetValue(gmTools.coalesce(self.data['comment'], ''))
    210                  self._CHBOX_inactive.Value = self.data['inactive']
    211
    212                  self._TCTRL_comment.SetFocus()
    213
    214  #------------------------------------------------------------
    215 -class cExternalCareMgrPnl(gmListWidgets.cGenericListManagerPnl):
    216          """A list for managing a patient's external care.
    217
    218          Does NOT act on/listen to the current patient.
    219          """
    220 -        def __init__(self, *args, **kwargs):
    221
    222                  try:
    223                          self.__identity = kwargs['identity']
    224                          del kwargs['identity']
    225                  except KeyError:
    226                          self.__identity = None
    227
    228                  gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs)
    229
    230                  self.refresh_callback = self.refresh
    231                  self.new_callback = self._add_care
    232                  self.edit_callback = self._edit_care
    233                  self.delete_callback = self._del_care
    234
    235                  self.__init_ui()
    236                  self.refresh()
    237          #--------------------------------------------------------
    238          # external API
    239          #--------------------------------------------------------
    240 -        def refresh(self, *args, **kwargs):
    241                  if self.__identity is None:
    242                          self._LCTRL_items.set_string_items()
    243                          return
    244
    245                  emr = self.__identity.emr
    246                  care = emr.get_external_care_items(order_by = 'inactive, issue, provider, unit, organization')
    247                  items = [ [
    248                          '%s @ %s' % (
    249                                  c['unit'],
    250                                  c['organization']
    251                          ),
    252                          gmTools.coalesce(c['provider'], ''),
    253                          c['issue'],
    254                          gmTools.bool2subst(c['inactive'], _('inactive'), '', '<ERROR: .inactive IS NULL>'),
    255                          gmTools.coalesce(c['comment'], '')
    256                  ] for c in care ]
    257                  self._LCTRL_items.set_string_items(items)
    258                  self._LCTRL_items.set_column_widths()
    259                  self._LCTRL_items.set_data(data = care)
    260
    261          #--------------------------------------------------------
    262          # internal helpers
    263          #--------------------------------------------------------
    264 -        def __init_ui(self):
    265                  self._LCTRL_items.set_columns(columns = [
    266                          _('Care location'),
    267                          _('Provider'),
    268                          _('Reason for care'),
    269                          _('Status'),
    270                          _('Comment')
    271                  ])
    272
    273          #--------------------------------------------------------
    274 -        def _add_care(self):
    275                  return edit_external_care_item(parent = self, external_care_item = None)
    276
    277          #--------------------------------------------------------
    278 -        def _edit_care(self, external_care_item):
    279                  return edit_external_care_item(parent = self, external_care_item = external_care_item)
    280
    281          #--------------------------------------------------------
    282 -        def _del_care(self, external_care_item):
    283                  go_ahead = gmGuiHelpers.gm_show_question (
    284                          _(      'Do you really want to delete this\n'
    285                                  'external care entry from the patient ?'),
    286                          _('Deleting external care entry')
    287                  )
    288                  if not go_ahead:
    289                          return False
    290                  if gmExternalCare.delete_external_care_item(pk_external_care = external_care_item['pk_external_care']):
    291                          return True
    292                  gmDispatcher.send (
    293                          signal = 'statustext',
    294                          msg = _('Cannot delete external care item.'),
    295                          beep = True
    296                  )
    297                  return False
    298
    299          #--------------------------------------------------------
    300          # properties
    301          #--------------------------------------------------------
    302 -        def _get_identity(self):
    303                  return self.__identity
    304
    305 -        def _set_identity(self, identity):
    306                  self.__identity = identity
    307                  self.refresh()
    308
    309          identity = property(_get_identity, _set_identity)
    310  #------------------------------------------------------------
    311