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