Skip to content

Commit

Permalink
Use JobSeekerProfileFieldMixin in JobSeekerPersonalDataForm
Browse files Browse the repository at this point in the history
Simplify the inheritance for JobSeekerNIRUpdateMixin, as all forms are
now based on the User model, and consolidate implementation of user
profile fields on JobSeekerProfileFieldMixin.

Because fields are now added to JobSeekerPersonalDataForm dynamically by
the JobSeekerProfileFieldMixin, `base_fields` are empty. Use the
birthdate field on the form instance to perform the validation. That’s
also a bit safer, as we don’t risk making changes to the reused
base_fields for other requests.
  • Loading branch information
francoisfreitag committed Jan 23, 2025
1 parent 93f028b commit 5647265
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
5 changes: 1 addition & 4 deletions itou/common_apps/nir/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class JobSeekerNIRUpdateMixin:
nir & lack_of_nir_reason must be declared in the form's Meta.fields
"""

def get_user_instance(self):
return self.instance

def __init__(self, *args, editor=None, tally_form_query=None, **kwargs):
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -60,7 +57,7 @@ def __init__(self, *args, editor=None, tally_form_query=None, **kwargs):
# Disable NIR editing altogether if the job seeker already has one
self.fields["nir"].disabled = True
self.fields["lack_of_nir"].widget = forms.HiddenInput()
user_instance = self.get_user_instance()
user_instance = self.instance
if user_instance.pk:
# These messages should only appear when updating a job seeker
# and not when creating one
Expand Down
19 changes: 10 additions & 9 deletions itou/www/apply/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from itou.files.forms import ItouFileField
from itou.job_applications import enums as job_applications_enums
from itou.job_applications.models import JobApplication, PriorAction
from itou.users.models import JobSeekerProfile
from itou.users.forms import JobSeekerProfileFieldsMixin
from itou.users.models import JobSeekerProfile, User
from itou.utils import constants as global_constants
from itou.utils.templatetags.str_filters import mask_unless
from itou.utils.types import InclusiveDateRange
Expand Down Expand Up @@ -578,28 +579,28 @@ def clean(self):
return cleaned_data


class JobSeekerPersonalDataForm(JobSeekerNIRUpdateMixin, forms.ModelForm):
class JobSeekerPersonalDataForm(JobSeekerNIRUpdateMixin, JobSeekerProfileFieldsMixin, forms.ModelForm):
"""
Info that will be used to search for an existing Pôle emploi approval.
"""

PROFILE_FIELDS = ["birthdate", "pole_emploi_id", "lack_of_pole_emploi_id_reason", "nir", "lack_of_nir_reason"]

class Meta:
model = JobSeekerProfile
fields = ["birthdate", "pole_emploi_id", "lack_of_pole_emploi_id_reason", "nir", "lack_of_nir_reason"]
help_texts = {"birthdate": "Au format JJ/MM/AAAA, par exemple 20/12/1978."}
model = User
fields = []

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["birthdate"].widget = DuetDatePickerWidget(
birthdate = self.fields["birthdate"]
birthdate.help_text = "Au format JJ/MM/AAAA, par exemple 20/12/1978."
birthdate.widget = DuetDatePickerWidget(
attrs={
"min": DuetDatePickerWidget.min_birthdate(),
"max": DuetDatePickerWidget.max_birthdate(),
}
)

def get_user_instance(self):
return self.instance.user

def clean(self):
super().clean()
JobSeekerProfile.clean_pole_emploi_fields(self.cleaned_data)
Expand Down
6 changes: 2 additions & 4 deletions itou/www/apply/views/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ def _accept(request, company, job_seeker, error_url, back_url, template_name, ex
valid_diagnosis = EligibilityDiagnosis.objects.last_considered_valid(job_seeker=job_seeker, for_siae=company)
# Info that will be used to search for an existing Pôle emploi approval.
form_personal_data = JobSeekerPersonalDataForm(
instance=job_seeker.jobseeker_profile,
instance=job_seeker,
data=request.POST or None,
tally_form_query=f"jobapplication={job_application.pk}" if job_application else None,
)
forms.append(form_personal_data)
try:
birthdate = JobSeekerPersonalDataForm.base_fields["birthdate"].clean(
form_personal_data.data.get("birthdate")
)
birthdate = form_personal_data.fields["birthdate"].clean(form_personal_data.data.get("birthdate"))
except ValidationError:
pass # will be presented to user later

Expand Down

0 comments on commit 5647265

Please sign in to comment.