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  import wx
      9
     10  from Gnumed.pycommon import gmTools, gmI18N
     11  from Gnumed.business import gmPerson
     12  from Gnumed.business import gmStaff
     13  from Gnumed.wxpython import gmGuiHelpers, gmAuthWidgets
     14  from Gnumed.wxGladeWidgets import wxgAddPatientAsStaffDlg, wxgEditStaffListDlg
     15
     16  _log = logging.getLogger('gm.ui')
     17  #==========================================================================
     18 -class cEditStaffListDlg(wxgEditStaffListDlg.wxgEditStaffListDlg):
     19
     20 -        def __init__(self, *args, **kwds):
     21                  wxgEditStaffListDlg.wxgEditStaffListDlg.__init__(self, *args, **kwds)
     22
     23                  self._LCTRL_staff.InsertColumn(0, _('Alias'))
     24                  self._LCTRL_staff.InsertColumn(1, _('DB account'))
     25                  self._LCTRL_staff.InsertColumn(2, _('Role'))
     26                  self._LCTRL_staff.InsertColumn(3, _('Name'))
     27                  self._LCTRL_staff.InsertColumn(4, _('Comment'))
     28                  self._LCTRL_staff.InsertColumn(5, _('Status'))
     29
     30                  self.__init_ui_data()
     31          #--------------------------------------------------------
     32          # internal API
     33          #--------------------------------------------------------
     34 -        def __init_ui_data(self):
     35                  lbl_active = {True: _('active'), False: _('inactive')}
     36                  lbl_login = {True: _('can login'), False: _('can not login')}
     37
     38                  self._LCTRL_staff.DeleteAllItems()
     39                  staff_list = gmStaff.get_staff_list()
     40                  pos = len(staff_list) + 1
     41                  for staff in staff_list:
     42                          row_num = self._LCTRL_staff.InsertStringItem(pos, label=staff['short_alias'])
     43                          self._LCTRL_staff.SetStringItem(index = row_num, col = 1, label = staff['db_user'])
     44                          self._LCTRL_staff.SetStringItem(index = row_num, col = 2, label = staff['l10n_role'])
     45                          title = gmTools.coalesce(staff['title'], '')
     46                          self._LCTRL_staff.SetStringItem(index = row_num, col = 3, label = '%s %s, %s' % (title, staff['lastnames'], staff['firstnames']))
     47                          self._LCTRL_staff.SetStringItem(index = row_num, col = 4, label = gmTools.coalesce(staff['comment'], ''))
     48                          self._LCTRL_staff.SetStringItem(index = row_num, col = 5, label = '%s / %s' % (lbl_active[bool(staff['is_active'])], lbl_login[bool(staff['can_login'])]))
     49                          # color
     50                          if staff['is_active'] and staff['can_login']:
     51                                  #self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('BLUE'))
     52                                  pass
     53                          elif not staff['is_active'] and not staff['can_login']:
     54                                  self._LCTRL_staff.SetItemTextColour(row_num, col=wx.LIGHT_GREY)
     55                          else:
     56                                  self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('RED'))
     57                          # data
     58                          self._LCTRL_staff.SetItemData(item = row_num, data = staff['pk_staff'])
     59
     60                  if len(staff_list) > 0:
     61                          self._LCTRL_staff.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE)
     62                          self._LCTRL_staff.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE_USEHEADER)
     63                          self._LCTRL_staff.SetColumnWidth(col=2, width=wx.LIST_AUTOSIZE)
     64                          self._LCTRL_staff.SetColumnWidth(col=3, width=wx.LIST_AUTOSIZE)
     65                          self._LCTRL_staff.SetColumnWidth(col=4, width=wx.LIST_AUTOSIZE)
     66                          self._LCTRL_staff.SetColumnWidth(col=5, width=wx.LIST_AUTOSIZE)
     67
     68                  # disable buttons
     69                  self._btn_save.Enable(False)
     70                  self._btn_delete.Enable(False)
     71                  self._btn_deactivate.Enable(False)
     72                  self._btn_activate.Enable(False)
     73                  # clear editor
     74                  self._TCTRL_name.SetValue('')
     75                  self._TCTRL_alias.SetValue('')
     76                  self._TCTRL_account.SetValue('')
     77                  self._TCTRL_comment.SetValue('')
     78          #--------------------------------------------------------
     79          # event handlers
     80          #--------------------------------------------------------
     81 -        def _on_listitem_selected(self, evt):
     82                  self._btn_save.Enable(True)
     83                  self._btn_delete.Enable(True)
     84                  self._btn_deactivate.Enable(True)
     85                  self._btn_activate.Enable(True)
     86                  # fill editor
     87                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
     88                  staff = gmStaff.cStaff(aPK_obj=pk_staff)
     89                  self._TCTRL_name.SetValue('%s.%s %s' % (staff['title'], staff['firstnames'], staff['lastnames']))
     90                  self._TCTRL_alias.SetValue(staff['short_alias'])
     91                  self._TCTRL_account.SetValue(staff['db_user'])
     92                  self._TCTRL_comment.SetValue(gmTools.coalesce(staff['comment'], ''))
     93          #--------------------------------------------------------
     94 -        def _on_listitem_deselected(self, evt):
     95                  self._btn_save.Enable(False)
     96                  self._btn_delete.Enable(False)
     97                  self._btn_deactivate.Enable(False)
     98                  self._btn_activate.Enable(False)
     99                  # clear editor
    100                  self._TCTRL_name.SetValue('')
    101                  self._TCTRL_alias.SetValue('')
    102                  self._TCTRL_account.SetValue('')
    103                  self._TCTRL_comment.SetValue('')
    104          #--------------------------------------------------------
    105 -        def _on_activate_button_pressed(self, evt):
    106                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    107                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Activating GNUmed user.'))
    108                  if conn is None:
    109                          return False
    110                  gmStaff.activate_staff(conn = conn, pk_staff = pk_staff)
    111                  conn.close()
    112                  self.__init_ui_data()
    113                  return True
    114          #--------------------------------------------------------
    115 -        def _on_deactivate_button_pressed(self, evt):
    116                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    117                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Deactivating GNUmed user.'))
    118                  if conn is None:
    119                          return False
    120                  gmStaff.deactivate_staff(conn = conn, pk_staff = pk_staff)
    121                  conn.close()
    122                  self.__init_ui_data()
    123                  return True
    124          #--------------------------------------------------------
    125 -        def _on_delete_button_pressed(self, event):
    126                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    127                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Removing GNUmed user.'))
    128                  if conn is None:
    129                          return False
    130                  success, msg = gmStaff.delete_staff(conn = conn, pk_staff = pk_staff)
    131                  conn.close()
    132                  self.__init_ui_data()
    133                  if not success:
    134                          gmGuiHelpers.gm_show_error(aMessage = msg, aTitle = _('Removing GNUmed user'))
    135                          return False
    136                  return True
    137          #--------------------------------------------------------
    138 -        def _on_save_button_pressed(self, event):
    139                  pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected())
    140
    141                  conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Modifying GNUmed user.'))
    142                  if conn is None:
    143                          return False
    144
    145                  staff = gmStaff.cStaff(aPK_obj=pk_staff)
    146                  staff['short_alias'] = self._TCTRL_alias.GetValue()
    147                  staff['db_user'] = self._TCTRL_account.GetValue()
    148                  staff['comment'] = self._TCTRL_comment.GetValue()
    149                  success, data = staff.save_payload(conn=conn)
    150                  conn.close()
    151                  if not success:
    152                          gmGuiHelpers.gm_show_error (
    153                                  aMessage = _('Failed to save changes to GNUmed database user.'),
    154                                  aTitle = _('Modifying GNUmed user')
    155                          )
    156                          return False
    157
    158                  self.__init_ui_data()
    159                  return True
    160  #==========================================================================
    161 -class cAddPatientAsStaffDlg(wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg):
    162
    163 -        def __init__(self, *args, **kwds):
    164                  wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg.__init__(self, *args, **kwds)
    165                  self.__init_ui_data()
    166          #--------------------------------------------------------
    167          # internal API
    168          #--------------------------------------------------------
    169 -        def __init_ui_data(self):
    170                  pat = gmPerson.gmCurrentPatient()
    171                  name = pat.get_active_name()
    172                  txt = _("""
    173    %s "%s" %s
    174    born: %s""") % (name['firstnames'], name['preferred'], name['lastnames'], pat.get_formatted_dob(format = '%x', encoding = gmI18N.get_encoding()))
    175                  self._TXT_person.SetValue(txt)
    176                  txt = name['firstnames'][:2] + name['lastnames'][:2]
    177                  self._TXT_short_alias.SetValue(txt)
    178                  self._TXT_account.SetValue(txt.lower())
    179          #--------------------------------------------------------
    180          # event handlers
    181          #--------------------------------------------------------
    182 -        def _on_cancel_button_pressed(self, evt):
    183                  self.Close()
    184          #--------------------------------------------------------
    185 -        def _on_enlist_button_pressed(self, evt):
    186                  # sanity checks
    187                  if self._TXT_password.GetValue() != self._TXT_password_again.GetValue():
    188                          gmGuiHelpers.gm_show_error (
    189                                  aMessage = _('Password entries do not match. Please type in the passwords again to rule out typos.'),
    190                                  aTitle = _('Adding GNUmed user')
    191                          )
    192                          self._TXT_password.SetValue('')
    193                          self._TXT_password_again.SetValue('')
    194                          return False
    195
    196                  if self._TXT_password.GetValue().strip() == u'':
    197                          really_wants_empty_password = gmGuiHelpers.gm_show_question (
    198                                  aMessage = _(
    199                                          'Are you positively sure you want to create\n'
    200                                          'a user with an empty password ?\n'
    201                                          '\n'
    202                                          'Think about the record access implications !'
    203                                  ),
    204                                  aTitle = _('Adding GNUmed user')
    205                          )
    206                          if not really_wants_empty_password:
    207                                  return False
    208
    209                  # connect as "gm-dbo"
    210                  conn = gmAuthWidgets.get_dbowner_connection (
    211                          procedure = _('Enlisting person as user.'),
    212                          dbo_password = gmTools.none_if(self._TXT_dbo_password.GetValue(), u'')
    213                  )
    214                  if conn is None:
    215                          return False
    216
    217                  # create new user
    218                  success, msg = gmStaff.create_staff (
    219                          conn = conn,
    220                          db_account = self._TXT_account.GetValue(),
    221                          password = self._TXT_password.GetValue(),
    222                          identity = gmPerson.gmCurrentPatient().ID,
    223                          short_alias = self._TXT_short_alias.GetValue().strip()
    224                  )
    225                  conn.close()
    226                  if not success:
    227                          gmGuiHelpers.gm_show_error(aMessage = msg, aTitle = _('Adding GNUmed user'))
    228                          return False
    229
    230                  if self.IsModal():
    231                          self.EndModal(wx.ID_OK)
    232                  else:
    233                          self.Close()
    234  #==========================================================================
    235