Gnumed.wxpython.gmStaffWidgets

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

# Source Code for Module Gnumed.wxpython.gmStaffWidgets

      1  """GNUmed staff management widgets."""
      2  #=========================================================================
      3  __author__  = "K. Hilbert <Karsten.Hilbert@gmx.net>"
      4  __license__ = "GPL v2 or later (details at http://www.gnu.org)"
      5
      6  import logging
      7
      8
      9  import wx
     10
     11
     12  from Gnumed.pycommon import gmTools
     13  from Gnumed.pycommon import gmI18N
     14  from Gnumed.pycommon import gmMatchProvider
     15
     16  from Gnumed.business import gmPerson
     17  from Gnumed.business import gmStaff
     18
     19  from Gnumed.wxpython import gmGuiHelpers
     20  from Gnumed.wxpython import gmAuthWidgets
     21  from Gnumed.wxpython import gmPhraseWheel
     22
     23
     24  _log = logging.getLogger('gm.ui')
     25
     26  #==========================================================================
     27 -class cProviderPhraseWheel(gmPhraseWheel.cPhraseWheel):
     28
     29 -        def __init__(self, *args, **kwargs):
     30
     31                  gmPhraseWheel.cPhraseWheel.__init__ (
     32                          self,
     33                          *args,
     34                          **kwargs
     35                  )
     36                  self.matcher = gmPerson.cMatchProvider_Provider()
     37                  self.SetToolTipString(_('Select a healthcare provider.'))
     38                  self.selection_only = True
     39
     40  #==========================================================================
     41 -class cUserRolePRW(gmPhraseWheel.cPhraseWheel):
     42
     43 -        def __init__(self, *args, **kwargs):
     44
     45                  gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
     46
     47                  items = [
     48                          {'list_label': _('Public (no clinical or demographic access)'), 'field_label': _('public'), 'data': 'public access', 'weight': 1},
     49                          {'list_label': _('Staff (demographic access only)'), 'field_label': _('staff (clerical)'), 'data': 'non-clinical access', 'weight': 1},
     50                          {'list_label': _('full clinical access'), 'field_label': _('full clinical access'), 'data': 'full clinical access', 'weight': 1}
     51                  ]
     52                  mp = gmMatchProvider.cMatchProvider_FixedList(items)
     53                  mp.setThresholds(1, 2, 3)
     54                  mp.word_separators = None
     55                  #mp.ignored_chars = r"[.'\\(){}\[\]<>~#*$%^_=&@\t0123456789]+" + r'"'
     56                  #self.SetToolTipString(_('The preparation (form) of the substance or brand.'))
     57                  self.matcher = mp
     58                  self.selection_only = True
     59
     60  #==========================================================================
     61  from Gnumed.wxGladeWidgets import wxgEditStaffListDlg
     62
     63 -class cEditStaffListDlg(wxgEditStaffListDlg.wxgEditStaffListDlg):
     64
     65 -        def __init__(self, *args, **kwds):
     66                  wxgEditStaffListDlg.wxgEditStaffListDlg.__init__(self, *args, **kwds)
     67
     68                  self._LCTRL_staff.InsertColumn(0, _('Alias'))
     69                  self._LCTRL_staff.InsertColumn(1, _('DB account'))
     70                  self._LCTRL_staff.InsertColumn(2, _('Role'))
     71                  self._LCTRL_staff.InsertColumn(3, _('Name'))
     72                  self._LCTRL_staff.InsertColumn(4, _('Comment'))
     73                  self._LCTRL_staff.InsertColumn(5, _('Status'))
     74
     75                  self.__init_ui_data()
     76          #--------------------------------------------------------
     77          # internal API
     78          #--------------------------------------------------------
     79 -        def __init_ui_data(self):
     80                  lbl_active = {True: _('active'), False: _('inactive')}
     81                  lbl_login = {True: _('can login'), False: _('can not login')}
     82
     83                  self._LCTRL_staff.DeleteAllItems()
     84                  staff_list = gmStaff.get_staff_list()
     85                  pos = len(staff_list) + 1
     86                  for staff in staff_list:
     87                          row_num = self._LCTRL_staff.InsertStringItem(pos, label=staff['short_alias'])
     88                          self._LCTRL_staff.SetStringItem(index = row_num, col = 1, label = staff['db_user'])
     89                          self._LCTRL_staff.SetStringItem(index = row_num, col = 2, label = staff['l10n_role'])
     90                          title = gmTools.coalesce(staff['title'], '')
     91                          self._LCTRL_staff.SetStringItem(index = row_num, col = 3, label = '%s %s, %s' % (title, staff['lastnames'], staff['firstnames']))
     92                          self._LCTRL_staff.SetStringItem(index = row_num, col = 4, label = gmTools.coalesce(staff['comment'], ''))
     93                          self._LCTRL_staff.SetStringItem(index = row_num, col = 5, label = '%s / %s' % (lbl_active[bool(staff['is_active'])], lbl_login[bool(staff['can_login'])]))
     94                          # color
     95                          if staff['is_active'] and staff['can_login']:
     96                                  #self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('BLUE'))
     97                                  pass
     98                          elif not staff['is_active'] and not staff['can_login']:
     99                                  self._LCTRL_staff.SetItemTextColour(row_num, col=wx.LIGHT_GREY)
    100                          else:
    101                                  self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('RED'))
    102                          # data
    103                          self._LCTRL_staff.SetItemData(item = row_num, data = staff['pk_staff'])
    104
    105                  if len(staff_list) > 0:
    106                          self._LCTRL_staff.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE)
    107                          self._LCTRL_staff.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE_USEHEADER)
    108                          self._LCTRL_staff.SetColumnWidth(col=2, width=wx.LIST_AUTOSIZE)
    109                          self._LCTRL_staff.SetColumnWidth(col=3, width=wx.LIST_AUTOSIZE)
    110                          self._LCTRL_staff.SetColumnWidth(col=4, width=wx.LIST_AUTOSIZE)
    111                          self._LCTRL_staff.SetColumnWidth(col=5, width=wx.LIST_AUTOSIZE)
    112
    113                  # disable buttons
    114                  self._btn_save.Enable(False)
    115                  self._btn_delete.Enable(False)
    116                  self._btn_deactivate.Enable(False)
    117                  self._btn_activate.Enable(False)
    118                  # clear editor
    119                  self._TCTRL_name.SetValue('')
    120                  self._TCTRL_alias.SetValue('')
    121                  self._TCTRL_account.SetValue('')
    122                  self._PRW_user_role.SetText(value = u'', data = None)
    123                  self._TCTRL_comment.SetValue('')
    124          #--------------------------------------------------------
    125          # event handlers
    126          #--------------------------------------------------------
    127 -        def _on_listitem_selected(self, evt):
    128                  self._btn_save.Enable(True)
    129                  self._btn_delete.Enable(True)
    130                  self._btn_deactivate.Enable(True)
    131                  self._btn_activate.Enable(True)
    132                  # fill editor
    133                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    134                  staff = gmStaff.cStaff(aPK_obj=pk_staff)
    135                  self._TCTRL_name.SetValue('%s.%s %s' % (staff['title'], staff['firstnames'], staff['lastnames']))
    136                  self._TCTRL_alias.SetValue(staff['short_alias'])
    137                  self._TCTRL_account.SetValue(staff['db_user'])
    138                  self._PRW_user_role.SetText(value = staff['l10n_role'], data = staff['role'], suppress_smarts = True)
    139                  self._TCTRL_comment.SetValue(gmTools.coalesce(staff['comment'], ''))
    140          #--------------------------------------------------------
    141 -        def _on_listitem_deselected(self, evt):
    142                  self._btn_save.Enable(False)
    143                  self._btn_delete.Enable(False)
    144                  self._btn_deactivate.Enable(False)
    145                  self._btn_activate.Enable(False)
    146                  # clear editor
    147                  self._TCTRL_name.SetValue('')
    148                  self._TCTRL_alias.SetValue('')
    149                  self._TCTRL_account.SetValue('')
    150                  self._PRW_user_role.SetText(value = u'', data = None)
    151                  self._TCTRL_comment.SetValue('')
    152          #--------------------------------------------------------
    153 -        def _on_activate_button_pressed(self, evt):
    154                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    155                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Activating GNUmed user.'))
    156                  if conn is None:
    157                          return False
    158                  gmStaff.activate_staff(conn = conn, pk_staff = pk_staff)
    159                  conn.close()
    160                  self.__init_ui_data()
    161                  return True
    162          #--------------------------------------------------------
    163 -        def _on_deactivate_button_pressed(self, evt):
    164                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    165                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Deactivating GNUmed user.'))
    166                  if conn is None:
    167                          return False
    168                  gmStaff.deactivate_staff(conn = conn, pk_staff = pk_staff)
    169                  conn.close()
    170                  self.__init_ui_data()
    171                  return True
    172          #--------------------------------------------------------
    173 -        def _on_delete_button_pressed(self, event):
    174                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    175                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Removing GNUmed user.'))
    176                  if conn is None:
    177                          return False
    178                  success, msg = gmStaff.delete_staff(conn = conn, pk_staff = pk_staff)
    179                  conn.close()
    180                  self.__init_ui_data()
    181                  if not success:
    182                          gmGuiHelpers.gm_show_error(aMessage = msg, aTitle = _('Removing GNUmed user'))
    183                          return False
    184                  return True
    185          #--------------------------------------------------------
    186 -        def _on_save_button_pressed(self, event):
    187                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    188
    189                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Modifying GNUmed user.'))
    190                  if conn is None:
    191                          return False
    192
    193                  staff = gmStaff.cStaff(aPK_obj = pk_staff)
    194                  staff['short_alias'] = self._TCTRL_alias.GetValue()
    195                  staff['db_user'] = self._TCTRL_account.GetValue()
    196                  staff['comment'] = self._TCTRL_comment.GetValue()
    197                  success, data = staff.save_payload(conn = conn)
    198                  if not success:
    199                          conn.close()
    200                          gmGuiHelpers.gm_show_error (
    201                                  aMessage = _('Failed to save changes to GNUmed database user.'),
    202                                  aTitle = _('Modifying GNUmed user')
    203                          )
    204                          return False
    205
    206                  target_role = self._PRW_user_role.GetData()
    207                  if target_role is not None:
    208                          if not staff.set_role(conn = conn, role = target_role):
    209                                  gmGuiHelpers.gm_show_error (
    210                                          aMessage = _('Failed to set role [%s] for GNUmed database user.') % self._PRW_user_role.GetValue().strip(),
    211                                          aTitle = _('Modifying GNUmed user')
    212                                  )
    213
    214                  conn.close()
    215                  self.__init_ui_data()
    216                  return True
    217  #==========================================================================
    218  from Gnumed.wxGladeWidgets import wxgAddPatientAsStaffDlg
    219
    220 -class cAddPatientAsStaffDlg(wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg):
    221
    222 -        def __init__(self, *args, **kwds):
    223                  wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg.__init__(self, *args, **kwds)
    224                  self.__init_ui_data()
    225          #--------------------------------------------------------
    226          # internal API
    227          #--------------------------------------------------------
    228 -        def __init_ui_data(self):
    229                  pat = gmPerson.gmCurrentPatient()
    230                  name = pat.get_active_name()
    231                  txt = _("""
    232    %s "%s" %s
    233    born: %s""") % (name['firstnames'], name['preferred'], name['lastnames'], pat.get_formatted_dob(format = '%Y %b %d', encoding = gmI18N.get_encoding()))
    234                  self._TXT_person.SetValue(txt)
    235                  txt = name['firstnames'][:2] + name['lastnames'][:2]
    236                  self._TXT_short_alias.SetValue(txt)
    237                  self._TXT_account.SetValue(txt.lower())
    238          #--------------------------------------------------------
    239          # event handlers
    240          #--------------------------------------------------------
    241 -        def _on_cancel_button_pressed(self, evt):
    242                  self.Close()
    243          #--------------------------------------------------------
    244 -        def _on_enlist_button_pressed(self, evt):
    245                  # sanity checks
    246                  if self._TXT_password.GetValue() != self._TXT_password_again.GetValue():
    247                          gmGuiHelpers.gm_show_error (
    248                                  aMessage = _('Password entries do not match. Please type in the passwords again to rule out typos.'),
    249                                  aTitle = _('Adding GNUmed user')
    250                          )
    251                          self._TXT_password.SetValue('')
    252                          self._TXT_password_again.SetValue('')
    253                          return False
    254
    255                  if self._TXT_password.GetValue().strip() == u'':
    256                          really_wants_empty_password = gmGuiHelpers.gm_show_question (
    257                                  aMessage = _(
    258                                          'Are you positively sure you want to create\n'
    259                                          'a user with an empty password ?\n'
    260                                          '\n'
    261                                          'Think about the record access implications !'
    262                                  ),
    263                                  aTitle = _('Adding GNUmed user')
    264                          )
    265                          if not really_wants_empty_password:
    266                                  return False
    267
    268                  # connect as "gm-dbo"
    269                  conn = gmAuthWidgets.get_dbowner_connection (
    270                          procedure = _('Enlisting person as user.'),
    271                          dbo_password = gmTools.none_if(self._TXT_dbo_password.GetValue(), u'')
    272                  )
    273                  if conn is None:
    274                          return False
    275
    276                  # create new user
    277                  success, msg = gmStaff.create_staff (
    278                          conn = conn,
    279                          db_account = self._TXT_account.GetValue(),
    280                          password = self._TXT_password.GetValue(),
    281                          identity = gmPerson.gmCurrentPatient().ID,
    282                          short_alias = self._TXT_short_alias.GetValue().strip()
    283                  )
    284                  conn.close()
    285                  if not success:
    286                          gmGuiHelpers.gm_show_error(aMessage = msg, aTitle = _('Adding GNUmed user'))
    287                          return False
    288
    289                  if self.IsModal():
    290                          self.EndModal(wx.ID_OK)
    291                  else:
    292                          self.Close()
    293
    294  #==========================================================================
    295