Package Gnumed :: Package wxpython :: Module gmOrganizationWidgets |
[frames] | no frames]

# Source Code for Module Gnumed.wxpython.gmOrganizationWidgets

      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  #------------------------------------------------------------
     36 -def edit_org_unit(parent=None, org_unit=None, single_entry=False, org=None):
     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 False
     49
     50  #============================================================
     51 -def select_org_unit(parent=None, msg=None, no_parent=False):
     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 True
     63          #--------------------
     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  #============================================================
     91 -class cOrgUnitPhraseWheel(gmPhraseWheel.cPhraseWheel):
     92
     93 -        def __init__(self, *args, **kwargs):
     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 = mp
    150          #--------------------------------------------------------
    151 -        def _get_data_tooltip(self):
    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          #--------------------------------------------------------
    159 -        def _data2instance(self):
    160                  if self.GetData() is None:
    161                          return None
    162                  return gmOrganization.cOrgUnit(aPK_obj = self.GetData())
    163  #============================================================
    164 -class cOrgUnitsManagerPnl(gmListWidgets.cGenericListManagerPnl):
    165          """A list for managing organizational units."""
    166
    167 -        def __init__(self, *args, **kwargs):
    168
    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          #--------------------------------------------------------
    188 -        def refresh(self, lctrl=None):
    189                  self.__refresh()
    190          #--------------------------------------------------------
    191          # event handlers
    192          #--------------------------------------------------------
    193 -        def _add(self):
    194                  return edit_org_unit(parent = self, org_unit = None, single_entry = False, org = self.__org)
    195          #--------------------------------------------------------
    196 -        def _edit(self, item):
    197                  return edit_org_unit(parent = self, org_unit = item, single_entry = True)
    198          #--------------------------------------------------------
    199 -        def _del(self, item):
    200                  return gmOrganization.delete_org_unit(unit = item['pk_org_unit'])
    201          #--------------------------------------------------------
    202 -        def _on_list_item_focused(self, event):
    203                  pass
    204          #--------------------------------------------------------
    205          # internal helpers
    206          #--------------------------------------------------------
    207 -        def __init_ui(self):
    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_tooltip
    211                  #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE])
    212          #--------------------------------------------------------
    213 -        def get_tooltip(self, unit):
    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          #--------------------------------------------------------
    218 -        def __refresh(self):
    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          #--------------------------------------------------------
    257 -        def _get_org(self):
    258                  return self.__org
    259
    260 -        def _set_org(self, org):
    261                  self.__org = org
    262                  self.__refresh()
    263
    264          org = property(_get_org, _set_org)
    265          #--------------------------------------------------------
    266 -        def _get_show_none_if_no_org(self):
    267                  return self.__show_none_if_no_org
    268
    269 -        def _set_show_none_if_no_org(self, show_none_if_no_org):
    270                  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)
    279
    280  #============================================================
    281  # org unit edit area
    282  from Gnumed.wxGladeWidgets import wxgOrgUnitEAPnl
    283
    284 -class cOrgUnitEAPnl(wxgOrgUnitEAPnl.wxgOrgUnitEAPnl, gmEditArea.cGenericEditAreaMixin):
    285
    286 -        def __init__(self, *args, **kwargs):
    287
    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          #----------------------------------------------------------------
    309 -        def _valid_for_save(self):
    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 validity
    340          #----------------------------------------------------------------
    341 -        def _save_as_new(self):
    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 True
    351          #----------------------------------------------------------------
    352 -        def _save_as_update(self):
    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 True
    358          #----------------------------------------------------------------
    359 -        def _refresh_as_new(self):
    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          #----------------------------------------------------------------
    366 -        def _refresh_as_new_from_existing(self):
    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          #----------------------------------------------------------------
    373 -        def _refresh_from_existing(self):
    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          #----------------------------------------------------------------
    380 -        def _set_org(self, org):
    381                  self._PRW_org.SetText(value = org['organization'], data = org['pk_org'])
    382
    383          organization = property(lambda x:x, _set_org)
    384  #============================================================
    385  from Gnumed.wxGladeWidgets import wxgOrgUnitAddressPnl
    386
    387 -class cOrgUnitAddressPnl(wxgOrgUnitAddressPnl.wxgOrgUnitAddressPnl):
    388
    389 -        def __init__(self, *args, **kwargs):
    390
    391                  wxgOrgUnitAddressPnl.wxgOrgUnitAddressPnl.__init__(self, *args, **kwargs)
    392
    393                  self.__unit = None
    394          #--------------------------------------------------------
    395          # internal helpers
    396          #--------------------------------------------------------
    397 -        def __refresh(self):
    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          #--------------------------------------------------------
    429 -        def _on_save_picked_address_button_pressed(self, event):
    430                  if self._PRW_address_searcher.GetData() is None:
    431                          if self._PRW_address_searcher.GetValue().strip() != u'':
    432                                  gmDispatcher.send(signal = 'statustext', msg = _('Invalid address selection.'))
    433                                  self._PRW_address_searcher.display_as_valid(False)
    434                                  self._PRW_address_searcher.SetFocus()
    435                                  return
    436
    437                  self._PRW_address_searcher.display_as_valid(True)
    438
    439                  self.__unit['pk_address'] = self._PRW_address_searcher.GetData()
    440                  self.__unit.save()
    441                  self.__refresh()
    442          #--------------------------------------------------------
    443 -        def _on_add_new_address_button_pressed(self, event):
    444                  ea = gmAddressWidgets.cAddressEAPnl(self, -1)
    445                  ea.address_holder = self.__unit
    446                  ea.type_is_editable = False
    447                  dlg = gmEditArea.cGenericEditAreaDlg2(self, -1, edit_area = ea)
    448                  dlg.SetTitle(_('Adding new address'))
    449                  if dlg.ShowModal() != wx.ID_OK:
    450                          return False
    451                  self.__refresh()
    452                  return True
    453          #--------------------------------------------------------
    454 -        def _on_manage_addresses_button_pressed(self, event):
    455                  picked_address = gmAddressWidgets.manage_addresses(parent = self)
    456                  if picked_address is None:
    457                          return
    458
    459                  question = u'%s\n\n  %s\n' % (
    460                          _('Link the following address to the organizational unit ?'),
    461                          u'\n  '.join(picked_address.format())
    462                  )
    463
    464                  link_it = gmGuiHelpers.gm_show_question (
    465                          title = _('Linking selected address'),
    466                          question = question
    467                  )
    468                  if not link_it:
    469                          return
    470
    471                  self._PRW_address_searcher.address = picked_address['pk_address']
    472                  self._PRW_address_searcher.display_as_valid(True)
    473                  self.__unit['pk_address'] = self._PRW_address_searcher.GetData()
    474                  self.__unit.save()
    475          #--------------------------------------------------------
    476          # properties
    477          #--------------------------------------------------------
    478 -        def _get_unit(self):
    479                  return self.__unit
    480
    481 -        def _set_unit(self, unit):
    482                  self.__unit = unit
    483                  self.__refresh()
    484
    485          unit = property(_get_unit, _set_unit)
    486          #--------------------------------------------------------
    487 -        def _get_message(self):
    488                  return self._LBL_message.GetLabel()
    489
    490 -        def _set_message(self, msg):
    491                  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)
    500  #============================================================
    501  # organizations API
    502  #------------------------------------------------------------
    503 -def manage_orgs(parent=None, no_parent=False):
    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  #============================================================
    515 -def edit_org(parent=None, org=None, single_entry=False):
    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 False
    526  #============================================================
    527 -class cOrganizationPhraseWheel(gmPhraseWheel.cPhraseWheel):
    528
    529 -        def __init__(self, *args, **kwargs):
    530                  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 = True
    573
    574  #====================================================================
    575  from Gnumed.wxGladeWidgets import wxgOrganizationEAPnl
    576
    577 -class cOrganizationEAPnl(wxgOrganizationEAPnl.wxgOrganizationEAPnl, gmEditArea.cGenericEditAreaMixin):
    578
    579 -        def __init__(self, *args, **kwargs):
    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          #----------------------------------------------------------------
    597 -        def __init_ui(self):
    598                  self._PRW_org.selection_only = False
    599          #----------------------------------------------------------------
    600          # generic Edit Area mixin API
    601          #----------------------------------------------------------------
    602 -        def _valid_for_save(self):
    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 validity
    640          #----------------------------------------------------------------
    641 -        def _save_as_new(self):
    642                  self.data = gmOrganization.create_org (
    643                          organization = self._PRW_org.GetValue().strip(),
    644                          category = self._PRW_category.GetData()
    645                  )
    646                  return True
    647          #----------------------------------------------------------------
    648 -        def _save_as_update(self):
    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 True
    654          #----------------------------------------------------------------
    655 -        def _refresh_as_new(self):
    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          #----------------------------------------------------------------
    661 -        def _refresh_as_new_from_existing(self):
    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          #----------------------------------------------------------------
    667 -        def _refresh_from_existing(self):
    668                  self._PRW_org.SetText(value = self.data['organization'], data = self.data['pk_org'])
    669                  self._PRW_category.SetText(value = self.data['l10n_category'], data = self.data['pk_category_org'])
    670
    671                  self._PRW_category.SetFocus()
    672
    673  #============================================================
    674 -class cOrgCategoryPhraseWheel(gmPhraseWheel.cPhraseWheel):
    675
    676 -        def __init__(self, *args, **kwargs):
    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 = True
    704
    705  #============================================================
    706 -class cOrganizationsManagerPnl(gmListWidgets.cGenericListManagerPnl):
    707          """A list for managing organizations."""
    708
    709 -        def __init__(self, *args, **kwargs):
    710
    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          #--------------------------------------------------------
    723 -        def refresh(self, lctrl=None):
    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                                  break
    734          #--------------------------------------------------------
    735          # event handlers
    736          #--------------------------------------------------------
    737 -        def _add(self):
    738                  return edit_org(parent = self, org = None, single_entry = False)
    739          #--------------------------------------------------------
    740 -        def _edit(self, item):
    741                  return edit_org(parent = self, org = item, single_entry = True)
    742          #--------------------------------------------------------
    743 -        def _del(self, item):
    744                  return gmOrganization.delete_org(organization = item['pk_org'])
    745          #--------------------------------------------------------
    746 -        def _on_list_item_focused(self, event):
    747                  pass
    748          #--------------------------------------------------------
    749          # internal helpers
    750          #--------------------------------------------------------
    751 -        def __init_ui(self):
    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_tooltip
    755                  #self._LCTRL_items.set_column_widths(widths = [wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE, wx.LIST_AUTOSIZE])
    756          #--------------------------------------------------------
    757 -        def get_tooltip(self, org):
    758                  if org is None:
    759                          return _('Organizations registered in GNUmed.')
    760                  return org.format()
    761  #============================================================
    762  from Gnumed.wxGladeWidgets import wxgOrganizationManagerDlg
    763
    764 -class cOrganizationManagerDlg(wxgOrganizationManagerDlg.wxgOrganizationManagerDlg):
    765
    766 -        def __init__(self, *args, **kwargs):
    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          #--------------------------------------------------------
    785 -        def _on_org_selected(self, item):
    786                  self._PNL_units.org = item
    787                  self._on_unit_selected(None)
    788          #--------------------------------------------------------
    789 -        def _on_unit_selected(self, item):
    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)
    796  #============================================================
    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          #--------------------------------------------------------
    813 -        def test_org_prw():
    814                  app = wx.PyWidgetTester(size = (200, 50))
    815                  pw = cOrganizationPhraseWheel(app.frame, -1)
    816                  app.frame.Show(True)
    817                  app.MainLoop()
    818          #--------------------------------------------------------
    819 -        def test_org_unit_prw():
    820                  app = wx.PyWidgetTester(size = (200, 50))
    821                  pw = cOrgUnitPhraseWheel(app.frame, -1)
    822                  app.frame.Show(True)
    823                  app.MainLoop()
    824          #--------------------------------------------------------
    825 -        def test():
    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