Example #1
0
def manage_praxis_branches(parent=None):

    if parent is None:
        parent = wx.GetApp().GetTopWindow()

    #---------------------------
    def get_unit_tooltip(unit):
        if unit is None:
            return None
        return u'\n'.join(
            unit.format(with_address=True, with_org=True, with_comms=True))

    #---------------------------
    def manage_orgs():
        gmOrganizationWidgets.manage_orgs(parent=parent)

    #---------------------------

    branches = gmPraxis.get_praxis_branches()
    org = branches[0].organization
    praxis = branches[0]['praxis']

    msg = _('Pick those units of "%s" which are branches of your praxis !\n'
            '\n'
            'Note that no other client should be connected at this time.\n'
            '\n'
            'If you want to select another organization entirely\n'
            'first remove all existing branches.\n') % praxis

    picker = gmListWidgets.cItemPickerDlg(parent, -1, msg=msg)
    picker.extra_button = (_('Manage units'),
                           _('Manage organizations and their units'),
                           manage_orgs)
    picker.left_item_tooltip_callback = get_unit_tooltip
    picker.right_item_tooltip_callback = get_unit_tooltip
    picker.set_columns(columns=[_('Units of "%s"') % praxis],
                       columns_right=[_('Branches of your praxis')])
    units = org.units
    branch_unit_pks = [b['pk_org_unit'] for b in branches]
    branch_units = []
    for unit in units:
        if unit['pk_org_unit'] in branch_unit_pks:
            branch_units.append(unit)
    items = [
        u'%s%s' %
        (u['unit'], gmTools.coalesce(u['l10n_unit_category'], u'', u' (%s)'))
        for u in units
    ]
    picker.set_choices(choices=items, data=units)
    items = [
        u'%s%s' %
        (u['unit'], gmTools.coalesce(u['l10n_unit_category'], u'', u' (%s)'))
        for u in branch_units
    ]
    picker.set_picks(picks=items, data=branch_units)
    del units
    del branch_unit_pks
    del branch_units
    del items

    result = picker.ShowModal()

    if result == wx.ID_CANCEL:
        picker.Destroy()
        return None

    picks = picker.picks
    picker.Destroy()

    failed_delete_msg = _('Cannot delete praxis branch(es).\n'
                          '\n'
                          'There are probably clients logged in\n'
                          'from other locations. You need to log out\n'
                          'all but this client before the praxis can\n'
                          'be reconfigured.')

    if len(picks) == 0:
        if not gmPraxis.delete_praxis_branches():
            gmGuiHelpers.gm_show_error(error=failed_delete_msg,
                                       title=_('Configuring praxis ...'))
            return False
        while not set_active_praxis_branch(parent=parent):
            pass
        return

    pk_picked_units = [p['pk_org_unit'] for p in picks]
    pk_branches_to_keep = [
        b['pk_praxis_branch'] for b in gmPraxis.get_praxis_branches()
        if b['pk_org_unit'] in pk_picked_units
    ]
    if len(pk_branches_to_keep) == 0:
        if not gmPraxis.delete_praxis_branches():
            gmGuiHelpers.gm_show_error(error=failed_delete_msg,
                                       title=_('Configuring praxis ...'))
            return False
    else:
        if not gmPraxis.delete_praxis_branches(
                except_pk_praxis_branches=pk_branches_to_keep):
            gmGuiHelpers.gm_show_error(error=failed_delete_msg,
                                       title=_('Configuring praxis ...'))
            return False
    gmPraxis.create_praxis_branches(pk_org_units=pk_picked_units)

    # detect whether active branch in kept branches
    if gmPraxis.gmCurrentPraxisBranch(
    )['pk_praxis_branch'] in pk_branches_to_keep:
        return

    while not set_active_praxis_branch(parent=parent):
        pass
Example #2
0
def manage_praxis_branches(parent=None):

	if parent is None:
		parent = wx.GetApp().GetTopWindow()

	#---------------------------
	def get_unit_tooltip(unit):
		if unit is None:
			return None
		return u'\n'.join(unit.format(with_address = True, with_org = True, with_comms = True))
	#---------------------------
	def manage_orgs():
		gmOrganizationWidgets.manage_orgs(parent = parent)
	#---------------------------

	branches = gmPraxis.get_praxis_branches()
	org = branches[0].organization
	praxis = branches[0]['praxis']

	msg = _(
		'Pick those units of "%s" which are branches of your praxis !\n'
		'\n'
		'Note that no other client should be connected at this time.\n'
		'\n'
		'If you want to select another organization entirely\n'
		'first remove all existing branches.\n'
	) % praxis

	picker = gmListWidgets.cItemPickerDlg(parent, -1, msg = msg)
	picker.extra_button = (
		_('Manage units'),
		_('Manage organizations and their units'),
		manage_orgs
	)
	picker.left_item_tooltip_callback = get_unit_tooltip
	picker.right_item_tooltip_callback = get_unit_tooltip
	picker.set_columns(columns = [_('Units of "%s"') % praxis], columns_right = [_('Branches of your praxis')])
	units = org.units
	branch_unit_pks = [b['pk_org_unit'] for b in branches]
	branch_units = []
	for unit in units:
		if unit['pk_org_unit'] in branch_unit_pks:
			branch_units.append(unit)
	items = [ u'%s%s' % (u['unit'], gmTools.coalesce(u['l10n_unit_category'], u'', u' (%s)')) for u in units ]
	picker.set_choices(choices = items, data = units)
	items = [ u'%s%s' % (u['unit'], gmTools.coalesce(u['l10n_unit_category'], u'', u' (%s)')) for u in branch_units ]
	picker.set_picks(picks = items, data = branch_units)
	del units
	del branch_unit_pks
	del branch_units
	del items

	result = picker.ShowModal()

	if result == wx.ID_CANCEL:
		picker.Destroy()
		return None

	picks = picker.picks
	picker.Destroy()

	failed_delete_msg = _(
		'Cannot delete praxis branch(es).\n'
		'\n'
		'There are probably clients logged in\n'
		'from other locations. You need to log out\n'
		'all but this client before the praxis can\n'
		'be reconfigured.'
	)

	if len(picks) == 0:
		if not gmPraxis.delete_praxis_branches():
			gmGuiHelpers.gm_show_error (
				error = failed_delete_msg,
				title = _('Configuring praxis ...')
			)
			return False
		while not set_active_praxis_branch(parent = parent):
			pass
		return

	pk_picked_units = [p['pk_org_unit'] for p in picks]
	pk_branches_to_keep = [
		b['pk_praxis_branch'] for b in gmPraxis.get_praxis_branches()
		if b['pk_org_unit'] in pk_picked_units
	]
	if len(pk_branches_to_keep) == 0:
		if not gmPraxis.delete_praxis_branches():
			gmGuiHelpers.gm_show_error (
				error = failed_delete_msg,
				title = _('Configuring praxis ...')
			)
			return False
	else:
		if not gmPraxis.delete_praxis_branches(except_pk_praxis_branches = pk_branches_to_keep):
			gmGuiHelpers.gm_show_error (
				error = failed_delete_msg,
				title = _('Configuring praxis ...')
			)
			return False
	gmPraxis.create_praxis_branches(pk_org_units = pk_picked_units)

	# detect whether active branch in kept branches
	if gmPraxis.gmCurrentPraxisBranch()['pk_praxis_branch'] in pk_branches_to_keep:
		return

	while not set_active_praxis_branch(parent = parent):
		pass