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):578664 665 #============================================================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.mode == 'edit': 613 if self._PRW_org.GetData() is None: 614 validity = False 615 self._PRW_org.display_as_valid(False) 616 self._PRW_org.SetFocus() 617 else: 618 self._PRW_org.display_as_valid(True) 619 else: 620 if self._PRW_org.GetValue().strip() == u'': 621 validity = False 622 self._PRW_org.display_as_valid(False) 623 self._PRW_org.SetFocus() 624 else: 625 if self._PRW_org.GetData() is not None: 626 validity = False 627 self._PRW_org.display_as_valid(False) 628 self._PRW_org.SetFocus() 629 else: 630 self._PRW_org.display_as_valid(True) 631 632 return validity633 #----------------------------------------------------------------635 self.data = gmOrganization.create_org ( 636 organization = self._PRW_org.GetValue().strip(), 637 category = self._PRW_category.GetData() 638 ) 639 return True640 #----------------------------------------------------------------642 self.data['pk_org'] = self._PRW_org.GetData() 643 self.data['pk_category_org'] = self._PRW_category.GetData() 644 self.data.save() 645 return True646 #----------------------------------------------------------------648 self._PRW_org.SetText(value = u'', data = None) 649 self._PRW_category.SetText(value = u'', data = None) 650 651 self._PRW_org.SetFocus()652 #----------------------------------------------------------------654 self._PRW_org.SetText(value = u'', data = None) 655 self._PRW_category.SetText(value = self.data['l10n_category'], data = self.data['pk_category_org']) 656 657 self._PRW_org.SetFocus()658 #----------------------------------------------------------------667696 697 #============================================================669 query = u""" 670 SELECT DISTINCT ON (data) 671 * 672 FROM ( 673 SELECT 674 pk 675 AS data, 676 _(description) || ' (' || description || ')' 677 AS list_label, 678 _(description) 679 AS field_label 680 FROM 681 dem.org_category 682 WHERE 683 _(description) %(fragment_condition)s 684 OR 685 description %(fragment_condition)s 686 ORDER BY list_label 687 ) AS ordered_matches 688 LIMIT 50 689 """ 690 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 691 mp.setThresholds(1, 3, 5) 692 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 693 self.SetToolTipString(_("Select an organizational category.")) 694 self.matcher = mp 695 self.selection_only = True699 """A list for managing organizations.""" 700753 #============================================================ 754 from Gnumed.wxGladeWidgets import wxgOrganizationManagerDlg 755702 703 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs) 704 705 self.refresh_callback = self.refresh 706 self.new_callback = self._add 707 self.edit_callback = self._edit 708 self.delete_callback = self._del 709 710 self.__init_ui() 711 self.refresh()712 #-------------------------------------------------------- 713 # external API 714 #--------------------------------------------------------716 orgs = gmOrganization.get_orgs(order_by = 'organization, l10n_category') 717 items = [ [o['organization'], o['l10n_category'], o['pk_org']] for o in orgs ] 718 self._LCTRL_items.set_string_items(items) 719 self._LCTRL_items.set_data(orgs) 720 721 for idx in range(len(orgs)): 722 org = orgs[idx] 723 if org['is_praxis']: 724 self._LCTRL_items.SetItemTextColour(idx, col=wx.NamedColour('RED')) 725 break726 #-------------------------------------------------------- 727 # event handlers 728 #-------------------------------------------------------- 731 #-------------------------------------------------------- 734 #-------------------------------------------------------- 737 #-------------------------------------------------------- 740 #-------------------------------------------------------- 741 # internal helpers 742 #--------------------------------------------------------744 self._LCTRL_items.set_columns(columns = [_('Organization'), _('Category'), u'#']) 745 self._LCTRL_items.SetToolTipString(_('Organizations registered in GNUmed.')) 746 self._LCTRL_items.item_tooltip_callback = self.get_tooltip747 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE]) 748 #--------------------------------------------------------757788 #============================================================ 789 # main 790 #------------------------------------------------------------ 791 if __name__ == "__main__": 792 793 if len(sys.argv) < 2: 794 sys.exit() 795 796 if sys.argv[1] != u'test': 797 sys.exit() 798 799 from Gnumed.pycommon import gmPG2 800 from Gnumed.pycommon import gmI18N 801 gmI18N.activate_locale() 802 gmI18N.install_domain() 803 804 #--------------------------------------------------------759 760 wxgOrganizationManagerDlg.wxgOrganizationManagerDlg.__init__(self, *args, **kwargs) 761 762 self.Centre(direction = wx.BOTH) 763 764 self._PNL_address.type_is_editable = False 765 self._PNL_orgs.select_callback = self._on_org_selected 766 self._PNL_units.select_callback = self._on_unit_selected 767 self._PNL_comms.message = _('Communication channels') 768 769 # FIXME: find proper button 770 #self._PNL_units.MoveAfterInTabOrder(self._PNL_orgs._BTN_) 771 772 self._on_org_selected(None) 773 self._PNL_orgs._LCTRL_items.SetFocus()774 #-------------------------------------------------------- 775 # event handlers 776 #-------------------------------------------------------- 780 #--------------------------------------------------------782 self._PNL_address.unit = item 783 self._PNL_comms.channel_owner = item 784 if item is None: 785 self._PNL_comms._BTN_add.Enable(False) 786 else: 787 self._PNL_comms._BTN_add.Enable(True)806 app = wx.PyWidgetTester(size = (200, 50)) 807 pw = cOrganizationPhraseWheel(app.frame, -1) 808 app.frame.Show(True) 809 app.MainLoop()810 #--------------------------------------------------------812 app = wx.PyWidgetTester(size = (200, 50)) 813 pw = cOrgUnitPhraseWheel(app.frame, -1) 814 app.frame.Show(True) 815 app.MainLoop()816 #--------------------------------------------------------818 conn = gmPG2.get_connection() 819 app = wx.PyWidgetTester(size = (600, 600)) 820 dlg = cOrganizationManagerDlg(app.frame, -1, size = (600, 600)) 821 dlg.SetSize((600, 600)) 822 dlg.ShowModal() 823 # app.SetWidget(dlg, -1) 824 app.MainLoop()825 #-------------------------------------------------------- 826 #test_org_unit_prw() 827 #test_org_prw() 828 test() 829 830 #====================================================================== 831
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Jul 12 03:57:07 2013 | http://epydoc.sourceforge.net |