Skip to content

Commit

Permalink
adds validation to constrain the ability to apply for the same positi…
Browse files Browse the repository at this point in the history
…on twice
  • Loading branch information
Snorre98 committed Nov 3, 2024
1 parent b1baff2 commit a7a17f4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/samfundet/models/recruitment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down

0 comments on commit a7a17f4

Please sign in to comment.