diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 75c1c7cf5..2b7c0db1c 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -122,7 +122,6 @@ def test_tender_wizard_form_all_good_authenticated(self): self.assertEqual(tender.contact_email, self.user_buyer.email) self.assertEqual(tender.contact_phone, self.user_buyer.phone) - def test_tender_wizard_form_not_created(self): self.client.force_login(self.user_buyer) tenders_step_data = self._generate_fake_data_form() diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index 55a2f2990..8feeee1cb 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -152,6 +152,15 @@ def get_context_data(self, form, **kwargs): def save_instance_tender(self, tender_dict: dict, form_dict: dict, is_draft: bool): tender_status = tender_constants.STATUS_DRAFT if is_draft else tender_constants.STATUS_PUBLISHED tender_published_at = None if is_draft else timezone.now() + + if self.request.user.is_authenticated: + tender_dict |= { + "contact_first_name": self.request.user.first_name, + "contact_last_name": self.request.user.last_name, + "contact_email": self.request.user.email, + "contact_phone": self.request.user.phone, + } + if self.instance.id: # update self.instance.status = tender_status @@ -161,38 +170,26 @@ def save_instance_tender(self, tender_dict: dict, form_dict: dict, is_draft: boo if model_form.has_changed(): if step != self.STEP_SURVEY: for attribute in model_form.changed_data: - if attribute == "sectors": - sectors = tender_dict.get("sectors", None) - self.instance.sectors.set(sectors) - elif attribute == "location": - location = tender_dict.get("location") - self.instance.location = location - self.instance.perimeters.set([location]) - elif attribute == "questions_list": - update_or_create_questions_list( - tender=self.instance, questions_list=tender_dict.get("questions_list") - ) - else: - setattr(self.instance, attribute, tender_dict.get(attribute)) + match attribute: + case "sectors": + sectors = tender_dict.get("sectors", None) + self.instance.sectors.set(sectors) + case "location": + location = tender_dict.get("location") + self.instance.location = location + self.instance.perimeters.set([location]) + case "questions_list": + update_or_create_questions_list( + tender=self.instance, questions_list=tender_dict.get("questions_list") + ) + case _: + setattr(self.instance, attribute, tender_dict.get(attribute)) elif step == self.STEP_SURVEY: setattr(self.instance, "scale_marche_useless", tender_dict.get("scale_marche_useless")) self.instance.extra_data.update(tender_dict.get("extra_data")) - if self.request.user.is_authenticated: - self.instance.contact_first_name = self.request.user.first_name - self.instance.contact_last_name = self.request.user.last_name - self.instance.contact_email = self.request.user.email - self.instance.contact_phone = self.request.user.phone - self.instance.save() else: tender_dict |= {"status": tender_status, "published_at": tender_published_at} - if self.request.user.is_authenticated: - tender_dict |= { - "contact_first_name": self.request.user.first_name, - "contact_last_name": self.request.user.last_name, - "contact_email": self.request.user.email, - "contact_phone": self.request.user.phone, - } self.instance = create_tender_from_dict(tender_dict) def done(self, _, form_dict, **kwargs):