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: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/wxpython/gmStaffWidgets.py,v $ 
 10  # $Id: gmStaffWidgets.py,v 1.27 2010-01-31 18:20:03 ncq Exp $ 
 11  __version__ = "$Revision: 1.27 $" 
 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 if self._TXT_password.GetValue().strip() == u'': 219 really_wants_empty_password = gmGuiHelpers.gm_show_question ( 220 aMessage = _( 221 'Are you positively sure you want to create\n' 222 'a user with an empty password ?\n' 223 '\n' 224 'Think about the record access implications !' 225 ), 226 aTitle = _('Adding GNUmed user') 227 ) 228 if not really_wants_empty_password: 229 return False 230 231 # connect as "gm-dbo" 232 conn = gmAuthWidgets.get_dbowner_connection ( 233 procedure = _('Enlisting person as user.'), 234 dbo_password = gmTools.none_if(self._TXT_dbo_password.GetValue(), u'') 235 ) 236 if conn is None: 237 return False 238 239 # create new user 240 pat = gmPerson.gmCurrentPatient() 241 db_account = self._TXT_account.GetValue() 242 queries = [ 243 # database account 244 {'cmd': u'select gm.create_user(%s, %s)', 'args': [db_account, self._TXT_password.GetValue()]}, 245 # staff entry 246 { 247 '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)", 248 'args': [pat.ID, db_account, self._TXT_short_alias.GetValue().strip()] 249 } 250 ] 251 try: 252 rows, idx = gmPG2.run_rw_queries(link_obj = conn, queries = queries, end_tx = True) 253 except gmPG2.dbapi.IntegrityError, e: 254 if e.pgcode == gmPG2.sql_error_codes.UNIQUE_VIOLATION: 255 gmGuiHelpers.gm_show_error ( 256 aMessage = _( 257 'Cannot add GNUmed user.\n' 258 '\n' 259 'The database account [%s] is already listed as a\n' 260 'GNUmed user. There can only be one GNUmed user\n' 261 'for each database account.\n' 262 ) % db_account, 263 aTitle = _('Adding GNUmed user') 264 ) 265 return False 266 raise 267 268 if self.IsModal(): 269 self.EndModal(wx.ID_OK) 270 else: 271 self.Close()
272 #========================================================================== 273 # $Log: gmStaffWidgets.py,v $ 274 # Revision 1.27 2010-01-31 18:20:03 ncq 275 # - protect against empty passwords 276 # 277 # Revision 1.26 2009/07/23 16:42:38 ncq 278 # - staff -> user 279 # 280 # Revision 1.25 2009/06/04 16:33:51 ncq 281 # - adjust to dob-less persons 282 # 283 # Revision 1.24 2008/12/01 12:17:17 ncq 284 # - again 285 # 286 # Revision 1.23 2008/12/01 12:15:36 ncq 287 # - gm_*_user now live in gm. 288 # 289 # Revision 1.22 2008/10/22 12:21:58 ncq 290 # - use %x in strftime where appropriate 291 # 292 # Revision 1.21 2008/08/20 14:55:33 ncq 293 # - explicitely signal error condition of database 294 # account already being in use for a staff member 295 # 296 # Revision 1.20 2008/03/05 22:30:15 ncq 297 # - new style logging 298 # 299 # Revision 1.19 2008/01/11 16:15:33 ncq 300 # - first/last -> first-/lastnames 301 # 302 # Revision 1.18 2008/01/05 16:41:27 ncq 303 # - remove logging from gm_show_*() 304 # 305 # Revision 1.17 2007/12/04 16:16:27 ncq 306 # - use gmAuthWidgets 307 # 308 # Revision 1.16 2007/04/23 01:11:51 ncq 309 # - handle gm-dbo password field 310 # 311 # Revision 1.15 2007/03/01 16:35:01 ncq 312 # - no more idents 313 # 314 # Revision 1.14 2007/02/22 17:41:13 ncq 315 # - adjust to gmPerson changes 316 # 317 # Revision 1.13 2006/12/31 16:25:43 ncq 318 # - strftime() does not take unicode 319 # 320 # Revision 1.12 2006/11/24 14:23:41 ncq 321 # - EndModal() needs wx.ID_* 322 # 323 # Revision 1.11 2006/10/31 13:30:27 ncq 324 # - use gmPG2 325 # 326 # Revision 1.10 2006/10/25 07:46:44 ncq 327 # - Format() -> strftime() since datetime.datetime does not have .Format() 328 # 329 # Revision 1.9 2006/10/25 07:21:57 ncq 330 # - no more gmPG 331 # 332 # Revision 1.8 2006/09/03 11:32:10 ncq 333 # - clean up wx import 334 # - use gmTools.coalesce() 335 # - use gmGuiHelpers.get_dbowner_connection instead of local crap copy 336 # 337 # Revision 1.7 2006/06/17 16:45:19 ncq 338 # - only insert column labels once 339 # - use get_dbowner_connection() in gmGuiHelpers 340 # - implement activate()/save() on staff details 341 # 342 # Revision 1.6 2006/06/15 20:57:49 ncq 343 # - actually do something with the improved staff list editor 344 # 345 # Revision 1.5 2006/06/10 05:13:06 ncq 346 # - improved "edit staff list" 347 # 348 # Revision 1.4 2006/06/09 14:43:02 ncq 349 # - improve staff member handling 350 # 351 # Revision 1.3 2006/06/06 20:54:36 ncq 352 # - add staff member delisting dialog 353 # 354 # Revision 1.2 2006/03/14 21:31:15 ncq 355 # - add event handlers for buttons 356 # - actually implement adding a new provider 357 # 358 # Revision 1.1 2006/03/09 21:10:14 ncq 359 # - simple wrapper around dialog to add current patient as staff member 360 # 361