1 """GNUmed praxis related widgets.
2
3 Praxis:
4
5 Each database belongs to ONE praxis only. A praxis can
6 have several branches. A praxis is at the same level
7 as an organization, except that it is not explicitely
8 defined. Rather, that ONE organization of which at least
9 one unit is defined as a praxis branch IS the praxis.
10
11 Praxis branch
12
13 Branches are the sites/locations of a praxis. There
14 can be several branches. Each branch must link to
15 units of ONE AND THE SAME organization (because
16 it is not considered good data protection practice
17 to mix charts of *different* organizations within
18 one database). However, that organization which
19 has units that are praxis branches can also have
20 other units which are not branches :-)
21
22 copyright: authors
23 """
24
25 __author__ = "K.Hilbert"
26 __license__ = "GPL v2 or later (details at http://www.gnu.org)"
27
28 import logging
29 import sys
30
31
32
33 import wx
34
35
36 if __name__ == '__main__':
37 sys.path.insert(0, '../../')
38 from Gnumed.pycommon import gmCfg
39 from Gnumed.pycommon import gmDispatcher
40 from Gnumed.pycommon import gmTools
41 from Gnumed.pycommon import gmPG2
42
43 from Gnumed.business import gmPraxis
44 from Gnumed.business import gmStaff
45 from Gnumed.business import gmOrganization
46
47 from Gnumed.wxpython import gmOrganizationWidgets
48 from Gnumed.wxpython import gmGuiHelpers
49 from Gnumed.wxpython import gmAuthWidgets
50 from Gnumed.wxpython import gmListWidgets
51 from Gnumed.wxpython import gmPlugin
52 from Gnumed.wxpython import gmCfgWidgets
53
54
55 _log = logging.getLogger('gm.praxis')
56
57
59
60 if parent is None:
61 parent = wx.GetApp().GetTopWindow()
62
63 conn = gmAuthWidgets.get_dbowner_connection(procedure = _('showing audit trail'))
64 if conn is None:
65 return False
66
67
68 def refresh(lctrl):
69 cmd = u'SELECT * FROM audit.v_audit_trail ORDER BY audit_when_ts'
70 rows, idx = gmPG2.run_ro_queries(link_obj = conn, queries = [{'cmd': cmd}], get_col_idx = False)
71 lctrl.set_string_items (
72 [ [
73 r['event_when'],
74 r['event_by'],
75 u'%s %s %s' % (
76 gmTools.coalesce(r['row_version_before'], gmTools.u_diameter),
77 gmTools.u_right_arrow,
78 gmTools.coalesce(r['row_version_after'], gmTools.u_diameter)
79 ),
80 r['event_table'],
81 r['event'],
82 r['pk_audit']
83 ] for r in rows ]
84 )
85
86 gmListWidgets.get_choices_from_list (
87 parent = parent,
88 msg = u'',
89 caption = _('GNUmed database audit log ...'),
90 columns = [ _('When'), _('Who'), _('Revisions'), _('Table'), _('Event'), '#' ],
91 single_selection = True,
92 refresh_callback = refresh
93 )
94
95
132
133
134
135
180
181 def edit(workplace=None):
182
183 dbcfg = gmCfg.cCfgSQL()
184
185 if workplace is None:
186 dlg = wx.TextEntryDialog (
187 parent = parent,
188 message = _('Enter a descriptive name for the new workplace:'),
189 caption = _('Configuring GNUmed workplaces ...'),
190 defaultValue = u'',
191 style = wx.OK | wx.CENTRE
192 )
193 dlg.ShowModal()
194 workplace = dlg.GetValue().strip()
195 if workplace == u'':
196 gmGuiHelpers.gm_show_error(_('Cannot save a new workplace without a name.'), _('Configuring GNUmed workplaces ...'))
197 return False
198 curr_plugins = []
199 else:
200 curr_plugins = gmTools.coalesce(dbcfg.get2 (
201 option = u'horstspace.notebook.plugin_load_order',
202 workplace = workplace,
203 bias = 'workplace'
204 ), []
205 )
206
207 msg = _(
208 'Pick the plugin(s) to be loaded the next time the client is restarted under the workplace:\n'
209 '\n'
210 ' [%s]\n'
211 ) % workplace
212
213 picker = gmListWidgets.cItemPickerDlg (
214 parent,
215 -1,
216 title = _('Configuring workplace plugins ...'),
217 msg = msg
218 )
219 picker.set_columns(['Available plugins'], ['Active plugins'])
220 available_plugins = gmPlugin.get_installed_plugins(plugin_dir = 'gui')
221 picker.set_choices(available_plugins)
222 picker.set_picks(picks = curr_plugins[:])
223 btn_pressed = picker.ShowModal()
224 if btn_pressed != wx.ID_OK:
225 picker.Destroy()
226 return False
227
228 new_plugins = picker.get_picks()
229 picker.Destroy()
230 if new_plugins == curr_plugins:
231 return True
232
233 if new_plugins is None:
234 return True
235
236 dbcfg.set (
237 option = u'horstspace.notebook.plugin_load_order',
238 value = new_plugins,
239 workplace = workplace
240 )
241
242 return True
243
244 def edit_old(workplace=None):
245
246 available_plugins = gmPlugin.get_installed_plugins(plugin_dir='gui')
247
248 dbcfg = gmCfg.cCfgSQL()
249
250 if workplace is None:
251 dlg = wx.TextEntryDialog (
252 parent = parent,
253 message = _('Enter a descriptive name for the new workplace:'),
254 caption = _('Configuring GNUmed workplaces ...'),
255 defaultValue = u'',
256 style = wx.OK | wx.CENTRE
257 )
258 dlg.ShowModal()
259 workplace = dlg.GetValue().strip()
260 if workplace == u'':
261 gmGuiHelpers.gm_show_error(_('Cannot save a new workplace without a name.'), _('Configuring GNUmed workplaces ...'))
262 return False
263 curr_plugins = []
264 choices = available_plugins
265 else:
266 curr_plugins = gmTools.coalesce(dbcfg.get2 (
267 option = u'horstspace.notebook.plugin_load_order',
268 workplace = workplace,
269 bias = 'workplace'
270 ), []
271 )
272 choices = curr_plugins[:]
273 for p in available_plugins:
274 if p not in choices:
275 choices.append(p)
276
277 sels = range(len(curr_plugins))
278 new_plugins = gmListWidgets.get_choices_from_list (
279 parent = parent,
280 msg = _(
281 '\n'
282 'Select the plugin(s) to be loaded the next time\n'
283 'the client is restarted under the workplace:\n'
284 '\n'
285 ' [%s]'
286 '\n'
287 ) % workplace,
288 caption = _('Configuring GNUmed workplaces ...'),
289 choices = choices,
290 selections = sels,
291 columns = [_('Plugins')],
292 single_selection = False
293 )
294
295 if new_plugins == curr_plugins:
296 return True
297
298 if new_plugins is None:
299 return True
300
301 dbcfg.set (
302 option = u'horstspace.notebook.plugin_load_order',
303 value = new_plugins,
304 workplace = workplace
305 )
306
307 return True
308
309 def clone(workplace=None):
310 if workplace is None:
311 return False
312
313 new_name = wx.GetTextFromUser (
314 message = _('Enter a name for the new workplace !'),
315 caption = _('Cloning workplace'),
316 default_value = u'%s-2' % workplace,
317 parent = parent
318 ).strip()
319
320 if new_name == u'':
321 return False
322
323 dbcfg = gmCfg.cCfgSQL()
324 opt = u'horstspace.notebook.plugin_load_order'
325
326 plugins = dbcfg.get2 (
327 option = opt,
328 workplace = workplace,
329 bias = 'workplace'
330 )
331
332 dbcfg.set (
333 option = opt,
334 value = plugins,
335 workplace = new_name
336 )
337
338
339
340 return True
341
342 def refresh(lctrl):
343 workplaces = gmPraxis.gmCurrentPraxisBranch().workplaces
344 curr_workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace
345 try:
346 sels = [workplaces.index(curr_workplace)]
347 except ValueError:
348 sels = []
349
350 lctrl.set_string_items(workplaces)
351 lctrl.set_selections(selections = sels)
352
353 gmListWidgets.get_choices_from_list (
354 parent = parent,
355 msg = _(
356 '\nSelect the workplace to configure below.\n'
357 '\n'
358 'The currently active workplace is preselected.\n'
359 ),
360 caption = _('Configuring GNUmed workplaces ...'),
361 columns = [_('Workplace')],
362 single_selection = True,
363 refresh_callback = refresh,
364 edit_callback = edit,
365 new_callback = edit,
366 delete_callback = delete,
367 left_extra_button = (_('Clone'), _('Clone the selected workplace'), clone)
368 )
369
370
371 from Gnumed.wxGladeWidgets import wxgGreetingEditorDlg
372
389
390
392
393 if no_parent:
394 parent = None
395 else:
396 if parent is None:
397 parent = wx.GetApp().GetTopWindow()
398
399 branches = gmPraxis.get_praxis_branches()
400
401 if len(branches) == 1:
402 _log.debug('only one praxis branch configured')
403 gmPraxis.gmCurrentPraxisBranch(branches[0])
404 return True
405
406 if len(branches) == 0:
407 orgs = gmOrganization.get_orgs()
408 if len(orgs) == 0:
409 pk_cat = gmOrganization.create_org_category(category = u'Praxis')
410 org = gmOrganization.create_org(_('Your praxis'), pk_cat)
411 unit = org.add_unit(_('Your branch'))
412 branch = gmPraxis.create_praxis_branch(pk_org_unit = unit['pk_org_unit'])
413 _log.debug('auto-created praxis branch because no organizations existed: %s', branch)
414 gmPraxis.gmCurrentPraxisBranch(branch)
415 gmGuiHelpers.gm_show_info (
416 title = _('Praxis configuration ...'),
417 info = _(
418 'GNUmed has auto-created the following\n'
419 'praxis branch for you (which you can\n'
420 'later configure as needed):\n'
421 '\n'
422 '%s'
423 ) % branch.format()
424 )
425 return True
426
427 _log.debug('no praxis branches configured, selecting from organization units')
428 msg = _(
429 'No praxis branches configured yet.\n'
430 '\n'
431 'You must select one unit of an organization to be\n'
432 'the initial branch (site, office) of your praxis.'
433 )
434 unit = gmOrganizationWidgets.select_org_unit(msg = msg, no_parent = True)
435 if unit is None:
436 _log.warning('no organization unit selected, aborting')
437 return False
438 _log.debug('org unit selected as praxis branch: %s', unit)
439 branch = gmPraxis.create_praxis_branch(pk_org_unit = unit['pk_org_unit'])
440 _log.debug('created praxis branch: %s', branch)
441 gmPraxis.gmCurrentPraxisBranch(branch)
442 return True
443
444
445 def refresh(lctrl):
446 branches = gmPraxis.get_praxis_branches()
447 items = [
448 [ b['branch'],
449 b['l10n_unit_category']
450 ] for b in branches
451 ]
452 lctrl.set_string_items(items = items)
453 lctrl.set_data(data = branches)
454
455 branch = gmListWidgets.get_choices_from_list (
456 parent = parent,
457 msg = _("Select branch (of praxis [%s]) which you are logging in from.\n") % branches[0]['praxis'],
458 caption = _('Praxis branch selection ...'),
459 columns = [_('Branch'), _('Branch type')],
460 can_return_empty = False,
461 single_selection = single_selection,
462 refresh_callback = refresh
463 )
464 if branch is None:
465 _log.warning('no praxis branch selected, aborting')
466 return False
467 gmPraxis.gmCurrentPraxisBranch(branch)
468 return True
469
470
472
473 if parent is None:
474 parent = wx.GetApp().GetTopWindow()
475
476 branches = gmPraxis.get_praxis_branches()
477 org = branches[0].branch.org
478
479 msg = _('Pick the units of')
480
481
482
483
484
485
486 if __name__ == "__main__":
487
488 if len(sys.argv) < 2:
489 sys.exit()
490
491 if sys.argv[1] != u'test':
492 sys.exit()
493
494
495
496
497
498
502
503
504
505
506