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 21 from Gnumed.business import gmOrganization 22 23 from Gnumed.wxpython import gmListWidgets 24 from Gnumed.wxpython import gmEditArea 25 from Gnumed.wxpython import gmPhraseWheel 26 from Gnumed.wxpython import gmPersonContactWidgets 27 from Gnumed.wxpython import gmAddressWidgets 28 from Gnumed.wxpython import gmGuiHelpers 29 30 31 _log = logging.getLogger('gm.organization') 32 33 #============================================================ 34 # organizational units API 35 #------------------------------------------------------------37 ea = cOrgUnitEAPnl(parent = parent, id = -1) 38 ea.data = org_unit 39 ea.mode = gmTools.coalesce(org_unit, 'new', 'edit') 40 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry) 41 if org is not None: 42 ea.organization = org 43 dlg.SetTitle(gmTools.coalesce(org_unit, _('Adding new organizational unit'), _('Editing organizational unit'))) 44 if dlg.ShowModal() == wx.ID_OK: 45 dlg.Destroy() 46 return True 47 dlg.Destroy() 48 return False49 50 #============================================================52 53 if no_parent: 54 parent = None 55 else: 56 if parent is None: 57 parent = wx.GetApp().GetTopWindow() 58 59 #-------------------- 60 def new(): 61 manage_orgs(parent = parent, no_parent = no_parent) 62 return True63 #-------------------- 64 def refresh(lctrl): 65 units = gmOrganization.get_org_units(order_by = 'organization, unit, l10n_unit_category') 66 items = [ [ 67 u['organization'], 68 u['unit'], 69 gmTools.coalesce(u['l10n_unit_category'], u''), 70 u['pk_org_unit'] 71 ] for u in units ] 72 73 lctrl.set_string_items(items = items) 74 lctrl.set_data(data = units) 75 #-------------------- 76 if msg is None: 77 msg = _("Organizations and units thereof.\n") 78 79 return gmListWidgets.get_choices_from_list ( 80 parent = parent, 81 msg = msg, 82 caption = _('Unit selection ...'), 83 columns = [_('Organization'), _('Unit'), _('Unit type'), '#'], 84 can_return_empty = False, 85 single_selection = True, 86 refresh_callback = refresh, 87 new_callback = new 88 ) 89 90 #============================================================92163 #============================================================94 query = u""" 95 SELECT DISTINCT ON (data) * FROM ( 96 SELECT * FROM (( 97 98 SELECT 99 pk_org_unit 100 AS data, 101 unit || coalesce(' (' || l10n_unit_category || ')', '') || ': ' || organization || ' (' || l10n_organization_category || ')' 102 AS list_label, 103 unit || ' (' || organization || ')' 104 AS field_label 105 FROM 106 dem.v_org_units 107 WHERE 108 unit %(fragment_condition)s 109 110 ) UNION ALL ( 111 112 SELECT 113 pk_org_unit 114 AS data, 115 coalesce(l10n_unit_category || ' ', '') || '"' || unit || '": ' || organization || ' (' || l10n_organization_category || ')' 116 AS list_label, 117 unit || ' (' || organization || ')' 118 AS field_label 119 FROM 120 dem.v_org_units 121 WHERE 122 l10n_unit_category %(fragment_condition)s 123 OR 124 unit_category %(fragment_condition)s 125 126 ) UNION ALL ( 127 128 SELECT 129 pk_org_unit 130 AS data, 131 organization || ': ' || unit || coalesce(' (' || l10n_unit_category || ')', '') 132 AS list_label, 133 unit || ' (' || organization || ')' 134 AS field_label 135 FROM 136 dem.v_org_units 137 WHERE 138 organization %(fragment_condition)s 139 140 )) AS all_matches 141 ORDER BY list_label 142 ) AS ordered_matches 143 LIMIT 50 144 """ 145 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 146 mp.setThresholds(1, 3, 5) 147 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 148 self.SetToolTipString(_("Select an organizational unit.")) 149 self.matcher = mp150 #--------------------------------------------------------152 if self.GetData() is None: 153 return None 154 unit = self._data2instance() 155 if unit is None: 156 return None 157 return u'\n'.join(unit.format(with_address = True))158 #--------------------------------------------------------160 if self.GetData() is None: 161 return None 162 return gmOrganization.cOrgUnit(aPK_obj = self.GetData())165 """A list for managing organizational units.""" 166279 280 #============================================================ 281 # org unit edit area 282 from Gnumed.wxGladeWidgets import wxgOrgUnitEAPnl 283168 169 try: 170 self.__org = kwargs['org'] 171 del kwargs['org'] 172 except KeyError: 173 self.__org = None 174 175 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs) 176 177 self.refresh_callback = self.refresh 178 self.new_callback = self._add 179 self.edit_callback = self._edit 180 self.delete_callback = self._del 181 182 self.__show_none_if_no_org = True 183 self.__init_ui() 184 self.__refresh()185 #-------------------------------------------------------- 186 # external API 187 #-------------------------------------------------------- 190 #-------------------------------------------------------- 191 # event handlers 192 #-------------------------------------------------------- 195 #-------------------------------------------------------- 198 #-------------------------------------------------------- 201 #-------------------------------------------------------- 204 #-------------------------------------------------------- 205 # internal helpers 206 #--------------------------------------------------------208 self._LCTRL_items.set_columns(columns = [ _('Organizational Unit'), _('Unit Category'), u'#' ]) 209 self._LCTRL_items.SetToolTipString(_('Units (sites, parts, departments, branches, ...) of organizations registered in GNUmed.')) 210 self._LCTRL_items.item_tooltip_callback = self.get_tooltip211 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE]) 212 #--------------------------------------------------------214 if unit is None: 215 return _('Units (sites, parts, departments, branches, ...) of organizations registered in GNUmed.') 216 return u'\n'.join(unit.format(with_address = True, with_org = True, with_comms = True))217 #--------------------------------------------------------219 220 msg_template = _('Units of: %s') 221 222 if self.__org is None: 223 self._BTN_add.Enable(False) 224 self._BTN_edit.Enable(False) 225 self._BTN_remove.Enable(False) 226 pk = None 227 self.message = msg_template % _('<no organization selected>') 228 if self.__show_none_if_no_org: 229 self._LCTRL_items.set_string_items(items = None) 230 return 231 else: 232 self._BTN_add.Enable(True) 233 pk = self.__org['pk_org'] 234 org_str = u'%s (%s)' % ( 235 self.__org['organization'], 236 self.__org['l10n_category'] 237 ) 238 self.message = msg_template % org_str 239 240 units = gmOrganization.get_org_units(order_by = 'unit, l10n_unit_category', org = pk) 241 items = [ [ 242 u['unit'], 243 gmTools.coalesce(u['l10n_unit_category'], u''), 244 u['pk_org_unit'] 245 ] for u in units ] 246 247 self._LCTRL_items.set_string_items(items) 248 self._LCTRL_items.set_data(units) 249 250 for idx in range(len(units)): 251 unit = units[idx] 252 if unit['is_praxis_branch']: 253 self._LCTRL_items.SetItemTextColour(idx, col=wx.NamedColour('RED'))254 #-------------------------------------------------------- 255 # properties 256 #-------------------------------------------------------- 259 263 264 org = property(_get_org, _set_org) 265 #-------------------------------------------------------- 268270 if show_none_if_no_org == self.__show_none_if_no_org: 271 return 272 if show_none_if_no_org: 273 self.__show_none_if_no_org = True 274 else: 275 self.__show_none_if_no_org = False 276 self.__refresh()277 278 show_none_if_no_org = property(_get_show_none_if_no_org, _set_show_none_if_no_org)285384 #============================================================ 385 from Gnumed.wxGladeWidgets import wxgOrgUnitAddressPnl 386287 288 try: 289 data = kwargs['unit'] 290 del kwargs['unit'] 291 except KeyError: 292 data = None 293 294 wxgOrgUnitEAPnl.wxgOrgUnitEAPnl.__init__(self, *args, **kwargs) 295 gmEditArea.cGenericEditAreaMixin.__init__(self) 296 297 self.mode = 'new' 298 self.data = data 299 if data is not None: 300 self.mode = 'edit'301 302 # self.__init_ui() 303 #---------------------------------------------------------------- 304 # def __init_ui(self): 305 # pass 306 #---------------------------------------------------------------- 307 # generic Edit Area mixin API 308 #----------------------------------------------------------------310 validity = True 311 312 if self._PRW_category.GetData() is not None: 313 self._PRW_category.display_as_valid(True) 314 else: 315 if self._PRW_category.GetValue().strip() == u'': 316 self._PRW_category.display_as_valid(True) 317 else: 318 validity = False 319 self._PRW_category.display_as_valid(False) 320 self._PRW_category.SetFocus() 321 322 if self._PRW_unit.GetData() is not None: 323 self._PRW_unit.display_as_valid(True) 324 else: 325 if self._PRW_unit.GetValue().strip() != u'': 326 self._PRW_unit.display_as_valid(True) 327 else: 328 validity = False 329 self._PRW_unit.display_as_valid(False) 330 self._PRW_unit.SetFocus() 331 332 if self._PRW_org.GetData() is None: 333 validity = False 334 self._PRW_org.display_as_valid(False) 335 self._PRW_org.SetFocus() 336 else: 337 self._PRW_org.display_as_valid(True) 338 339 return validity340 #----------------------------------------------------------------342 data = gmOrganization.create_org_unit ( 343 pk_organization = self._PRW_org.GetData(), 344 unit = self._PRW_unit.GetValue().strip() 345 ) 346 data['pk_category_unit'] = self._PRW_category.GetData() 347 data.save() 348 349 self.data = data 350 return True351 #----------------------------------------------------------------353 self.data['pk_org'] = self._PRW_org.GetData() 354 self.data['unit'] = self._PRW_unit.GetValue().strip() 355 self.data['pk_category_unit'] = self._PRW_category.GetData() 356 self.data.save() 357 return True358 #----------------------------------------------------------------360 self._PRW_org.SetText(value = u'', data = None) 361 self._PRW_unit.SetText(value = u'', data = None) 362 self._PRW_category.SetText(value = u'', data = None) 363 364 self._PRW_unit.SetFocus()365 #----------------------------------------------------------------367 self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org']) 368 self._PRW_unit.SetText(value = u'', data = None) 369 self._PRW_category.SetText(value = self.data['unit_category'], data = self.data['pk_category_unit']) 370 371 self._PRW_unit.SetFocus()372 #----------------------------------------------------------------374 self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org']) 375 self._PRW_unit.SetText(value = self.data['unit'], data = self.data['pk_org_unit']) 376 self._PRW_category.SetText(value = self.data['unit_category'], data = self.data['pk_category_unit']) 377 378 self._PRW_unit.SetFocus()379 #---------------------------------------------------------------- 382 383 organization = property(lambda x:x, _set_org)388500 #============================================================ 501 # organizations API 502 #------------------------------------------------------------390 391 wxgOrgUnitAddressPnl.wxgOrgUnitAddressPnl.__init__(self, *args, **kwargs) 392 393 self.__unit = None394 #-------------------------------------------------------- 395 # internal helpers 396 #--------------------------------------------------------398 if self.__unit is None: 399 self.message = _('<no unit selected>') 400 self._PRW_address_searcher.SetText(u'', None) 401 self._PRW_address_searcher.Enable(False) 402 self._PRW_address_searcher.display_as_disabled(True) 403 self._BTN_save_picked_address.Enable(False) 404 self._BTN_add_new_address.Enable(False) 405 else: 406 if self.__unit['l10n_unit_category'] is None: 407 cat = u'' 408 left_delim = u'' 409 right_delim = u'' 410 else: 411 cat = u'%s ' % self.__unit['l10n_unit_category'] 412 left_delim = gmTools.u_left_double_angle_quote 413 right_delim = gmTools.u_right_double_angle_quote 414 self.message = u'%s%s%s%s' % ( 415 cat, 416 left_delim, 417 self.__unit['unit'], 418 right_delim 419 ) 420 self._PRW_address_searcher.Enable(True) 421 self._PRW_address_searcher.address = self.__unit['pk_address'] 422 self._PRW_address_searcher.Enable(True) 423 self._PRW_address_searcher.display_as_disabled(False) 424 self._BTN_save_picked_address.Enable(True) 425 self._BTN_add_new_address.Enable(True)426 #-------------------------------------------------------- 427 # event handlers 428 #-------------------------------------------------------- 442 #-------------------------------------------------------- 453 #-------------------------------------------------------- 475 #-------------------------------------------------------- 476 # properties 477 #-------------------------------------------------------- 480 484 485 unit = property(_get_unit, _set_unit) 486 #-------------------------------------------------------- 489491 if msg is None: 492 self._LBL_message.Hide() 493 self._LBL_message.SetLabel(u'') 494 else: 495 self._LBL_message.SetLabel(msg) 496 self._LBL_message.Show() 497 self.Layout()498 499 message = property(_get_message, _set_message)504 505 if no_parent: 506 parent = None 507 else: 508 if parent is None: 509 parent = wx.GetApp().GetTopWindow() 510 511 dlg = cOrganizationManagerDlg(parent, -1) 512 dlg.ShowModal() 513 dlg.Destroy()514 #============================================================516 ea = cOrganizationEAPnl(parent = parent, id = -1) 517 ea.data = org 518 ea.mode = gmTools.coalesce(org, 'new', 'edit') 519 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry) 520 dlg.SetTitle(gmTools.coalesce(org, _('Adding new organization'), _('Editing organization'))) 521 if dlg.ShowModal() == wx.ID_OK: 522 dlg.Destroy() 523 return True 524 dlg.Destroy() 525 return False526 #============================================================528573 574 #==================================================================== 575 from Gnumed.wxGladeWidgets import wxgOrganizationEAPnl 576530 query = u""" 531 SELECT DISTINCT ON (data) * FROM ( 532 SELECT * FROM (( 533 534 SELECT 535 pk_org 536 AS data, 537 organization || ' (' || l10n_category || ')' 538 AS list_label, 539 organization || ' (' || l10n_category || ')' 540 AS field_label 541 FROM 542 dem.v_orgs 543 WHERE 544 organization %(fragment_condition)s 545 546 ) UNION ALL ( 547 548 SELECT 549 pk_org 550 AS data, 551 l10n_category || ': ' || organization 552 AS list_label, 553 organization || ' (' || l10n_category || ')' 554 AS field_label 555 FROM 556 dem.v_orgs 557 WHERE 558 l10n_category %(fragment_condition)s 559 OR 560 category %(fragment_condition)s 561 562 )) AS all_matches 563 ORDER BY list_label 564 ) AS ordered_matches 565 LIMIT 50 566 """ 567 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 568 mp.setThresholds(1, 3, 5) 569 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 570 self.SetToolTipString(_("Select an organization.")) 571 self.matcher = mp 572 self.selection_only = True577 -class cOrganizationEAPnl(wxgOrganizationEAPnl.wxgOrganizationEAPnl, gmEditArea.cGenericEditAreaMixin):578672 673 #============================================================580 581 try: 582 data = kwargs['organization'] 583 del kwargs['organization'] 584 except KeyError: 585 data = None 586 587 wxgOrganizationEAPnl.wxgOrganizationEAPnl.__init__(self, *args, **kwargs) 588 gmEditArea.cGenericEditAreaMixin.__init__(self) 589 590 self.mode = 'new' 591 self.data = data 592 if data is not None: 593 self.mode = 'edit'594 595 #self.__init_ui() 596 #---------------------------------------------------------------- 599 #---------------------------------------------------------------- 600 # generic Edit Area mixin API 601 #----------------------------------------------------------------603 validity = True 604 605 if self._PRW_category.GetData() is None: 606 validity = False 607 self._PRW_category.display_as_valid(False) 608 self._PRW_category.SetFocus() 609 else: 610 self._PRW_category.display_as_valid(True) 611 612 if self._PRW_org.GetValue().strip() == u'': 613 validity = False 614 self._PRW_org.display_as_valid(False) 615 self._PRW_org.SetFocus() 616 else: 617 self._PRW_org.display_as_valid(True) 618 619 # if self.mode == 'edit': 620 # if self._PRW_org.GetData() is None: 621 # validity = False 622 # self._PRW_org.display_as_valid(False) 623 # self._PRW_org.SetFocus() 624 # else: 625 # self._PRW_org.display_as_valid(True) 626 # else: 627 # if self._PRW_org.GetValue().strip() == u'': 628 # validity = False 629 # self._PRW_org.display_as_valid(False) 630 # self._PRW_org.SetFocus() 631 # else: 632 # if self._PRW_org.GetData() is not None: 633 # validity = False 634 # self._PRW_org.display_as_valid(False) 635 # self._PRW_org.SetFocus() 636 # else: 637 # self._PRW_org.display_as_valid(True) 638 639 return validity640 #----------------------------------------------------------------642 self.data = gmOrganization.create_org ( 643 organization = self._PRW_org.GetValue().strip(), 644 category = self._PRW_category.GetData() 645 ) 646 return True647 #----------------------------------------------------------------649 #self.data['pk_org'] = self._PRW_org.GetData() 650 self.data['organization'] = self._PRW_org.GetValue().strip() 651 self.data['pk_category_org'] = self._PRW_category.GetData() 652 self.data.save() 653 return True654 #----------------------------------------------------------------656 self._PRW_org.SetText(value = u'', data = None) 657 self._PRW_category.SetText(value = u'', data = None) 658 659 self._PRW_org.SetFocus()660 #----------------------------------------------------------------662 self._PRW_org.SetText(value = u'', data = None) 663 self._PRW_category.SetText(value = self.data['l10n_category'], data = self.data['pk_category_org']) 664 665 self._PRW_org.SetFocus()666 #----------------------------------------------------------------675704 705 #============================================================677 query = u""" 678 SELECT DISTINCT ON (data) 679 * 680 FROM ( 681 SELECT 682 pk 683 AS data, 684 _(description) || ' (' || description || ')' 685 AS list_label, 686 _(description) 687 AS field_label 688 FROM 689 dem.org_category 690 WHERE 691 _(description) %(fragment_condition)s 692 OR 693 description %(fragment_condition)s 694 ORDER BY list_label 695 ) AS ordered_matches 696 LIMIT 50 697 """ 698 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 699 mp.setThresholds(1, 3, 5) 700 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 701 self.SetToolTipString(_("Select an organizational category.")) 702 self.matcher = mp 703 self.selection_only = True707 """A list for managing organizations.""" 708761 #============================================================ 762 from Gnumed.wxGladeWidgets import wxgOrganizationManagerDlg 763710 711 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs) 712 713 self.refresh_callback = self.refresh 714 self.new_callback = self._add 715 self.edit_callback = self._edit 716 self.delete_callback = self._del 717 718 self.__init_ui() 719 self.refresh()720 #-------------------------------------------------------- 721 # external API 722 #--------------------------------------------------------724 orgs = gmOrganization.get_orgs(order_by = 'organization, l10n_category') 725 items = [ [o['organization'], o['l10n_category'], o['pk_org']] for o in orgs ] 726 self._LCTRL_items.set_string_items(items) 727 self._LCTRL_items.set_data(orgs) 728 729 for idx in range(len(orgs)): 730 org = orgs[idx] 731 if org['is_praxis']: 732 self._LCTRL_items.SetItemTextColour(idx, col=wx.NamedColour('RED')) 733 break734 #-------------------------------------------------------- 735 # event handlers 736 #-------------------------------------------------------- 739 #-------------------------------------------------------- 742 #-------------------------------------------------------- 745 #-------------------------------------------------------- 748 #-------------------------------------------------------- 749 # internal helpers 750 #--------------------------------------------------------752 self._LCTRL_items.set_columns(columns = [_('Organization'), _('Category'), u'#']) 753 self._LCTRL_items.SetToolTipString(_('Organizations registered in GNUmed.')) 754 self._LCTRL_items.item_tooltip_callback = self.get_tooltip755 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE]) 756 #--------------------------------------------------------765796 #============================================================ 797 # main 798 #------------------------------------------------------------ 799 if __name__ == "__main__": 800 801 if len(sys.argv) < 2: 802 sys.exit() 803 804 if sys.argv[1] != u'test': 805 sys.exit() 806 807 from Gnumed.pycommon import gmPG2 808 from Gnumed.pycommon import gmI18N 809 gmI18N.activate_locale() 810 gmI18N.install_domain() 811 812 #--------------------------------------------------------767 768 wxgOrganizationManagerDlg.wxgOrganizationManagerDlg.__init__(self, *args, **kwargs) 769 770 self.Centre(direction = wx.BOTH) 771 772 self._PNL_address.type_is_editable = False 773 self._PNL_orgs.select_callback = self._on_org_selected 774 self._PNL_units.select_callback = self._on_unit_selected 775 self._PNL_comms.message = _('Communication channels') 776 777 # FIXME: find proper button 778 #self._PNL_units.MoveAfterInTabOrder(self._PNL_orgs._BTN_) 779 780 self._on_org_selected(None) 781 self._PNL_orgs._LCTRL_items.SetFocus()782 #-------------------------------------------------------- 783 # event handlers 784 #-------------------------------------------------------- 788 #--------------------------------------------------------790 self._PNL_address.unit = item 791 self._PNL_comms.channel_owner = item 792 if item is None: 793 self._PNL_comms._BTN_add.Enable(False) 794 else: 795 self._PNL_comms._BTN_add.Enable(True)814 app = wx.PyWidgetTester(size = (200, 50)) 815 pw = cOrganizationPhraseWheel(app.frame, -1) 816 app.frame.Show(True) 817 app.MainLoop()818 #--------------------------------------------------------820 app = wx.PyWidgetTester(size = (200, 50)) 821 pw = cOrgUnitPhraseWheel(app.frame, -1) 822 app.frame.Show(True) 823 app.MainLoop()824 #--------------------------------------------------------826 conn = gmPG2.get_connection() 827 app = wx.PyWidgetTester(size = (600, 600)) 828 dlg = cOrganizationManagerDlg(app.frame, -1, size = (600, 600)) 829 dlg.SetSize((600, 600)) 830 dlg.ShowModal() 831 # app.SetWidget(dlg, -1) 832 app.MainLoop()833 #-------------------------------------------------------- 834 #test_org_unit_prw() 835 #test_org_prw() 836 test() 837 838 #====================================================================== 839
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Aug 3 03:56:14 2013 | http://epydoc.sourceforge.net |