diff --git a/lemarche/templates/tenders/create_step_contact.html b/lemarche/templates/tenders/create_step_contact.html index e739a9fbd..ec83009f6 100644 --- a/lemarche/templates/tenders/create_step_contact.html +++ b/lemarche/templates/tenders/create_step_contact.html @@ -9,15 +9,19 @@ {% csrf_token %}
-
- {% bootstrap_field form.contact_first_name form_group_class="form-group col-12 col-md-6" %} - {% bootstrap_field form.contact_last_name form_group_class="form-group col-12 col-md-6" %} -
+ {% if not user.is_authenticated %} +
+ {% bootstrap_field form.contact_first_name form_group_class="form-group col-12 col-md-6" %} + {% bootstrap_field form.contact_last_name form_group_class="form-group col-12 col-md-6" %} +
+ {% endif %} {% bootstrap_field form.contact_company_name %} -
- {% bootstrap_field form.contact_email form_group_class="form-group col-12 col-md-6" %} - {% bootstrap_field form.contact_phone form_group_class="form-group col-12 col-md-6" %} -
+ {% if not user.is_authenticated %} +
+ {% bootstrap_field form.contact_email form_group_class="form-group col-12 col-md-6" %} + {% bootstrap_field form.contact_phone form_group_class="form-group col-12 col-md-6" %} +
+ {% endif %} {% bootstrap_field form.response_kind %} {% bootstrap_field form.deadline_date %}
diff --git a/lemarche/www/tenders/forms.py b/lemarche/www/tenders/forms.py index 388dce1a4..7fee4bc25 100644 --- a/lemarche/www/tenders/forms.py +++ b/lemarche/www/tenders/forms.py @@ -145,29 +145,36 @@ class Meta: widgets = { "deadline_date": forms.widgets.DateInput(attrs={"class": "form-control", "type": "date"}), } + labels = { + "contact_first_name": "Prénom", + "contact_last_name": "Nom", + "contact_email": "E-mail", + "contact_phone": "Téléphone", + } def __init__(self, max_deadline_date, external_link, user: User, *args, **kwargs): super().__init__(*args, **kwargs) self.max_deadline_date = max_deadline_date self.external_link = external_link + self.user = user user_is_anonymous = not user.is_authenticated if self.instance.deadline_date: self.initial["deadline_date"] = self.instance.deadline_date.isoformat() # required fields - self.fields["contact_first_name"].required = True - self.fields["contact_last_name"].required = True self.fields["response_kind"].required = True self.fields["deadline_date"].required = True if user_is_anonymous: + self.fields["contact_first_name"].required = True + self.fields["contact_last_name"].required = True self.fields["contact_email"].required = True self.fields["contact_phone"].required = True else: - self.initial["contact_first_name"] = user.first_name - self.initial["contact_last_name"] = user.last_name - self.initial["contact_email"] = user.email - self.initial["contact_phone"] = user.phone + del self.fields["contact_first_name"] + 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 if user_does_not_have_company_name: @@ -194,18 +201,28 @@ def clean(self): self.add_error( "deadline_date", "La date de clôture des réponses ne doit pas être antérieure à aujourd'hui." ) - # contact_email must be filled if RESPONSE_KIND_TEL - if self.cleaned_data.get("response_kind") and ( - Tender.RESPONSE_KIND_EMAIL in self.cleaned_data.get("response_kind") - and not self.cleaned_data.get("contact_email") - ): - self.add_error("response_kind", "E-mail sélectionné mais aucun e-mail renseigné.") - # contact_phone must be filled if RESPONSE_KIND_TEL - if self.cleaned_data.get("response_kind") and ( - Tender.RESPONSE_KIND_TEL in self.cleaned_data.get("response_kind") - and not self.cleaned_data.get("contact_phone") - ): - self.add_error("response_kind", "Téléphone sélectionné mais aucun téléphone renseigné.") + + if not self.user.is_authenticated: + # contact_email must be filled if RESPONSE_KIND_EMAIL + if self.cleaned_data.get("response_kind") and ( + Tender.RESPONSE_KIND_EMAIL in self.cleaned_data.get("response_kind") + and not self.cleaned_data.get("contact_email") + ): + self.add_error("response_kind", "E-mail sélectionné mais aucun e-mail renseigné.") + # contact_phone must be filled if RESPONSE_KIND_TEL + if self.cleaned_data.get("response_kind") and ( + Tender.RESPONSE_KIND_TEL in self.cleaned_data.get("response_kind") + and not self.cleaned_data.get("contact_phone") + ): + self.add_error("response_kind", "Téléphone sélectionné mais aucun téléphone renseigné.") + elif not self.user.phone: + if self.cleaned_data.get("response_kind") and ( + Tender.RESPONSE_KIND_TEL in self.cleaned_data.get("response_kind") + ): + self.add_error( + "response_kind", "Téléphone sélectionné mais aucun téléphone renseigné dans votre profil." + ) + # external_link must be filled if RESPONSE_KIND_EXTERNAL if self.cleaned_data.get("response_kind") and ( Tender.RESPONSE_KIND_EXTERNAL in self.cleaned_data.get("response_kind") and not self.external_link diff --git a/lemarche/www/tenders/tests.py b/lemarche/www/tenders/tests.py index 844aa60a1..922c2dded 100644 --- a/lemarche/www/tenders/tests.py +++ b/lemarche/www/tenders/tests.py @@ -120,6 +120,10 @@ def test_tender_wizard_form_all_good_authenticated(self): TenderCreateMultiStepView, tenders_step_data, tender, is_draft=False ), ) + self.assertEqual(tender.contact_first_name, self.user_buyer.first_name) + self.assertEqual(tender.contact_last_name, self.user_buyer.last_name) + 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) diff --git a/lemarche/www/tenders/views.py b/lemarche/www/tenders/views.py index 69ae9dfd0..f3daa8edc 100644 --- a/lemarche/www/tenders/views.py +++ b/lemarche/www/tenders/views.py @@ -153,6 +153,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 @@ -162,19 +171,20 @@ 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")) diff --git a/pyproject.toml b/pyproject.toml index 9b80c88a5..ff077b0d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,7 +87,7 @@ use_parentheses = true [tool.black] line-length = 119 -target-version = ['py38', 'py39'] +target-version = ['py310'] include = '\.pyi?$' [tool.pytest.ini_options]