Skip to content

Commit

Permalink
[Dépôt de besoin] Simplification du formulaire en supprimant les info…
Browse files Browse the repository at this point in the history
…rmations de contact pour les utilisateurs connectés (#916)

* tender form : remove first_name and last_name field

* introduce match to have beautiful code and avoid too complexe flake8 alert

* anonymous users must fill their first and last name

* lighten the contact labels

* raise error if the user does not have a phone
  • Loading branch information
SebastienReuiller authored Sep 26, 2023
1 parent de9481a commit 8274b6c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 40 deletions.
20 changes: 12 additions & 8 deletions lemarche/templates/tenders/create_step_contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
{% csrf_token %}
<div class="row">
<div class="col-12 col-lg-7">
<div class="form-row">
{% 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" %}
</div>
{% if not user.is_authenticated %}
<div class="form-row">
{% 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" %}
</div>
{% endif %}
{% bootstrap_field form.contact_company_name %}
<div class="form-row">
{% 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" %}
</div>
{% if not user.is_authenticated %}
<div class="form-row">
{% 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" %}
</div>
{% endif %}
{% bootstrap_field form.response_kind %}
{% bootstrap_field form.deadline_date %}
</div>
Expand Down
53 changes: 35 additions & 18 deletions lemarche/www/tenders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 23 additions & 13 deletions lemarche/www/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 8274b6c

Please sign in to comment.