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 False47 48 #============================================================50 51 if no_parent: 52 parent = None 53 else: 54 if parent is None: 55 parent = wx.GetApp().GetTopWindow() 56 57 #-------------------- 58 def new(): 59 manage_orgs(parent = parent, no_parent = no_parent) 60 return True61 #-------------------- 62 def refresh(lctrl): 63 units = gmOrganization.get_org_units(order_by = 'organization, unit, l10n_unit_category') 64 items = [ [ 65 u['organization'], 66 u['unit'], 67 gmTools.coalesce(u['l10n_unit_category'], u''), 68 u['pk_org_unit'] 69 ] for u in units ] 70 71 lctrl.set_string_items(items = items) 72 lctrl.set_data(data = units) 73 #-------------------- 74 if msg is None: 75 msg = _("Organizations and units thereof.\n") 76 77 return gmListWidgets.get_choices_from_list ( 78 parent = parent, 79 msg = msg, 80 caption = _('Unit selection ...'), 81 columns = [_('Organization'), _('Unit'), _('Unit type'), '#'], 82 can_return_empty = False, 83 single_selection = True, 84 refresh_callback = refresh, 85 new_callback = new 86 ) 87 88 #============================================================90161 #============================================================92 query = u""" 93 SELECT DISTINCT ON (data) * FROM ( 94 SELECT * FROM (( 95 96 SELECT 97 pk_org_unit 98 AS data, 99 unit || coalesce(' (' || l10n_unit_category || ')', '') || ': ' || organization || ' (' || l10n_organization_category || ')' 100 AS list_label, 101 unit || ' (' || organization || ')' 102 AS field_label 103 FROM 104 dem.v_org_units 105 WHERE 106 unit %(fragment_condition)s 107 108 ) UNION ALL ( 109 110 SELECT 111 pk_org_unit 112 AS data, 113 coalesce(l10n_unit_category || ' ', '') || '"' || unit || '": ' || organization || ' (' || l10n_organization_category || ')' 114 AS list_label, 115 unit || ' (' || organization || ')' 116 AS field_label 117 FROM 118 dem.v_org_units 119 WHERE 120 l10n_unit_category %(fragment_condition)s 121 OR 122 unit_category %(fragment_condition)s 123 124 ) UNION ALL ( 125 126 SELECT 127 pk_org_unit 128 AS data, 129 organization || ': ' || unit || coalesce(' (' || l10n_unit_category || ')', '') 130 AS list_label, 131 unit || ' (' || organization || ')' 132 AS field_label 133 FROM 134 dem.v_org_units 135 WHERE 136 organization %(fragment_condition)s 137 138 )) AS all_matches 139 ORDER BY list_label 140 ) AS ordered_matches 141 LIMIT 50 142 """ 143 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 144 mp.setThresholds(1, 3, 5) 145 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 146 self.SetToolTipString(_("Select an organizational unit.")) 147 self.matcher = mp148 #--------------------------------------------------------150 if self.GetData() is None: 151 return None 152 unit = self._data2instance() 153 if unit is None: 154 return None 155 return u'\n'.join(unit.format(with_address = True))156 #--------------------------------------------------------158 if self.GetData() is None: 159 return None 160 return gmOrganization.cOrgUnit(aPK_obj = self.GetData())163 """A list for managing organizational units.""" 164266 267 #============================================================ 268 # org unit edit area 269 from Gnumed.wxGladeWidgets import wxgOrgUnitEAPnl 270166 167 try: 168 self.__org = kwargs['org'] 169 del kwargs['org'] 170 except KeyError: 171 self.__org = None 172 173 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs) 174 175 self.refresh_callback = self.refresh 176 self.new_callback = self._add 177 self.edit_callback = self._edit 178 self.delete_callback = self._del 179 180 self.__show_none_if_no_org = True 181 self.__init_ui() 182 self.__refresh()183 #-------------------------------------------------------- 184 # external API 185 #-------------------------------------------------------- 188 #-------------------------------------------------------- 189 # event handlers 190 #-------------------------------------------------------- 193 #-------------------------------------------------------- 196 #-------------------------------------------------------- 199 #-------------------------------------------------------- 202 #-------------------------------------------------------- 203 # internal helpers 204 #--------------------------------------------------------206 self._LCTRL_items.SetToolTipString(_('Units (sites, parts, departments, branches, ...) of organizations registered in GNUmed.')) 207 self._LCTRL_items.set_columns(columns = [ _('Organizational Unit'), _('Unit Category'), u'#' ])208 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE]) 209 #--------------------------------------------------------211 212 msg_template = _('Units of: %s') 213 214 if self.__org is None: 215 self._BTN_add.Enable(False) 216 self._BTN_edit.Enable(False) 217 self._BTN_remove.Enable(False) 218 pk = None 219 self.message = msg_template % _('<no organization selected>') 220 if self.__show_none_if_no_org: 221 self._LCTRL_items.set_string_items(items = None) 222 return 223 else: 224 self._BTN_add.Enable(True) 225 pk = self.__org['pk_org'] 226 org_str = u'%s (%s)' % ( 227 self.__org['organization'], 228 self.__org['l10n_category'] 229 ) 230 self.message = msg_template % org_str 231 232 units = gmOrganization.get_org_units(order_by = 'unit, l10n_unit_category', org = pk) 233 items = [ [ 234 u['unit'], 235 gmTools.coalesce(u['l10n_unit_category'], u''), 236 u['pk_org_unit'] 237 ] for u in units ] 238 239 self._LCTRL_items.set_string_items(items) 240 self._LCTRL_items.set_data(units)241 #-------------------------------------------------------- 242 # properties 243 #-------------------------------------------------------- 246 250 251 org = property(_get_org, _set_org) 252 #-------------------------------------------------------- 255257 if show_none_if_no_org == self.__show_none_if_no_org: 258 return 259 if show_none_if_no_org: 260 self.__show_none_if_no_org = True 261 else: 262 self.__show_none_if_no_org = False 263 self.__refresh()264 265 show_none_if_no_org = property(_get_show_none_if_no_org, _set_show_none_if_no_org)272371 #============================================================ 372 from Gnumed.wxGladeWidgets import wxgOrgUnitAddressPnl 373274 275 try: 276 data = kwargs['unit'] 277 del kwargs['unit'] 278 except KeyError: 279 data = None 280 281 wxgOrgUnitEAPnl.wxgOrgUnitEAPnl.__init__(self, *args, **kwargs) 282 gmEditArea.cGenericEditAreaMixin.__init__(self) 283 284 self.mode = 'new' 285 self.data = data 286 if data is not None: 287 self.mode = 'edit'288 289 # self.__init_ui() 290 #---------------------------------------------------------------- 291 # def __init_ui(self): 292 # pass 293 #---------------------------------------------------------------- 294 # generic Edit Area mixin API 295 #----------------------------------------------------------------297 validity = True 298 299 if self._PRW_category.GetData() is not None: 300 self._PRW_category.display_as_valid(True) 301 else: 302 if self._PRW_category.GetValue().strip() == u'': 303 self._PRW_category.display_as_valid(True) 304 else: 305 validity = False 306 self._PRW_category.display_as_valid(False) 307 self._PRW_category.SetFocus() 308 309 if self._PRW_unit.GetData() is not None: 310 self._PRW_unit.display_as_valid(True) 311 else: 312 if self._PRW_unit.GetValue().strip() != u'': 313 self._PRW_unit.display_as_valid(True) 314 else: 315 validity = False 316 self._PRW_unit.display_as_valid(False) 317 self._PRW_unit.SetFocus() 318 319 if self._PRW_org.GetData() is None: 320 validity = False 321 self._PRW_org.display_as_valid(False) 322 self._PRW_org.SetFocus() 323 else: 324 self._PRW_org.display_as_valid(True) 325 326 return validity327 #----------------------------------------------------------------329 data = gmOrganization.create_org_unit ( 330 pk_organization = self._PRW_org.GetData(), 331 unit = self._PRW_unit.GetValue().strip() 332 ) 333 data['pk_category_unit'] = self._PRW_category.GetData() 334 data.save() 335 336 self.data = data 337 return True338 #----------------------------------------------------------------340 self.data['pk_org'] = self._PRW_org.GetData() 341 self.data['unit'] = self._PRW_unit.GetValue().strip() 342 self.data['pk_category_unit'] = self._PRW_category.GetData() 343 self.data.save() 344 return True345 #----------------------------------------------------------------347 self._PRW_org.SetText(value = u'', data = None) 348 self._PRW_unit.SetText(value = u'', data = None) 349 self._PRW_category.SetText(value = u'', data = None) 350 351 self._PRW_unit.SetFocus()352 #----------------------------------------------------------------354 self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org']) 355 self._PRW_unit.SetText(value = u'', data = None) 356 self._PRW_category.SetText(value = self.data['unit_category'], data = self.data['pk_category_unit']) 357 358 self._PRW_unit.SetFocus()359 #----------------------------------------------------------------361 self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org']) 362 self._PRW_unit.SetText(value = self.data['unit'], data = self.data['pk_org_unit']) 363 self._PRW_category.SetText(value = self.data['unit_category'], data = self.data['pk_category_unit']) 364 365 self._PRW_unit.SetFocus()366 #---------------------------------------------------------------- 369 370 organization = property(lambda x:x, _set_org)375487 #============================================================ 488 # organizations API 489 #------------------------------------------------------------377 378 wxgOrgUnitAddressPnl.wxgOrgUnitAddressPnl.__init__(self, *args, **kwargs) 379 380 self.__unit = None381 #-------------------------------------------------------- 382 # internal helpers 383 #--------------------------------------------------------385 if self.__unit is None: 386 self.message = _('<no unit selected>') 387 self._PRW_address_searcher.SetText(u'', None) 388 self._PRW_address_searcher.Enable(False) 389 self._PRW_address_searcher.display_as_disabled(True) 390 self._BTN_save_picked_address.Enable(False) 391 self._BTN_add_new_address.Enable(False) 392 else: 393 if self.__unit['l10n_unit_category'] is None: 394 cat = u'' 395 left_delim = u'' 396 right_delim = u'' 397 else: 398 cat = u'%s ' % self.__unit['l10n_unit_category'] 399 left_delim = gmTools.u_left_double_angle_quote 400 right_delim = gmTools.u_right_double_angle_quote 401 self.message = u'%s%s%s%s' % ( 402 cat, 403 left_delim, 404 self.__unit['unit'], 405 right_delim 406 ) 407 self._PRW_address_searcher.Enable(True) 408 self._PRW_address_searcher.address = self.__unit['pk_address'] 409 self._PRW_address_searcher.Enable(True) 410 self._PRW_address_searcher.display_as_disabled(False) 411 self._BTN_save_picked_address.Enable(True) 412 self._BTN_add_new_address.Enable(True)413 #-------------------------------------------------------- 414 # event handlers 415 #-------------------------------------------------------- 429 #-------------------------------------------------------- 440 #-------------------------------------------------------- 462 #-------------------------------------------------------- 463 # properties 464 #-------------------------------------------------------- 467 471 472 unit = property(_get_unit, _set_unit) 473 #-------------------------------------------------------- 476478 if msg is None: 479 self._LBL_message.Hide() 480 self._LBL_message.SetLabel(u'') 481 else: 482 self._LBL_message.SetLabel(msg) 483 self._LBL_message.Show() 484 self.Layout()485 486 message = property(_get_message, _set_message)491 492 if no_parent: 493 parent = None 494 else: 495 if parent is None: 496 parent = wx.GetApp().GetTopWindow() 497 498 dlg = cOrganizationManagerDlg(parent, -1) 499 dlg.ShowModal() 500 dlg.Destroy()501 #============================================================503 ea = cOrganizationEAPnl(parent = parent, id = -1) 504 ea.data = org 505 ea.mode = gmTools.coalesce(org, 'new', 'edit') 506 dlg = gmEditArea.cGenericEditAreaDlg2(parent = parent, id = -1, edit_area = ea, single_entry = single_entry) 507 dlg.SetTitle(gmTools.coalesce(org, _('Adding new organization'), _('Editing organization'))) 508 if dlg.ShowModal() == wx.ID_OK: 509 dlg.Destroy() 510 return True 511 dlg.Destroy() 512 return False513 #============================================================515560 561 #==================================================================== 562 from Gnumed.wxGladeWidgets import wxgOrganizationEAPnl 563517 query = u""" 518 SELECT DISTINCT ON (data) * FROM ( 519 SELECT * FROM (( 520 521 SELECT 522 pk_org 523 AS data, 524 organization || ' (' || l10n_category || ')' 525 AS list_label, 526 organization || ' (' || l10n_category || ')' 527 AS field_label 528 FROM 529 dem.v_orgs 530 WHERE 531 organization %(fragment_condition)s 532 533 ) UNION ALL ( 534 535 SELECT 536 pk_org 537 AS data, 538 l10n_category || ': ' || organization 539 AS list_label, 540 organization || ' (' || l10n_category || ')' 541 AS field_label 542 FROM 543 dem.v_orgs 544 WHERE 545 l10n_category %(fragment_condition)s 546 OR 547 category %(fragment_condition)s 548 549 )) AS all_matches 550 ORDER BY list_label 551 ) AS ordered_matches 552 LIMIT 50 553 """ 554 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 555 mp.setThresholds(1, 3, 5) 556 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 557 self.SetToolTipString(_("Select an organization.")) 558 self.matcher = mp 559 self.selection_only = True564 -class cOrganizationEAPnl(wxgOrganizationEAPnl.wxgOrganizationEAPnl, gmEditArea.cGenericEditAreaMixin):565651 652 #============================================================567 568 try: 569 data = kwargs['organization'] 570 del kwargs['organization'] 571 except KeyError: 572 data = None 573 574 wxgOrganizationEAPnl.wxgOrganizationEAPnl.__init__(self, *args, **kwargs) 575 gmEditArea.cGenericEditAreaMixin.__init__(self) 576 577 self.mode = 'new' 578 self.data = data 579 if data is not None: 580 self.mode = 'edit'581 582 #self.__init_ui() 583 #---------------------------------------------------------------- 586 #---------------------------------------------------------------- 587 # generic Edit Area mixin API 588 #----------------------------------------------------------------590 validity = True 591 592 if self._PRW_category.GetData() is None: 593 validity = False 594 self._PRW_category.display_as_valid(False) 595 self._PRW_category.SetFocus() 596 else: 597 self._PRW_category.display_as_valid(True) 598 599 if self.mode == 'edit': 600 if self._PRW_org.GetData() is None: 601 validity = False 602 self._PRW_org.display_as_valid(False) 603 self._PRW_org.SetFocus() 604 else: 605 self._PRW_org.display_as_valid(True) 606 else: 607 if self._PRW_org.GetValue().strip() == u'': 608 validity = False 609 self._PRW_org.display_as_valid(False) 610 self._PRW_org.SetFocus() 611 else: 612 if self._PRW_org.GetData() is not None: 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 return validity620 #----------------------------------------------------------------622 self.data = gmOrganization.create_org ( 623 organization = self._PRW_org.GetValue().strip(), 624 category = self._PRW_category.GetData() 625 ) 626 return True627 #----------------------------------------------------------------629 self.data['pk_org'] = self._PRW_org.GetData() 630 self.data['pk_category_org'] = self._PRW_category.GetData() 631 self.data.save() 632 return True633 #----------------------------------------------------------------635 self._PRW_org.SetText(value = u'', data = None) 636 self._PRW_category.SetText(value = u'', data = None) 637 638 self._PRW_org.SetFocus()639 #----------------------------------------------------------------641 self._PRW_org.SetText(value = u'', data = None) 642 self._PRW_category.SetText(value = self.data['l10n_category'], data = self.data['pk_category_org']) 643 644 self._PRW_org.SetFocus()645 #----------------------------------------------------------------654683 684 #============================================================656 query = u""" 657 SELECT DISTINCT ON (data) 658 * 659 FROM ( 660 SELECT 661 pk 662 AS data, 663 _(description) || ' (' || description || ')' 664 AS list_label, 665 _(description) 666 AS field_label 667 FROM 668 dem.org_category 669 WHERE 670 _(description) %(fragment_condition)s 671 OR 672 description %(fragment_condition)s 673 ORDER BY list_label 674 ) AS ordered_matches 675 LIMIT 50 676 """ 677 mp = gmMatchProvider.cMatchProvider_SQL2(queries=query) 678 mp.setThresholds(1, 3, 5) 679 gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs) 680 self.SetToolTipString(_("Select an organizational category.")) 681 self.matcher = mp 682 self.selection_only = True686 """A list for managing organizations.""" 687727 #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE]) 728 #============================================================ 729 from Gnumed.wxGladeWidgets import wxgOrganizationManagerDlg 730689 690 gmListWidgets.cGenericListManagerPnl.__init__(self, *args, **kwargs) 691 692 self.refresh_callback = self.refresh 693 self.new_callback = self._add 694 self.edit_callback = self._edit 695 self.delete_callback = self._del 696 697 self.__init_ui() 698 self.refresh()699 #-------------------------------------------------------- 700 # external API 701 #--------------------------------------------------------703 orgs = gmOrganization.get_orgs(order_by = 'organization, l10n_category') 704 items = [ [o['organization'], o['l10n_category'], o['pk_org']] for o in orgs ] 705 self._LCTRL_items.set_string_items(items) 706 self._LCTRL_items.set_data(orgs)707 #-------------------------------------------------------- 708 # event handlers 709 #-------------------------------------------------------- 712 #-------------------------------------------------------- 715 #-------------------------------------------------------- 718 #-------------------------------------------------------- 721 #-------------------------------------------------------- 722 # internal helpers 723 #--------------------------------------------------------725 self._LCTRL_items.SetToolTipString(_('Organizations registered in GNUmed.')) 726 self._LCTRL_items.set_columns(columns = [_('Organization'), _('Category'), u'#'])732763 #============================================================ 764 # main 765 #------------------------------------------------------------ 766 if __name__ == "__main__": 767 768 if len(sys.argv) < 2: 769 sys.exit() 770 771 if sys.argv[1] != u'test': 772 sys.exit() 773 774 from Gnumed.pycommon import gmPG2 775 from Gnumed.pycommon import gmI18N 776 gmI18N.activate_locale() 777 gmI18N.install_domain() 778 779 #--------------------------------------------------------734 735 wxgOrganizationManagerDlg.wxgOrganizationManagerDlg.__init__(self, *args, **kwargs) 736 737 self.Centre(direction = wx.BOTH) 738 739 self._PNL_address.type_is_editable = False 740 self._PNL_orgs.select_callback = self._on_org_selected 741 self._PNL_units.select_callback = self._on_unit_selected 742 self._PNL_comms.message = _('Communication channels') 743 744 # FIXME: find proper button 745 #self._PNL_units.MoveAfterInTabOrder(self._PNL_orgs._BTN_) 746 747 self._on_org_selected(None) 748 self._PNL_orgs._LCTRL_items.SetFocus()749 #-------------------------------------------------------- 750 # event handlers 751 #-------------------------------------------------------- 755 #--------------------------------------------------------757 self._PNL_address.unit = item 758 self._PNL_comms.channel_owner = item 759 if item is None: 760 self._PNL_comms._BTN_add.Enable(False) 761 else: 762 self._PNL_comms._BTN_add.Enable(True)781 app = wx.PyWidgetTester(size = (200, 50)) 782 pw = cOrganizationPhraseWheel(app.frame, -1) 783 app.frame.Show(True) 784 app.MainLoop()785 #--------------------------------------------------------787 app = wx.PyWidgetTester(size = (200, 50)) 788 pw = cOrgUnitPhraseWheel(app.frame, -1) 789 app.frame.Show(True) 790 app.MainLoop()791 #--------------------------------------------------------793 conn = gmPG2.get_connection() 794 app = wx.PyWidgetTester(size = (600, 600)) 795 dlg = cOrganizationManagerDlg(app.frame, -1, size = (600, 600)) 796 dlg.SetSize((600, 600)) 797 dlg.ShowModal() 798 # app.SetWidget(dlg, -1) 799 app.MainLoop()800 #-------------------------------------------------------- 801 #test_org_unit_prw() 802 #test_org_prw() 803 test() 804 805 #====================================================================== 806
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Mon Jun 10 03:57:01 2013 | http://epydoc.sourceforge.net |