Skip to content

Commit

Permalink
fix clean method
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Sep 27, 2023
1 parent 4d69296 commit cd5c9bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
22 changes: 11 additions & 11 deletions lemarche/www/tenders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,28 +122,29 @@ 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."
)


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
Expand Down
4 changes: 1 addition & 3 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ 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:
Expand Down

0 comments on commit cd5c9bf

Please sign in to comment.