def update_from_dnb(model_admin, request, object_id): """ Tool to let admin users update company with a valid `duns_number` by pulling fresh data from D&B. The company record will be versioned after the update from D&B is applied. The `pending_dnb_investigation` field will be set to False. """ if not model_admin.has_change_permission(request): raise PermissionDenied() dh_company = model_admin.get_object(request, object_id) company_change_page = reverse( admin_urlname(model_admin.model._meta, 'change'), kwargs={'object_id': dh_company.pk}, ) if dh_company is None or dh_company.duns_number is None: raise SuspiciousOperation() try: dnb_company = get_company(dh_company.duns_number, request) except DNBServiceInvalidRequest: message = 'No matching company found in D&B database.' raise AdminException([message], company_change_page) except DNBServiceException: message = 'Something went wrong in an upstream service.' raise AdminException([message], company_change_page) if request.method == 'GET': return TemplateResponse( request, 'admin/company/company/update-from-dnb.html', { **model_admin.admin_site.each_context(request), 'media': model_admin.media, 'opts': model_admin.model._meta, 'object': dh_company, 'title': gettext_lazy('Confirm update from D&B'), 'diff': format_company_diff(dh_company, dnb_company), }, ) try: update_company_from_dnb(dh_company, dnb_company, request.user) return HttpResponseRedirect(company_change_page) except serializers.ValidationError: message = 'Data from D&B did not pass the Data Hub validation checks.' raise AdminException([message], company_change_page)
def _get_company(duns_number, error_url, request=None): try: return get_company(duns_number, request) except DNBServiceInvalidRequest: message = 'No matching company found in D&B database.' raise AdminException([message], error_url) except DNBServiceException: message = 'Something went wrong in an upstream service.' raise AdminException([message], error_url)
def _link_company_with_dnb(dh_company_id, duns_number, user, error_url): # We don't need to catch CompanyAlreadyDNBLinkedException as our form will # do this validation for us try: link_company_with_dnb(dh_company_id, duns_number, user) except serializers.ValidationError: message = 'Data from D&B did not pass the Data Hub validation checks.' raise AdminException([message], error_url) except DNBServiceInvalidRequest: message = 'No matching company found in D&B database.' raise AdminException([message], error_url) except DNBServiceException: message = 'Something went wrong in an upstream service.' raise AdminException([message], error_url)
def dnb_link_review_changes(model_admin, request): """ View to allow users to review changes that would be applied to a record before linking it. POSTs make the link and redirect the user to view the updated record. """ if not model_admin.has_change_permission(request): raise PermissionDenied() company_list_page = reverse( admin_urlname(model_admin.model._meta, 'changelist'), ) form = SelectIdsToLinkForm(data=request.GET) if not form.is_valid(): messages = _build_error_messages(form.errors) raise AdminException(messages, company_list_page) dh_company = form.cleaned_data['company'] duns_number = form.cleaned_data['duns_number'] is_post = request.method == 'POST' if is_post: _link_company_with_dnb(dh_company.pk, duns_number, request.user, company_list_page) django_messages.add_message( request, django_messages.SUCCESS, 'Company linked to D&B successfully.', ) company_change_page = reverse( admin_urlname(model_admin.model._meta, 'change'), kwargs={'object_id': dh_company.pk}, ) return HttpResponseRedirect(company_change_page) dnb_company = _get_company(duns_number, company_list_page, request) return TemplateResponse( request, 'admin/company/company/update-from-dnb.html', { **model_admin.admin_site.each_context(request), 'media': model_admin.media, 'opts': model_admin.model._meta, 'title': gettext_lazy('Confirm Link Company with D&B'), 'diff': format_company_diff(dh_company, dnb_company), }, )