diff --git a/supplier_app/constants/custom_messages.py b/supplier_app/constants/custom_messages.py index bf0ef4d..a19ad48 100644 --- a/supplier_app/constants/custom_messages.py +++ b/supplier_app/constants/custom_messages.py @@ -21,3 +21,5 @@ TAXPAYER_WORKDAY_UNIQUE_ERROR = _("There is other taxpayer with that workday id") THANKS = _("Thank you. We'll get back to you shortly.") + +COMPANY_ALREADY_EXIST = _('That company name and/or email is already in use!') diff --git a/supplier_app/tests/test_view.py b/supplier_app/tests/test_view.py index bb67fde..9a4ac1b 100644 --- a/supplier_app/tests/test_view.py +++ b/supplier_app/tests/test_view.py @@ -1760,6 +1760,24 @@ def test_valid_company_creation(self): self.company_constants['name'] ) + def test_duplicated_company_creation(self): + self.client.force_login(self.ap_user) + self._make_post() + self._make_post() + self.assertEqual( + len(Company.objects.filter(name='Eventbrite')), + 1 + ) + + def test_duplicated_company_creation_redirect_to_self(self): + self.client.force_login(self.ap_user) + self._make_post() + response = self._make_post() + self.assertEqual( + response.redirect_chain[0], + (reverse('company-create'), HTTPStatus.FOUND) + ) + def test_valid_company_creation_as_buyer(self): self.client.force_login(self.user_buyer_with_google_social) self._make_post() diff --git a/supplier_app/views.py b/supplier_app/views.py index 2826c89..f308921 100644 --- a/supplier_app/views.py +++ b/supplier_app/views.py @@ -36,6 +36,7 @@ TAXPAYER_WITHOUT_WORKDAY_ID_MESSAGE, TAXPAYER_WORKDAY_UNIQUE_ERROR, THANKS, + COMPANY_ALREADY_EXIST, ) from supplier_app.constants.taxpayer_status import ( TAXPAYER_STATUS_APPROVED, @@ -109,6 +110,8 @@ def get_context_data(self, **kwargs): return context def form_valid(self, form): + if Company.objects.filter(name=form.data['name']).exists(): + return HttpResponseRedirect(self.get_failure_url()) company = self.save_company(form) InvitingBuyer.objects.create(company=company, inviting_buyer=self.request.user) EBEntityCompany.objects.create(company=company, eb_entity=EBEntity.objects.get(pk=form.data['eb_entity'])) @@ -121,6 +124,13 @@ def save_company(self, forms): def get_success_url(self): return reverse('company-list') + + def get_failure_url(self): + messages.error( + self.request, + COMPANY_ALREADY_EXIST, + ) + return reverse('company-create') class CompanyListView(LoginRequiredMixin, ListView): model = Company diff --git a/templates/supplier_app/AP/company_creation.html b/templates/supplier_app/AP/company_creation.html index 0efd554..f917a7d 100644 --- a/templates/supplier_app/AP/company_creation.html +++ b/templates/supplier_app/AP/company_creation.html @@ -3,6 +3,7 @@ {% block content %} {% include 'navbar.html' %} +{% include 'messages_notification.html' %}