Exemple #1
0
def confirm_merge(model_admin, request):
    """
    View for confirming the merge before it is actually performed.

    This view displays the changes that would be made if the merge proceeds, and asks
    the user to confirm if they want to go ahead.

    Note that the source and target companies are passed in via the query string.
    """
    if not model_admin.has_change_permission(request):
        raise PermissionDenied

    target_company = model_admin.get_object(request,
                                            request.GET.get('target_company'))
    source_company = model_admin.get_object(request,
                                            request.GET.get('source_company'))

    if not (source_company and target_company):
        raise SuspiciousOperation()

    is_post = request.method == 'POST'

    if is_post and _perform_merge(request, source_company, target_company,
                                  model_admin):
        changelist_route_name = admin_urlname(model_admin.model._meta,
                                              'changelist')
        changelist_url = reverse(changelist_route_name)
        return HttpResponseRedirect(changelist_url)

    template_name = 'admin/company/company/merge/step_3_confirm_selection.html'
    title = gettext_lazy('Confirm merge')

    planned_merge_results, should_archive_source = get_planned_changes(
        source_company)
    merge_entries = transform_merge_results_to_merge_entry_summaries(
        planned_merge_results,
        skip_zeroes=True,
    )

    context = {
        **model_admin.admin_site.each_context(request),
        'source_company': source_company,
        'target_company': target_company,
        'merge_entries': merge_entries,
        'should_archive_source': should_archive_source,
        'media': model_admin.media,
        'opts': model_admin.model._meta,
        'title': title,
    }
    return TemplateResponse(request, template_name, context)
Exemple #2
0
    def test_get_planned_changes(
        self,
        source_company_factory,
        expected_result,
        expected_should_archive,
    ):
        """
        Tests that get_planned_changes() returns the correct planned changes for various
        cases.
        """
        source_company = source_company_factory()
        merge_results = get_planned_changes(source_company)

        expected_planned_merge_results = (expected_result, expected_should_archive)
        assert merge_results == expected_planned_merge_results
Exemple #3
0
def _build_option_context(source_company, target_company):
    merge_results, _ = get_planned_changes(target_company)
    merge_entries = transform_merge_results_to_merge_entry_summaries(
        merge_results)

    is_source_valid = is_company_a_valid_merge_source(source_company)
    is_target_valid = is_company_a_valid_merge_target(target_company)

    return {
        'target': target_company,
        'merge_entries': merge_entries,
        'is_source_valid': is_source_valid,
        'is_target_valid': is_target_valid,
        'is_valid': is_source_valid and is_target_valid,
    }