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

Source Code for Module Gnumed.wxpython.gmStaffWidgets

  1  """GNUmed staff management widgets.""" 
  2  #========================================================================= 
  3  # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/wxpython/gmStaffWidgets.py,v $ 
  4  # $Id: gmStaffWidgets.py,v 1.27 2010-01-31 18:20:03 ncq Exp $ 
  5  __version__ = "$Revision: 1.27 $" 
  6  __author__  = "K. Hilbert <Karsten.Hilbert@gmx.net>" 
  7  __license__ = "GPL v2 or later (details at http://www.gnu.org)" 
  8   
  9  import logging 
 10   
 11  import wx 
 12   
 13  from Gnumed.pycommon import gmPG2, gmTools, gmI18N 
 14  from Gnumed.business import gmPerson 
 15  from Gnumed.wxpython import gmGuiHelpers, gmAuthWidgets 
 16  from Gnumed.wxGladeWidgets import wxgAddPatientAsStaffDlg, wxgEditStaffListDlg 
 17   
 18  _log = logging.getLogger('gm.ui') 
 19  _log.info(__version__) 
 20  #========================================================================== 
21 -class cEditStaffListDlg(wxgEditStaffListDlg.wxgEditStaffListDlg):
22
23 - def __init__(self, *args, **kwds):
24 wxgEditStaffListDlg.wxgEditStaffListDlg.__init__(self, *args, **kwds) 25 26 self._LCTRL_staff.InsertColumn(0, _('Alias')) 27 self._LCTRL_staff.InsertColumn(1, _('DB account')) 28 self._LCTRL_staff.InsertColumn(2, _('Role')) 29 self._LCTRL_staff.InsertColumn(3, _('Name')) 30 self._LCTRL_staff.InsertColumn(4, _('Comment')) 31 self._LCTRL_staff.InsertColumn(5, _('Status')) 32 33 self.__init_ui_data()
34 #-------------------------------------------------------- 35 # internal API 36 #--------------------------------------------------------
37 - def __init_ui_data(self):
38 lbl_active = {True: _('active'), False: _('inactive')} 39 lbl_login = {True: _('can login'), False: _('can not login')} 40 41 self._LCTRL_staff.DeleteAllItems() 42 staff_list = gmPerson.get_staff_list() 43 pos = len(staff_list) + 1 44 for staff in staff_list: 45 row_num = self._LCTRL_staff.InsertStringItem(pos, label=staff['short_alias']) 46 self._LCTRL_staff.SetStringItem(index = row_num, col = 1, label = staff['db_user']) 47 self._LCTRL_staff.SetStringItem(index = row_num, col = 2, label = staff['l10n_role']) 48 title = gmTools.coalesce(staff['title'], '') 49 self._LCTRL_staff.SetStringItem(index = row_num, col = 3, label = '%s %s, %s' % (title, staff['lastnames'], staff['firstnames'])) 50 self._LCTRL_staff.SetStringItem(index = row_num, col = 4, label = gmTools.coalesce(staff['comment'], '')) 51 self._LCTRL_staff.SetStringItem(index = row_num, col = 5, label = '%s / %s' % (lbl_active[bool(staff['is_active'])], lbl_login[bool(staff['can_login'])])) 52 # color 53 if staff['is_active'] and staff['can_login']: 54 #self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('BLUE')) 55 pass 56 elif not staff['is_active'] and not staff['can_login']: 57 self._LCTRL_staff.SetItemTextColour(row_num, col=wx.LIGHT_GREY) 58 else: 59 self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('RED')) 60 # data 61 self._LCTRL_staff.SetItemData(item = row_num, data = staff['pk_staff']) 62 63 if len(staff_list) > 0: 64 self._LCTRL_staff.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE) 65 self._LCTRL_staff.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE_USEHEADER) 66 self._LCTRL_staff.SetColumnWidth(col=2, width=wx.LIST_AUTOSIZE) 67 self._LCTRL_staff.SetColumnWidth(col=3, width=wx.LIST_AUTOSIZE) 68 self._LCTRL_staff.SetColumnWidth(col=4, width=wx.LIST_AUTOSIZE) 69 self._LCTRL_staff.SetColumnWidth(col=5, width=wx.LIST_AUTOSIZE) 70 71 # disable buttons 72 self._btn_save.Enable(False) 73 self._btn_delete.Enable(False) 74 self._btn_deactivate.Enable(False) 75 self._btn_activate.Enable(False) 76 # clear editor 77 self._TCTRL_name.SetValue('') 78 self._TCTRL_alias.SetValue('') 79 self._TCTRL_account.SetValue('') 80 self._TCTRL_comment.SetValue('')
81 #-------------------------------------------------------- 82 # event handlers 83 #--------------------------------------------------------
84 - def _on_listitem_selected(self, evt):
85 self._btn_save.Enable(True) 86 self._btn_delete.Enable(True) 87 self._btn_deactivate.Enable(True) 88 self._btn_activate.Enable(True) 89 # fill editor 90 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 91 staff = gmPerson.cStaff(aPK_obj=pk_staff) 92 self._TCTRL_name.SetValue('%s.%s %s' % (staff['title'], staff['firstnames'], staff['lastnames'])) 93 self._TCTRL_alias.SetValue(staff['short_alias']) 94 self._TCTRL_account.SetValue(staff['db_user']) 95 self._TCTRL_comment.SetValue(gmTools.coalesce(staff['comment'], ''))
96 #--------------------------------------------------------
97 - def _on_listitem_deselected(self, evt):
98 self._btn_save.Enable(False) 99 self._btn_delete.Enable(False) 100 self._btn_deactivate.Enable(False) 101 self._btn_activate.Enable(False) 102 # clear editor 103 self._TCTRL_name.SetValue('') 104 self._TCTRL_alias.SetValue('') 105 self._TCTRL_account.SetValue('') 106 self._TCTRL_comment.SetValue('')
107 #--------------------------------------------------------
108 - def _on_activate_button_pressed(self, evt):
109 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 110 111 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Activating GNUmed user.')) 112 if conn is None: 113 return False 114 115 # 1) activate staff entry 116 staff = gmPerson.cStaff(aPK_obj=pk_staff) 117 staff['is_active'] = True 118 staff.save_payload(conn=conn) # FIXME: error handling 119 120 # 2) enable database account login 121 rowx, idx = gmPG2.run_rw_queries ( 122 link_obj = conn, 123 queries = [{'cmd': u'select gm.create_user(%s, %s)', 'args': [staff['db_user'], 'flying wombat']}], 124 end_tx = True 125 ) 126 conn.close() 127 self.__init_ui_data() 128 return True
129 #--------------------------------------------------------
130 - def _on_deactivate_button_pressed(self, evt):
131 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 132 133 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Deactivating GNUmed user.')) 134 if conn is None: 135 return False 136 137 # 1) inactivate staff entry 138 staff = gmPerson.cStaff(aPK_obj=pk_staff) 139 staff['is_active'] = False 140 staff.save_payload(conn=conn) # FIXME: error handling 141 142 # 2) disable database account login 143 rows, idx = gmPG2.run_rw_queries ( 144 link_obj = conn, 145 queries = [{'cmd': u'select gm.disable_user(%s)', 'args': [staff['db_user']]}], 146 end_tx = True 147 ) 148 conn.close() 149 self.__init_ui_data() 150 return True
151 #-------------------------------------------------------- 152 # def _on_delete_button_pressed(self, event): 153 #--------------------------------------------------------
154 - def _on_save_button_pressed(self, event):
155 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 156 157 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Modifying GNUmed user.')) 158 if conn is None: 159 return False 160 161 staff = gmPerson.cStaff(aPK_obj=pk_staff) 162 staff['short_alias'] = self._TCTRL_alias.GetValue() 163 staff['db_user'] = self._TCTRL_account.GetValue() 164 staff['comment'] = self._TCTRL_comment.GetValue() 165 success, data = staff.save_payload(conn=conn) 166 conn.close() 167 if not success: 168 gmGuiHelpers.gm_show_error ( 169 aMessage = _('Failed to save changes to GNUmed database user.'), 170 aTitle = _('Modifying GNUmed user') 171 ) 172 return False 173 174 self.__init_ui_data() 175 return True
176 #==========================================================================
177 -class cAddPatientAsStaffDlg(wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg):
178
179 - def __init__(self, *args, **kwds):
180 wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg.__init__(self, *args, **kwds) 181 self.__init_ui_data()
182 #-------------------------------------------------------- 183 # internal API 184 #--------------------------------------------------------
185 - def __init_ui_data(self):
186 pat = gmPerson.gmCurrentPatient() 187 name = pat.get_active_name() 188 txt = _(""" 189 %s "%s" %s 190 born: %s""") % (name['firstnames'], name['preferred'], name['lastnames'], pat.get_formatted_dob(format = '%x', encoding = gmI18N.get_encoding())) 191 self._TXT_person.SetValue(txt) 192 txt = name['firstnames'][:2] + name['lastnames'][:2] 193 self._TXT_short_alias.SetValue(txt) 194 self._TXT_account.SetValue(txt.lower())
195 #-------------------------------------------------------- 196 # event handlers 197 #--------------------------------------------------------
198 - def _on_cancel_button_pressed(self, evt):
199 self.Close()
200 #--------------------------------------------------------
201 - def _on_enlist_button_pressed(self, evt):
202 # sanity checks 203 if self._TXT_password.GetValue() != self._TXT_password_again.GetValue(): 204 gmGuiHelpers.gm_show_error ( 205 aMessage = _('Password entries do not match. Please type in the passwords again to rule out typos.'), 206 aTitle = _('Adding GNUmed user') 207 ) 208 self._TXT_password.SetValue('') 209 self._TXT_password_again.SetValue('') 210 return False 211 212 if self._TXT_password.GetValue().strip() == u'': 213 really_wants_empty_password = gmGuiHelpers.gm_show_question ( 214 aMessage = _( 215 'Are you positively sure you want to create\n' 216 'a user with an empty password ?\n' 217 '\n' 218 'Think about the record access implications !' 219 ), 220 aTitle = _('Adding GNUmed user') 221 ) 222 if not really_wants_empty_password: 223 return False 224 225 # connect as "gm-dbo" 226 conn = gmAuthWidgets.get_dbowner_connection ( 227 procedure = _('Enlisting person as user.'), 228 dbo_password = gmTools.none_if(self._TXT_dbo_password.GetValue(), u'') 229 ) 230 if conn is None: 231 return False 232 233 # create new user 234 pat = gmPerson.gmCurrentPatient() 235 db_account = self._TXT_account.GetValue() 236 queries = [ 237 # database account 238 {'cmd': u'select gm.create_user(%s, %s)', 'args': [db_account, self._TXT_password.GetValue()]}, 239 # staff entry 240 { 241 'cmd': u"insert into dem.staff (fk_identity, fk_role, db_user, short_alias) values (%s, (select pk from dem.staff_role where name='doctor'), %s, %s)", 242 'args': [pat.ID, db_account, self._TXT_short_alias.GetValue().strip()] 243 } 244 ] 245 try: 246 rows, idx = gmPG2.run_rw_queries(link_obj = conn, queries = queries, end_tx = True) 247 except gmPG2.dbapi.IntegrityError, e: 248 if e.pgcode == gmPG2.sql_error_codes.UNIQUE_VIOLATION: 249 gmGuiHelpers.gm_show_error ( 250 aMessage = _( 251 'Cannot add GNUmed user.\n' 252 '\n' 253 'The database account [%s] is already listed as a\n' 254 'GNUmed user. There can only be one GNUmed user\n' 255 'for each database account.\n' 256 ) % db_account, 257 aTitle = _('Adding GNUmed user') 258 ) 259 return False 260 raise 261 262 if self.IsModal(): 263 self.EndModal(wx.ID_OK) 264 else: 265 self.Close()
266 #========================================================================== 267