From d0e76d0111bea18176ecaf3211c627f7a081259f Mon Sep 17 00:00:00 2001 From: Tudor Amariei Date: Sun, 17 Nov 2024 13:10:44 +0200 Subject: [PATCH] Fix candidates being registered twice by an organization --- backend/hub/views.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/hub/views.py b/backend/hub/views.py index 10c5ab26..e22067da 100644 --- a/backend/hub/views.py +++ b/backend/hub/views.py @@ -806,7 +806,19 @@ def post(self, request, *args, **kwargs): if check_result: return check_result - return super().post(request, *args, **kwargs) + try: + return super().post(request, *args, **kwargs) + except IntegrityError as exc: + if "hub_candidate_org_id_key" in str(exc): + existing_candidate_pk = request.user.organization.candidate.pk + logger.info( + f"User {request.user} tried to create a new candidate when one already exists. " + f"Redirecting to existing candidate {existing_candidate_pk}." + ) + + return redirect(reverse("candidate-update", args=(existing_candidate_pk,))) + + raise exc def get_form_kwargs(self): kwargs = super(CandidateRegisterRequestCreateView, self).get_form_kwargs()