diff --git a/lemarche/www/tenders/forms.py b/lemarche/www/tenders/forms.py index 155369b91..206f224f5 100644 --- a/lemarche/www/tenders/forms.py +++ b/lemarche/www/tenders/forms.py @@ -90,8 +90,6 @@ class Meta: def __init__(self, kind, questions_list=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.kind = kind - # required fields self.fields["deadline_date"].required = True @@ -105,11 +103,11 @@ def __init__(self, kind, questions_list=None, *args, **kwargs): self.initial["start_working_date"] = self.instance.start_working_date.isoformat() # label, placeholder & help_text - if self.kind != tender_constants.KIND_TENDER: + if kind != tender_constants.KIND_TENDER: self.fields["external_link"].label = "Lien à partager" self.fields["external_link"].help_text = None - if self.kind == tender_constants.KIND_TENDER: + if kind == tender_constants.KIND_TENDER: self.fields["external_link"].required = True self.fields["amount"].label = "Montant € estimé de votre besoin" @@ -154,8 +152,6 @@ def clean(self): class TenderCreateStepContactForm(forms.ModelForm): # fields from previous step external_link = None - user_is_anonymous = None - user_does_not_have_company_name = None user: User = None response_kind = forms.MultipleChoiceField( @@ -183,15 +179,20 @@ class Meta: "contact_phone": "Téléphone", } - def __init__(self, external_link, user: User, *args, **kwargs): + def __init__(self, kind, external_link, user: User, *args, **kwargs): super().__init__(*args, **kwargs) + self.kind = kind self.external_link = external_link self.user = user user_is_anonymous = not user.is_authenticated + user_does_not_have_company_name = user_is_anonymous or not user.company_name - # display response_is_anonymous if the tender is a project - if self.instance and self.instance.kind != tender_constants.KIND_PROJECT: + # display response_is_anonymous only if the tender is a project + if self.instance and (self.instance.kind != tender_constants.KIND_PROJECT): self.fields["response_is_anonymous"].widget = forms.HiddenInput() + # if TENDER & external_link, then response_kind should have RESPONSE_KIND_EXTERNAL + if self.instance and (self.instance.kind == tender_constants.KIND_TENDER) and self.external_link: + self.initial["response_kind"] = [tender_constants.RESPONSE_KIND_EXTERNAL] # required fields if user_is_anonymous: self.fields["contact_first_name"].required = True @@ -203,8 +204,7 @@ def __init__(self, external_link, user: User, *args, **kwargs): del self.fields["contact_last_name"] del self.fields["contact_email"] del self.fields["contact_phone"] - - user_does_not_have_company_name = user_is_anonymous or not user.company_name + # user company_name field if user_does_not_have_company_name: self.fields["contact_company_name"].widget = forms.TextInput() # HiddenInput() by default self.fields["contact_company_name"].required = True @@ -245,6 +245,13 @@ def clean(self): self.add_error( "response_kind", "Lien externe sélectionné mais aucun lien renseigné (à l'étape précédente)." ) + if ( + self.kind == tender_constants.KIND_TENDER + and self.external_link + and self.cleaned_data.get("response_kind") + and not (tender_constants.RESPONSE_KIND_EXTERNAL in self.cleaned_data.get("response_kind")) + ): + self.add_error("response_kind", "Appel d'offre avec lien renseigné.") class TenderCreateStepSurveyForm(forms.ModelForm): diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 82b3fc73f..f9d0ee8ba 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -146,11 +146,20 @@ def test_tender_wizard_form_not_created(self): def test_tender_wizard_form_external_link_validation(self): self.client.force_login(self.user_buyer) tenders_step_data = self._generate_fake_data_form(_step_1={"general-kind": tender_constants.KIND_TENDER}) - # set an external_link with a wrong format + # set a wrong external_link (should be a valid url) tenders_step_data[1]["detail-external_link"] = "test" with self.assertRaises(AssertionError): self._check_every_step(tenders_step_data, final_redirect_page=reverse("siae:search_results")) + def test_tender_wizard_form_tender_with_external_link_response_kind_validation(self): + self.client.force_login(self.user_buyer) + tenders_step_data = self._generate_fake_data_form(_step_1={"general-kind": tender_constants.KIND_TENDER}) + tenders_step_data[1]["detail-external_link"] = "example.com" + # set a wrong reponse_kind (should have RESPONSE_KIND_EXTERNAL) + tenders_step_data[2]["contact-response_kind"] = [] + with self.assertRaises(AssertionError): + self._check_every_step(tenders_step_data, final_redirect_page=reverse("siae:search_results")) + def test_tender_wizard_form_external_link_required_for_tender(self): self.client.force_login(self.user_buyer) tenders_step_data = self._generate_fake_data_form(_step_1={"general-kind": tender_constants.KIND_TENDER}) diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index c057760cd..aec9c47d3 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -112,6 +112,7 @@ def get_form_kwargs(self, step): if self.instance.id: kwargs["questions_list"] = list(self.instance.questions_list()) if step == self.STEP_CONTACT: + kwargs["kind"] = self.get_cleaned_data_for_step(self.STEP_GENERAL).get("kind") kwargs["external_link"] = self.get_cleaned_data_for_step(self.STEP_DETAIL).get("external_link") kwargs["user"] = self.request.user return kwargs