From a7a17f4374a461291d0efbccb82f3190b228d724 Mon Sep 17 00:00:00 2001 From: Snorre Jr Date: Mon, 4 Nov 2024 00:42:38 +0100 Subject: [PATCH] adds validation to constrain the ability to apply for the same position twice --- backend/samfundet/models/recruitment.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/samfundet/models/recruitment.py b/backend/samfundet/models/recruitment.py index 065ae8de6..1a3cc15f2 100644 --- a/backend/samfundet/models/recruitment.py +++ b/backend/samfundet/models/recruitment.py @@ -578,6 +578,7 @@ def clean(self, *args: tuple, **kwargs: dict) -> None: # Don't validate withdrawn applications except for deadline if not self.withdrawn and self.applicant_priority: self._validate_priority(errors) + self._validate_no_duplicate_position(errors) if self.recruitment.max_applications: self._validate_application_limits(errors) @@ -587,6 +588,18 @@ def clean(self, *args: tuple, **kwargs: dict) -> None: if errors: raise ValidationError(errors) + def _validate_no_duplicate_position(self, errors: dict[str, list[str]]) -> None: + """ + Validates that the user hasn't already applied to this position, + while allowing updates to existing applications + """ + existing = ( + RecruitmentApplication.objects.filter(user=self.user, recruitment_position=self.recruitment_position, withdrawn=False).exclude(pk=self.pk).exists() + ) + + if existing: + errors['recruitment_position'].append('You have already applied for this position') + def _validate_priority(self, errors: dict[str, list[str]]) -> None: """ Validates that the application's priority is within allowed bounds.