| Home | Trees | Indices | Help |
|
|---|
|
|
1 """GNUmed organization handling widgets.
2
3 copyright: authors
4 """
5 #============================================================
6 __author__ = "K.Hilbert"
7 __license__ = "GPL v2 or later (details at http://www.gnu.org)"
8
9 import logging, sys
10
11
12 import wx
13
14
15 if __name__ == '__main__':
16 sys.path.insert(0, '../../')
17 from Gnumed.pycommon import gmTools
18 from Gnumed.pycommon import gmMatchProvider
19 from Gnumed.pycommon import gmDispatcher
20 from Gnumed.business import gmOrganization
21 from Gnumed.wxpython import gmListWidgets
22 from Gnumed.wxpython import gmEditArea
23 from Gnumed.wxpython import gmPhraseWheel
24 from Gnumed.wxpython import gmPersonContactWidgets
25 from Gnumed.wxpython import gmAddressWidgets
26 from Gnumed.wxpython import gmGuiHelpers
27
28
29 _log = logging.getLogger('gm.organization')
30
31 #============================================================
32 # organizational units API
33 #------------------------------------------------------------
35 ea = cOrgUnitEAPnl(parent = parent, id = -1)
36 ea.data = org_unit
37 ea.mode = gmTools.coalesce(org_unit, 'new', 'edit')
38 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry)
39 if org is not None:
40 ea.organization = org
41 dlg.SetTitle(gmTools.coalesce(org_unit, _('Adding new organizational unit'), _('Editing organizational unit')))
42 if dlg.ShowModal() == wx.ID_OK:
43 dlg.Destroy()
44 return True
45 dlg.Destroy()
46 return False
47 #============================================================
49
51 query = u"""
52 SELECT DISTINCT ON (data) * FROM (
53 SELECT * FROM ((
54
55 SELECT
56 pk_org_unit
57 AS data,
58 unit || coalesce(' (' || l10n_unit_category || ')', '') || ': ' || organization || ' (' || l10n_organization_category || ')'
59 AS list_label,
60 unit || ' (' || organization || ')'
61 AS field_label
62 FROM
63 dem.v_org_units
64 WHERE
65 unit %(fragment_condition)s
66
67 ) UNION ALL (
68
69 SELECT
70 pk_org_unit
71 AS data,
72 coalesce(l10n_unit_category || ' ', '') || '"' || unit || '": ' || organization || ' (' || l10n_organization_category || ')'
73 AS list_label,
74 unit || ' (' || organization || ')'
75 AS field_label
76 FROM
77 dem.v_org_units
78 WHERE
79 l10n_unit_category %(fragment_condition)s
80 OR
81 unit_category %(fragment_condition)s
82
83 ) UNION ALL (
84
85 SELECT
86 pk_org_unit
87 AS data,
88 organization || ': ' || unit || coalesce(' (' || l10n_unit_category || ')', '')
89 AS list_label,
90 unit || ' (' || organization || ')'
91 AS field_label
92 FROM
93 dem.v_org_units
94 WHERE
95 organization %(fragment_condition)s
96
97 )) AS all_matches
98 ORDER BY list_label
99 ) AS ordered_matches
100 LIMIT 50
101 """
102 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query)
103 mp.setThresholds(1, 3, 5)
104 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
105 self.SetToolTipString(_("Select an organizational unit."))
106 self.matcher = mp
107 #--------------------------------------------------------
109 if self.GetData() is None:
110 return None
111 unit = self._data2instance()
112 if unit is None:
113 return None
114 return u'\n'.join(unit.format(with_address = True))
115 #--------------------------------------------------------
117 if self.GetData() is None:
118 return None
119 return gmOrganization.cOrgUnit(aPK_obj = self.GetData())
120 #============================================================
122 """A list for managing organizational units."""
123
125
126 try:
127 self.__org = kwargs['org']
128 del kwargs['org']
129 except KeyError:
130 self.__org = None
131
132 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs)
133
134 self.refresh_callback = self.refresh
135 self.new_callback = self._add
136 self.edit_callback = self._edit
137 self.delete_callback = self._del
138
139 self.__show_none_if_no_org = True
140 self.__init_ui()
141 self.__refresh()
142 #--------------------------------------------------------
143 # external API
144 #--------------------------------------------------------
147 #--------------------------------------------------------
148 # event handlers
149 #--------------------------------------------------------
152 #--------------------------------------------------------
154 return edit_org_unit(parent = self, org_unit = item, single_entry = True)
155 #--------------------------------------------------------
158 #--------------------------------------------------------
161 #--------------------------------------------------------
162 # internal helpers
163 #--------------------------------------------------------
165 self._LCTRL_items.SetToolTipString(_('Units (sites, parts, departments, branches, ...) of organizations registered in GNUmed.'))
166 self._LCTRL_items.set_columns(columns = [ _('Organizational Unit'), _('Unit Category'), u'#' ])
167 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE])
168 #--------------------------------------------------------
170
171 msg_template = _('Units of: %s')
172
173 if self.__org is None:
174 self._BTN_add.Enable(False)
175 self._BTN_edit.Enable(False)
176 self._BTN_remove.Enable(False)
177 pk = None
178 self.message = msg_template % _('<no organization selected>')
179 if self.__show_none_if_no_org:
180 self._LCTRL_items.set_string_items(items = None)
181 return
182 else:
183 self._BTN_add.Enable(True)
184 pk = self.__org['pk_org']
185 org_str = u'%s (%s)' % (
186 self.__org['organization'],
187 self.__org['l10n_category']
188 )
189 self.message = msg_template % org_str
190
191 units = gmOrganization.get_org_units(order_by = 'unit, l10n_unit_category', org = pk)
192 items = [ [
193 u['unit'],
194 gmTools.coalesce(u['l10n_unit_category'], u''),
195 u['pk_org_unit']
196 ] for u in units ]
197
198 self._LCTRL_items.set_string_items(items)
199 self._LCTRL_items.set_data(units)
200 #--------------------------------------------------------
201 # properties
202 #--------------------------------------------------------
205
209
210 org = property(_get_org, _set_org)
211 #--------------------------------------------------------
214
216 if show_none_if_no_org == self.__show_none_if_no_org:
217 return
218 if show_none_if_no_org:
219 self.__show_none_if_no_org = True
220 else:
221 self.__show_none_if_no_org = False
222 self.__refresh()
223
224 show_none_if_no_org = property(_get_show_none_if_no_org, _set_show_none_if_no_org)
225
226 #============================================================
227 # org unit edit area
228 from Gnumed.wxGladeWidgets import wxgOrgUnitEAPnl
229
231
233
234 try:
235 data = kwargs['unit']
236 del kwargs['unit']
237 except KeyError:
238 data = None
239
240 wxgOrgUnitEAPnl.wxgOrgUnitEAPnl.__init__(self, *args, **kwargs)
241 gmEditArea.cGenericEditAreaMixin.__init__(self)
242
243 self.mode = 'new'
244 self.data = data
245 if data is not None:
246 self.mode = 'edit'
247
248 # self.__init_ui()
249 #----------------------------------------------------------------
250 # def __init_ui(self):
251 # pass
252 #----------------------------------------------------------------
253 # generic Edit Area mixin API
254 #----------------------------------------------------------------
256 validity = True
257
258 if self._PRW_category.GetData() is not None:
259 self._PRW_category.display_as_valid(True)
260 else:
261 if self._PRW_category.GetValue().strip() == u'':
262 self._PRW_category.display_as_valid(True)
263 else:
264 validity = False
265 self._PRW_category.display_as_valid(False)
266 self._PRW_category.SetFocus()
267
268 if self._PRW_unit.GetData() is not None:
269 self._PRW_unit.display_as_valid(True)
270 else:
271 if self._PRW_unit.GetValue().strip() != u'':
272 self._PRW_unit.display_as_valid(True)
273 else:
274 validity = False
275 self._PRW_unit.display_as_valid(False)
276 self._PRW_unit.SetFocus()
277
278 if self._PRW_org.GetData() is None:
279 validity = False
280 self._PRW_org.display_as_valid(False)
281 self._PRW_org.SetFocus()
282 else:
283 self._PRW_org.display_as_valid(True)
284
285 return validity
286 #----------------------------------------------------------------
288 data = gmOrganization.create_org_unit (
289 pk_organization = self._PRW_org.GetData(),
290 unit = self._PRW_unit.GetValue().strip()
291 )
292 data['pk_category_unit'] = self._PRW_category.GetData()
293 data.save()
294
295 self.data = data
296 return True
297 #----------------------------------------------------------------
299 self.data['pk_org'] = self._PRW_org.GetData()
300 self.data['unit'] = self._PRW_unit.GetValue().strip()
301 self.data['pk_category_unit'] = self._PRW_category.GetData()
302 self.data.save()
303 return True
304 #----------------------------------------------------------------
306 self._PRW_org.SetText(value = u'', data = None)
307 self._PRW_unit.SetText(value = u'', data = None)
308 self._PRW_category.SetText(value = u'', data = None)
309
310 self._PRW_unit.SetFocus()
311 #----------------------------------------------------------------
313 self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org'])
314 self._PRW_unit.SetText(value = u'', data = None)
315 self._PRW_category.SetText(value = self.data['unit_category'], data = self.data['pk_category_unit'])
316
317 self._PRW_unit.SetFocus()
318 #----------------------------------------------------------------
320 self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org'])
321 self._PRW_unit.SetText(value = self.data['unit'], data = self.data['pk_org_unit'])
322 self._PRW_category.SetText(value = self.data['unit_category'], data = self.data['pk_category_unit'])
323
324 self._PRW_unit.SetFocus()
325 #----------------------------------------------------------------
328
329 organization = property(lambda x:x, _set_org)
330 #============================================================
331 from Gnumed.wxGladeWidgets import wxgOrgUnitAddressPnl
332
334
336
337 wxgOrgUnitAddressPnl.wxgOrgUnitAddressPnl.__init__(self, *args, **kwargs)
338
339 self.__unit = None
340 #--------------------------------------------------------
341 # internal helpers
342 #--------------------------------------------------------
344 if self.__unit is None:
345 self.message = _('<no unit selected>')
346 self._PRW_address_searcher.SetText(u'', None)
347 self._PRW_address_searcher.Enable(False)
348 self._PRW_address_searcher.display_as_disabled(True)
349 self._BTN_save_picked_address.Enable(False)
350 self._BTN_add_new_address.Enable(False)
351 else:
352 if self.__unit['l10n_unit_category'] is None:
353 cat = u''
354 left_delim = u''
355 right_delim = u''
356 else:
357 cat = u'%s ' % self.__unit['l10n_unit_category']
358 left_delim = gmTools.u_left_double_angle_quote
359 right_delim = gmTools.u_right_double_angle_quote
360 self.message = u'%s%s%s%s' % (
361 cat,
362 left_delim,
363 self.__unit['unit'],
364 right_delim
365 )
366 self._PRW_address_searcher.Enable(True)
367 self._PRW_address_searcher.address = self.__unit['pk_address']
368 self._PRW_address_searcher.Enable(True)
369 self._PRW_address_searcher.display_as_disabled(False)
370 self._BTN_save_picked_address.Enable(True)
371 self._BTN_add_new_address.Enable(True)
372 #--------------------------------------------------------
373 # event handlers
374 #--------------------------------------------------------
388 #--------------------------------------------------------
399 #--------------------------------------------------------
421 #--------------------------------------------------------
422 # properties
423 #--------------------------------------------------------
426
430
431 unit = property(_get_unit, _set_unit)
432 #--------------------------------------------------------
435
437 if msg is None:
438 self._LBL_message.Hide()
439 self._LBL_message.SetLabel(u'')
440 else:
441 self._LBL_message.SetLabel(msg)
442 self._LBL_message.Show()
443 self.Layout()
444
445 message = property(_get_message, _set_message)
446 #============================================================
447 # organizations API
448 #------------------------------------------------------------
450
451 if parent is None:
452 parent = wx.GetApp().GetTopWindow()
453
454 dlg = cOrganizationManagerDlg(parent, -1)
455 dlg.ShowModal()
456 #============================================================
458 ea = cOrganizationEAPnl(parent = parent, id = -1)
459 ea.data = org
460 ea.mode = gmTools.coalesce(org, 'new', 'edit')
461 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry)
462 dlg.SetTitle(gmTools.coalesce(org, _('Adding new organization'), _('Editing organization')))
463 if dlg.ShowModal() == wx.ID_OK:
464 dlg.Destroy()
465 return True
466 dlg.Destroy()
467 return False
468 #============================================================
470
472 query = u"""
473 SELECT DISTINCT ON (data) * FROM (
474 SELECT * FROM ((
475
476 SELECT
477 pk_org
478 AS data,
479 organization || ' (' || l10n_category || ')'
480 AS list_label,
481 organization || ' (' || l10n_category || ')'
482 AS field_label
483 FROM
484 dem.v_orgs
485 WHERE
486 organization %(fragment_condition)s
487
488 ) UNION ALL (
489
490 SELECT
491 pk_org
492 AS data,
493 l10n_category || ': ' || organization
494 AS list_label,
495 organization || ' (' || l10n_category || ')'
496 AS field_label
497 FROM
498 dem.v_orgs
499 WHERE
500 l10n_category %(fragment_condition)s
501 OR
502 category %(fragment_condition)s
503
504 )) AS all_matches
505 ORDER BY list_label
506 ) AS ordered_matches
507 LIMIT 50
508 """
509 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query)
510 mp.setThresholds(1, 3, 5)
511 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
512 self.SetToolTipString(_("Select an organization."))
513 self.matcher = mp
514 self.selection_only = True
515
516 #====================================================================
517 from Gnumed.wxGladeWidgets import wxgOrganizationEAPnl
518
519 -class cOrganizationEAPnl(wxgOrganizationEAPnl.wxgOrganizationEAPnl, gmEditArea.cGenericEditAreaMixin):
520
522
523 try:
524 data = kwargs['organization']
525 del kwargs['organization']
526 except KeyError:
527 data = None
528
529 wxgOrganizationEAPnl.wxgOrganizationEAPnl.__init__(self, *args, **kwargs)
530 gmEditArea.cGenericEditAreaMixin.__init__(self)
531
532 self.mode = 'new'
533 self.data = data
534 if data is not None:
535 self.mode = 'edit'
536
537 #self.__init_ui()
538 #----------------------------------------------------------------
541 #----------------------------------------------------------------
542 # generic Edit Area mixin API
543 #----------------------------------------------------------------
545 validity = True
546
547 if self._PRW_category.GetData() is None:
548 validity = False
549 self._PRW_category.display_as_valid(False)
550 self._PRW_category.SetFocus()
551 else:
552 self._PRW_category.display_as_valid(True)
553
554 if self.mode == 'edit':
555 if self._PRW_org.GetData() is None:
556 validity = False
557 self._PRW_org.display_as_valid(False)
558 self._PRW_org.SetFocus()
559 else:
560 self._PRW_org.display_as_valid(True)
561 else:
562 if self._PRW_org.GetValue().strip() == u'':
563 validity = False
564 self._PRW_org.display_as_valid(False)
565 self._PRW_org.SetFocus()
566 else:
567 if self._PRW_org.GetData() is not None:
568 validity = False
569 self._PRW_org.display_as_valid(False)
570 self._PRW_org.SetFocus()
571 else:
572 self._PRW_org.display_as_valid(True)
573
574 return validity
575 #----------------------------------------------------------------
577 self.data = gmOrganization.create_org (
578 organization = self._PRW_org.GetValue().strip(),
579 category = self._PRW_category.GetData()
580 )
581 return True
582 #----------------------------------------------------------------
584 self.data['pk_org'] = self._PRW_org.GetData()
585 self.data['pk_category_org'] = self._PRW_category.GetData()
586 self.data.save()
587 return True
588 #----------------------------------------------------------------
590 self._PRW_org.SetText(value = u'', data = None)
591 self._PRW_category.SetText(value = u'', data = None)
592
593 self._PRW_org.SetFocus()
594 #----------------------------------------------------------------
596 self._PRW_org.SetText(value = u'', data = None)
597 self._PRW_category.SetText(value = self.data['l10n_category'], data = self.data['pk_category_org'])
598
599 self._PRW_org.SetFocus()
600 #----------------------------------------------------------------
606
607 #============================================================
609
611 query = u"""
612 SELECT DISTINCT ON (data)
613 *
614 FROM (
615 SELECT
616 pk
617 AS data,
618 _(description) || ' (' || description || ')'
619 AS list_label,
620 _(description)
621 AS field_label
622 FROM
623 dem.org_category
624 WHERE
625 _(description) %(fragment_condition)s
626 OR
627 description %(fragment_condition)s
628 ORDER BY list_label
629 ) AS ordered_matches
630 LIMIT 50
631 """
632 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query)
633 mp.setThresholds(1, 3, 5)
634 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
635 self.SetToolTipString(_("Select an organizational category."))
636 self.matcher = mp
637 self.selection_only = True
638
639 #============================================================
641 """A list for managing organizations."""
642
644
645 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs)
646
647 self.refresh_callback = self.refresh
648 self.new_callback = self._add
649 self.edit_callback = self._edit
650 self.delete_callback = self._del
651
652 self.__init_ui()
653 self.refresh()
654 #--------------------------------------------------------
655 # external API
656 #--------------------------------------------------------
658 orgs = gmOrganization.get_orgs(order_by = 'organization, l10n_category')
659 items = [ [o['organization'], o['l10n_category'], o['pk_org']] for o in orgs ]
660 self._LCTRL_items.set_string_items(items)
661 self._LCTRL_items.set_data(orgs)
662 #--------------------------------------------------------
663 # event handlers
664 #--------------------------------------------------------
667 #--------------------------------------------------------
670 #--------------------------------------------------------
673 #--------------------------------------------------------
676 #--------------------------------------------------------
677 # internal helpers
678 #--------------------------------------------------------
680 self._LCTRL_items.SetToolTipString(_('Organizations registered in GNUmed.'))
681 self._LCTRL_items.set_columns(columns = [_('Organization'), _('Category'), u'#'])
682 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE])
683 #============================================================
684 from Gnumed.wxGladeWidgets import wxgOrganizationManagerDlg
685
687
689
690 wxgOrganizationManagerDlg.wxgOrganizationManagerDlg.__init__(self, *args, **kwargs)
691
692 self.Centre(direction = wx.BOTH)
693
694 self._PNL_address.type_is_editable = False
695 self._PNL_orgs.select_callback = self._on_org_selected
696 self._PNL_units.select_callback = self._on_unit_selected
697 self._PNL_comms.message = _('Communication channels')
698
699 # FIXME: find proper button
700 #self._PNL_units.MoveAfterInTabOrder(self._PNL_orgs._BTN_)
701
702 self._on_org_selected(None)
703 self._PNL_orgs._LCTRL_items.SetFocus()
704 #--------------------------------------------------------
705 # event handlers
706 #--------------------------------------------------------
710 #--------------------------------------------------------
712 self._PNL_address.unit = item
713 self._PNL_comms.channel_owner = item
714 if item is None:
715 self._PNL_comms._BTN_add.Enable(False)
716 else:
717 self._PNL_comms._BTN_add.Enable(True)
718 #============================================================
719 # main
720 #------------------------------------------------------------
721 if __name__ == "__main__":
722
723 if len(sys.argv) < 2:
724 sys.exit()
725
726 if sys.argv[1] != u'test':
727 sys.exit()
728
729 from Gnumed.pycommon import gmPG2
730 from Gnumed.pycommon import gmI18N
731 gmI18N.activate_locale()
732 gmI18N.install_domain()
733
734 #--------------------------------------------------------
736 app = wx.PyWidgetTester(size = (200, 50))
737 pw = cOrganizationPhraseWheel(app.frame, -1)
738 app.frame.Show(True)
739 app.MainLoop()
740 #--------------------------------------------------------
742 app = wx.PyWidgetTester(size = (200, 50))
743 pw = cOrgUnitPhraseWheel(app.frame, -1)
744 app.frame.Show(True)
745 app.MainLoop()
746 #--------------------------------------------------------
748 conn = gmPG2.get_connection()
749 app = wx.PyWidgetTester(size = (600, 600))
750 dlg = cOrganizationManagerDlg(app.frame, -1, size = (600, 600))
751 dlg.SetSize((600, 600))
752 dlg.ShowModal()
753 # app.SetWidget(dlg, -1)
754 app.MainLoop()
755 #--------------------------------------------------------
756 #test_org_unit_prw()
757 #test_org_prw()
758 test()
759
760 #======================================================================
761
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Mon Jun 25 03:58:21 2012 | http://epydoc.sourceforge.net |