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

Source Code for Module Gnumed.wxpython.gmStaffWidgets

  1  """GNUmed staff management widgets. 
  2   
  3  This source code is protected by the GPL licensing scheme. 
  4  Details regarding the GPL are available at http://www.gnu.org 
  5  You may use and share it as long as you don't deny this right 
  6  to anybody else. 
  7  """ 
  8  #========================================================================= 
  9  # $Source: /cvsroot/gnumed/gnumed/gnumed/client/wxpython/gmStaffWidgets.py,v $ 
 10  # $Id: gmStaffWidgets.py,v 1.26 2009/07/23 16:42:38 ncq Exp $ 
 11  __version__ = "$Revision: 1.26 $" 
 12  __author__  = "K. Hilbert <Karsten.Hilbert@gmx.net>" 
 13  __license__ = "GPL (details at http://www.gnu.org)" 
 14   
 15  import logging 
 16   
 17  import wx 
 18   
 19  from Gnumed.pycommon import gmPG2, gmTools, gmI18N 
 20  from Gnumed.business import gmPerson 
 21  from Gnumed.wxpython import gmGuiHelpers, gmAuthWidgets 
 22  from Gnumed.wxGladeWidgets import wxgAddPatientAsStaffDlg, wxgEditStaffListDlg 
 23   
 24  _log = logging.getLogger('gm.ui') 
 25  _log.info(__version__) 
 26  #========================================================================== 
27 -class cEditStaffListDlg(wxgEditStaffListDlg.wxgEditStaffListDlg):
28
29 - def __init__(self, *args, **kwds):
30 wxgEditStaffListDlg.wxgEditStaffListDlg.__init__(self, *args, **kwds) 31 32 self._LCTRL_staff.InsertColumn(0, _('Alias')) 33 self._LCTRL_staff.InsertColumn(1, _('DB account')) 34 self._LCTRL_staff.InsertColumn(2, _('Role')) 35 self._LCTRL_staff.InsertColumn(3, _('Name')) 36 self._LCTRL_staff.InsertColumn(4, _('Comment')) 37 self._LCTRL_staff.InsertColumn(5, _('Status')) 38 39 self.__init_ui_data()
40 #-------------------------------------------------------- 41 # internal API 42 #--------------------------------------------------------
43 - def __init_ui_data(self):
44 lbl_active = {True: _('active'), False: _('inactive')} 45 lbl_login = {True: _('can login'), False: _('can not login')} 46 47 self._LCTRL_staff.DeleteAllItems() 48 staff_list = gmPerson.get_staff_list() 49 pos = len(staff_list) + 1 50 for staff in staff_list: 51 row_num = self._LCTRL_staff.InsertStringItem(pos, label=staff['short_alias']) 52 self._LCTRL_staff.SetStringItem(index = row_num, col = 1, label = staff['db_user']) 53 self._LCTRL_staff.SetStringItem(index = row_num, col = 2, label = staff['role']) 54 title = gmTools.coalesce(staff['title'], '') 55 self._LCTRL_staff.SetStringItem(index = row_num, col = 3, label = '%s %s, %s' % (title, staff['lastnames'], staff['firstnames'])) 56 self._LCTRL_staff.SetStringItem(index = row_num, col = 4, label = gmTools.coalesce(staff['comment'], '')) 57 self._LCTRL_staff.SetStringItem(index = row_num, col = 5, label = '%s / %s' % (lbl_active[bool(staff['is_active'])], lbl_login[bool(staff['can_login'])])) 58 # color 59 if staff['is_active'] and staff['can_login']: 60 #self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('BLUE')) 61 pass 62 elif not staff['is_active'] and not staff['can_login']: 63 self._LCTRL_staff.SetItemTextColour(row_num, col=wx.LIGHT_GREY) 64 else: 65 self._LCTRL_staff.SetItemTextColour(row_num, col=wx.NamedColour('RED')) 66 # data 67 self._LCTRL_staff.SetItemData(item = row_num, data = staff['pk_staff']) 68 69 if len(staff_list) > 0: 70 self._LCTRL_staff.SetColumnWidth(col=0, width=wx.LIST_AUTOSIZE) 71 self._LCTRL_staff.SetColumnWidth(col=1, width=wx.LIST_AUTOSIZE_USEHEADER) 72 self._LCTRL_staff.SetColumnWidth(col=2, width=wx.LIST_AUTOSIZE) 73 self._LCTRL_staff.SetColumnWidth(col=3, width=wx.LIST_AUTOSIZE) 74 self._LCTRL_staff.SetColumnWidth(col=4, width=wx.LIST_AUTOSIZE) 75 self._LCTRL_staff.SetColumnWidth(col=5, width=wx.LIST_AUTOSIZE) 76 77 # disable buttons 78 self._btn_save.Enable(False) 79 self._btn_delete.Enable(False) 80 self._btn_deactivate.Enable(False) 81 self._btn_activate.Enable(False) 82 # clear editor 83 self._TCTRL_name.SetValue('') 84 self._TCTRL_alias.SetValue('') 85 self._TCTRL_account.SetValue('') 86 self._TCTRL_comment.SetValue('')
87 #-------------------------------------------------------- 88 # event handlers 89 #--------------------------------------------------------
90 - def _on_listitem_selected(self, evt):
91 self._btn_save.Enable(True) 92 self._btn_delete.Enable(True) 93 self._btn_deactivate.Enable(True) 94 self._btn_activate.Enable(True) 95 # fill editor 96 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 97 staff = gmPerson.cStaff(aPK_obj=pk_staff) 98 self._TCTRL_name.SetValue('%s.%s %s' % (staff['title'], staff['firstnames'], staff['lastnames'])) 99 self._TCTRL_alias.SetValue(staff['short_alias']) 100 self._TCTRL_account.SetValue(staff['db_user']) 101 self._TCTRL_comment.SetValue(gmTools.coalesce(staff['comment'], ''))
102 #--------------------------------------------------------
103 - def _on_listitem_deselected(self, evt):
104 self._btn_save.Enable(False) 105 self._btn_delete.Enable(False) 106 self._btn_deactivate.Enable(False) 107 self._btn_activate.Enable(False) 108 # clear editor 109 self._TCTRL_name.SetValue('') 110 self._TCTRL_alias.SetValue('') 111 self._TCTRL_account.SetValue('') 112 self._TCTRL_comment.SetValue('')
113 #--------------------------------------------------------
114 - def _on_activate_button_pressed(self, evt):
115 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 116 117 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Activating GNUmed user.')) 118 if conn is None: 119 return False 120 121 # 1) activate staff entry 122 staff = gmPerson.cStaff(aPK_obj=pk_staff) 123 staff['is_active'] = True 124 staff.save_payload(conn=conn) # FIXME: error handling 125 126 # 2) enable database account login 127 rowx, idx = gmPG2.run_rw_queries ( 128 link_obj = conn, 129 queries = [{'cmd': u'select gm.create_user(%s, %s)', 'args': [staff['db_user'], 'flying wombat']}], 130 end_tx = True 131 ) 132 conn.close() 133 self.__init_ui_data() 134 return True
135 #--------------------------------------------------------
136 - def _on_deactivate_button_pressed(self, evt):
137 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 138 139 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Deactivating GNUmed user.')) 140 if conn is None: 141 return False 142 143 # 1) inactivate staff entry 144 staff = gmPerson.cStaff(aPK_obj=pk_staff) 145 staff['is_active'] = False 146 staff.save_payload(conn=conn) # FIXME: error handling 147 148 # 2) disable database account login 149 rows, idx = gmPG2.run_rw_queries ( 150 link_obj = conn, 151 queries = [{'cmd': u'select gm.disable_user(%s)', 'args': [staff['db_user']]}], 152 end_tx = True 153 ) 154 conn.close() 155 self.__init_ui_data() 156 return True
157 #-------------------------------------------------------- 158 # def _on_delete_button_pressed(self, event): 159 #--------------------------------------------------------
160 - def _on_save_button_pressed(self, event):
161 pk_staff = self._LCTRL_staff.GetItemData(self._LCTRL_staff.GetFirstSelected()) 162 163 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('Modifying GNUmed user.')) 164 if conn is None: 165 return False 166 167 staff = gmPerson.cStaff(aPK_obj=pk_staff) 168 staff['short_alias'] = self._TCTRL_alias.GetValue() 169 staff['db_user'] = self._TCTRL_account.GetValue() 170 staff['comment'] = self._TCTRL_comment.GetValue() 171 success, data = staff.save_payload(conn=conn) 172 conn.close() 173 if not success: 174 gmGuiHelpers.gm_show_error ( 175 aMessage = _('Failed to save changes to GNUmed database user.'), 176 aTitle = _('Modifying GNUmed user') 177 ) 178 return False 179 180 self.__init_ui_data() 181 return True
182 #==========================================================================
183 -class cAddPatientAsStaffDlg(wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg):
184
185 - def __init__(self, *args, **kwds):
186 wxgAddPatientAsStaffDlg.wxgAddPatientAsStaffDlg.__init__(self, *args, **kwds) 187 self.__init_ui_data()
188 #-------------------------------------------------------- 189 # internal API 190 #--------------------------------------------------------
191 - def __init_ui_data(self):
192 pat = gmPerson.gmCurrentPatient() 193 name = pat.get_active_name() 194 txt = _(""" 195 %s "%s" %s 196 born: %s""") % (name['firstnames'], name['preferred'], name['lastnames'], pat.get_formatted_dob(format = '%x', encoding = gmI18N.get_encoding())) 197 self._TXT_person.SetValue(txt) 198 txt = name['firstnames'][:2] + name['lastnames'][:2] 199 self._TXT_short_alias.SetValue(txt) 200 self._TXT_account.SetValue(txt.lower())
201 #-------------------------------------------------------- 202 # event handlers 203 #--------------------------------------------------------
204 - def _on_cancel_button_pressed(self, evt):
205 self.Close()
206 #--------------------------------------------------------
207 - def _on_enlist_button_pressed(self, evt):
208 # sanity checks 209 if self._TXT_password.GetValue() != self._TXT_password_again.GetValue(): 210 gmGuiHelpers.gm_show_error ( 211 aMessage = _('Password entries do not match. Please type in the passwords again to rule out typos.'), 212 aTitle = _('Adding GNUmed user') 213 ) 214 self._TXT_password.SetValue('') 215 self._TXT_password_again.SetValue('') 216 return False 217 218 # connect as "gm-dbo" 219 conn = gmAuthWidgets.get_dbowner_connection ( 220 procedure = _('Enlisting person as user.'), 221 dbo_password = gmTools.none_if(self._TXT_dbo_password.GetValue(), u'') 222 ) 223 if conn is None: 224 return False 225 226 # create new user 227 pat = gmPerson.gmCurrentPatient() 228 db_account = self._TXT_account.GetValue() 229 queries = [ 230 # database account 231 {'cmd': u'select gm.create_user(%s, %s)', 'args': [db_account, self._TXT_password.GetValue()]}, 232 # staff entry 233 { 234 '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)", 235 'args': [pat.ID, db_account, self._TXT_short_alias.GetValue().strip()] 236 } 237 ] 238 try: 239 rows, idx = gmPG2.run_rw_queries(link_obj = conn, queries = queries, end_tx = True) 240 except gmPG2.dbapi.IntegrityError, e: 241 if e.pgcode == gmPG2.sql_error_codes.UNIQUE_VIOLATION: 242 gmGuiHelpers.gm_show_error ( 243 aMessage = _( 244 'Cannot add GNUmed user.\n' 245 '\n' 246 'The database account [%s] is already listed as a\n' 247 'GNUmed user. There can only be one GNUmed user\n' 248 'for each database account.\n' 249 ) % db_account, 250 aTitle = _('Adding GNUmed user') 251 ) 252 return False 253 raise 254 255 if self.IsModal(): 256 self.EndModal(wx.ID_OK) 257 else: 258 self.Close()
259 #========================================================================== 260 # $Log: gmStaffWidgets.py,v $ 261 # Revision 1.26 2009/07/23 16:42:38 ncq 262 # - staff -> user 263 # 264 # Revision 1.25 2009/06/04 16:33:51 ncq 265 # - adjust to dob-less persons 266 # 267 # Revision 1.24 2008/12/01 12:17:17 ncq 268 # - again 269 # 270 # Revision 1.23 2008/12/01 12:15:36 ncq 271 # - gm_*_user now live in gm. 272 # 273 # Revision 1.22 2008/10/22 12:21:58 ncq 274 # - use %x in strftime where appropriate 275 # 276 # Revision 1.21 2008/08/20 14:55:33 ncq 277 # - explicitely signal error condition of database 278 # account already being in use for a staff member 279 # 280 # Revision 1.20 2008/03/05 22:30:15 ncq 281 # - new style logging 282 # 283 # Revision 1.19 2008/01/11 16:15:33 ncq 284 # - first/last -> first-/lastnames 285 # 286 # Revision 1.18 2008/01/05 16:41:27 ncq 287 # - remove logging from gm_show_*() 288 # 289 # Revision 1.17 2007/12/04 16:16:27 ncq 290 # - use gmAuthWidgets 291 # 292 # Revision 1.16 2007/04/23 01:11:51 ncq 293 # - handle gm-dbo password field 294 # 295 # Revision 1.15 2007/03/01 16:35:01 ncq 296 # - no more idents 297 # 298 # Revision 1.14 2007/02/22 17:41:13 ncq 299 # - adjust to gmPerson changes 300 # 301 # Revision 1.13 2006/12/31 16:25:43 ncq 302 # - strftime() does not take unicode 303 # 304 # Revision 1.12 2006/11/24 14:23:41 ncq 305 # - EndModal() needs wx.ID_* 306 # 307 # Revision 1.11 2006/10/31 13:30:27 ncq 308 # - use gmPG2 309 # 310 # Revision 1.10 2006/10/25 07:46:44 ncq 311 # - Format() -> strftime() since datetime.datetime does not have .Format() 312 # 313 # Revision 1.9 2006/10/25 07:21:57 ncq 314 # - no more gmPG 315 # 316 # Revision 1.8 2006/09/03 11:32:10 ncq 317 # - clean up wx import 318 # - use gmTools.coalesce() 319 # - use gmGuiHelpers.get_dbowner_connection instead of local crap copy 320 # 321 # Revision 1.7 2006/06/17 16:45:19 ncq 322 # - only insert column labels once 323 # - use get_dbowner_connection() in gmGuiHelpers 324 # - implement activate()/save() on staff details 325 # 326 # Revision 1.6 2006/06/15 20:57:49 ncq 327 # - actually do something with the improved staff list editor 328 # 329 # Revision 1.5 2006/06/10 05:13:06 ncq 330 # - improved "edit staff list" 331 # 332 # Revision 1.4 2006/06/09 14:43:02 ncq 333 # - improve staff member handling 334 # 335 # Revision 1.3 2006/06/06 20:54:36 ncq 336 # - add staff member delisting dialog 337 # 338 # Revision 1.2 2006/03/14 21:31:15 ncq 339 # - add event handlers for buttons 340 # - actually implement adding a new provider 341 # 342 # Revision 1.1 2006/03/09 21:10:14 ncq 343 # - simple wrapper around dialog to add current patient as staff member 344 # 345