-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
458 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from django import forms | ||
|
||
from itou.companies.enums import CompanyKind | ||
from itou.eligibility.enums import AuthorKind | ||
from itou.eligibility.models.geiq import GEIQEligibilityDiagnosis | ||
from itou.eligibility.models.iae import EligibilityDiagnosis | ||
from itou.users.enums import UserKind | ||
|
||
|
||
class GEIQEligibilityDiagnosisAdminForm(forms.ModelForm): | ||
class Meta: | ||
model = GEIQEligibilityDiagnosis | ||
fields = ["job_seeker", "author", "author_kind", "author_prescriber_organization", "author_geiq"] | ||
|
||
def clean_author_kind(self): | ||
value = self.cleaned_data["author_kind"] | ||
if value not in [AuthorKind.GEIQ, AuthorKind.PRESCRIBER]: | ||
raise forms.ValidationError("Un diagnostic GEIQ ne peut pas avoir ce type d'auteur.") | ||
return value | ||
|
||
def clean_job_seeker(self): | ||
value = self.cleaned_data["job_seeker"] | ||
if value.kind != UserKind.JOB_SEEKER: | ||
raise forms.ValidationError("L'utilisateur doit être un candidat") | ||
return value | ||
|
||
def clean(self): | ||
super().clean() | ||
|
||
author = self.cleaned_data.get("author") | ||
author_kind = self.cleaned_data.get("author_kind") | ||
author_prescriber_organization = self.cleaned_data.get("author_prescriber_organization") | ||
author_geiq = self.cleaned_data.get("author_geiq") | ||
|
||
if author and author_kind: | ||
if author.kind == UserKind.PRESCRIBER: | ||
if not author_kind == AuthorKind.PRESCRIBER: | ||
self.add_error("author_kind", "Le type ne correspond pas à l'utilisateur.") | ||
if not author_prescriber_organization or not author_prescriber_organization.is_authorized: | ||
self.add_error( | ||
"author_prescriber_organization", | ||
"Une organisation prescriptrice est obligatoire pour ce type d'auteur.", | ||
) | ||
if ( | ||
author_prescriber_organization | ||
and not author_prescriber_organization.memberships.filter(user=author, is_active=True).exists() | ||
): | ||
self.add_error("author_prescriber_organization", "L'auteur n'appartient pas à cette organisation.") | ||
elif author.kind == UserKind.EMPLOYER: | ||
if not author_kind == AuthorKind.GEIQ: | ||
self.add_error("author_kind", "Le type ne correspond pas à l'utilisateur.") | ||
if not author_geiq: | ||
self.add_error("author_geiq", "Une entreprise GEIQ est obligatoire pour ce type d'auteur") | ||
elif not author_geiq.memberships.filter(user=author, is_active=True).exists(): | ||
self.add_error("author_geiq", "L'auteur n'appartient pas à cette structure.") | ||
else: # Any other kind | ||
self.add_error("author", "Seul un prescripteur ou employeur peut être auteur d'un diagnostic.") | ||
|
||
|
||
class IAEEligibilityDiagnosisAdminForm(forms.ModelForm): | ||
class Meta: | ||
model = EligibilityDiagnosis | ||
fields = ["job_seeker", "author", "author_kind", "author_prescriber_organization", "author_siae"] | ||
|
||
def clean_author_kind(self): | ||
value = self.cleaned_data["author_kind"] | ||
if value not in [AuthorKind.EMPLOYER, AuthorKind.PRESCRIBER]: | ||
raise forms.ValidationError("Un diagnostic IAE ne peut pas avoir ce type d'auteur.") | ||
return value | ||
|
||
def clean_job_seeker(self): | ||
value = self.cleaned_data["job_seeker"] | ||
if value.kind != UserKind.JOB_SEEKER: | ||
raise forms.ValidationError("L'utilisateur doit être un candidat") | ||
return value | ||
|
||
def clean(self): | ||
super().clean() | ||
|
||
author = self.cleaned_data.get("author") | ||
author_kind = self.cleaned_data.get("author_kind") | ||
author_prescriber_organization = self.cleaned_data.get("author_prescriber_organization") | ||
author_siae = self.cleaned_data.get("author_siae") | ||
|
||
if author and author_kind: | ||
if author.kind == UserKind.PRESCRIBER: | ||
if not author_kind == AuthorKind.PRESCRIBER: | ||
self.add_error("author_kind", "Le type ne correspond pas à l'utilisateur.") | ||
if not author_prescriber_organization or not author_prescriber_organization.is_authorized: | ||
self.add_error( | ||
"author_prescriber_organization", | ||
"Une organisation prescriptrice est obligatoire pour ce type d'auteur.", | ||
) | ||
if ( | ||
author_prescriber_organization | ||
and not author_prescriber_organization.memberships.filter(user=author, is_active=True).exists() | ||
): | ||
self.add_error("author_prescriber_organization", "L'auteur n'appartient pas à cette organisation.") | ||
elif author.kind == UserKind.EMPLOYER: | ||
if not author_kind == AuthorKind.EMPLOYER: | ||
self.add_error("author_kind", "Le type ne correspond pas à l'utilisateur.") | ||
if not author_siae: | ||
self.add_error("author_siae", "Une SIAE est obligatoire pour ce type d'auteur.") | ||
elif not author_siae.memberships.filter(user=author, is_active=True).exists(): | ||
self.add_error("author_siae", "L'auteur n'appartient pas à cette structure.") | ||
else: # Any other kind | ||
self.add_error("author", "Seul un prescripteur ou employeur peut être auteur d'un diagnostic.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.