Skip to content

Commit

Permalink
feat(Besoins): Formulaire : si un lien externe est fourni, alors forc…
Browse files Browse the repository at this point in the history
…er le choix de réponse (#1164)
  • Loading branch information
raphodn authored Apr 19, 2024
1 parent 18cdc1a commit df05d51
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
29 changes: 18 additions & 11 deletions lemarche/www/tenders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df05d51

Please sign in to comment.