From 83aa662ce353fa0a15767b05dfda4e77a09f2217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Wed, 27 Sep 2023 17:35:33 +0200 Subject: [PATCH] fix clean method --- lemarche/www/tenders/forms.py | 22 +++++++++++----------- lemarche/www/tenders/views.py | 7 ++----- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lemarche/www/tenders/forms.py b/lemarche/www/tenders/forms.py index 7092ad8b5..dd1bc6d84 100644 --- a/lemarche/www/tenders/forms.py +++ b/lemarche/www/tenders/forms.py @@ -84,9 +84,8 @@ class Meta: "amount": forms.Select(attrs={"x-model": "formData.amount", "x-on:change": "getImpactMessage()"}), } - def __init__(self, max_deadline_date, kind, questions_list=None, *args, **kwargs): + def __init__(self, kind, questions_list=None, *args, **kwargs): super().__init__(*args, **kwargs) - self.max_deadline_date = max_deadline_date self.kind = kind # required fields @@ -123,20 +122,22 @@ def clean_questions_list(self): questions.pop(index) return questions - def clean_deadline_date(self): + def clean(self): + super().clean() today = date.today() + max_deadline_date = self.cleaned_data.get("start_working_date") + deadline_date = self.cleaned_data.get("deadline_date") # check that deadline_date < start_working_date - if ( - self.max_deadline_date - and self.cleaned_data.get("deadline_date") - and (self.cleaned_data.get("deadline_date") > self.max_deadline_date) - ): + if max_deadline_date and deadline_date and (deadline_date > max_deadline_date): self.add_error( "deadline_date", - f"La date de clôture des réponses ne doit pas être supérieure à la date de début d'intervention ({self.max_deadline_date}).", # noqa + ( + "La date de clôture des réponses ne doit pas être supérieure à la date " + f"de début d'intervention ({max_deadline_date})." + ), ) # check that deadline_date > today - if self.cleaned_data.get("deadline_date") and (self.cleaned_data.get("deadline_date") < today): + if deadline_date and (deadline_date < today): self.add_error( "deadline_date", "La date de clôture des réponses ne doit pas être antérieure à aujourd'hui." ) @@ -144,7 +145,6 @@ def clean_deadline_date(self): class TenderCreateStepContactForm(forms.ModelForm): # fields from previous step - max_deadline_date = None external_link = None user_is_anonymous = None user_does_not_have_company_name = None diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index 31b907aad..a499fe91b 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -104,14 +104,11 @@ def get_form_kwargs(self, step): """ kwargs = super().get_form_kwargs(step) if step == self.STEP_DETAIL: - cleaned_data_general = self.get_cleaned_data_for_step(self.STEP_GENERAL) - kwargs["max_deadline_date"] = cleaned_data_general.get("start_working_date") - kwargs["kind"] = cleaned_data_general.get("kind") + kwargs["kind"] = self.get_cleaned_data_for_step(self.STEP_GENERAL).get("kind") if self.instance.id: kwargs["questions_list"] = list(self.instance.questions_list()) if step == self.STEP_CONTACT: - cleaned_data_detail = self.get_cleaned_data_for_step(self.STEP_DETAIL) - kwargs["external_link"] = cleaned_data_detail.get("external_link") + kwargs["external_link"] = self.get_cleaned_data_for_step(self.STEP_DETAIL).get("external_link") kwargs["user"] = self.request.user return kwargs