From fd488e93f44f766314607453bd8169449430db7a Mon Sep 17 00:00:00 2001 From: Steve Brownlee Date: Sun, 25 Feb 2024 22:31:39 -0600 Subject: [PATCH] Catch and pass exceptions related to unique key contraints --- LearningAPI/views/student_view.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/LearningAPI/views/student_view.py b/LearningAPI/views/student_view.py index 34f69f5..e488360 100644 --- a/LearningAPI/views/student_view.py +++ b/LearningAPI/views/student_view.py @@ -4,6 +4,7 @@ import logging import requests from django.db import connection +from django.db import IntegrityError from django.http import HttpResponseServerError from django.utils.decorators import method_decorator from rest_framework import serializers, status @@ -215,11 +216,14 @@ def assess(self, request, pk): # 2. Assign all objectives/weights to the student as complete assessment_objectives = latest_assessment.assessment.objectives.all() for objective in assessment_objectives: - LearningRecord.objects.create( - student=student, - weight=objective, - achieved=True, - ) + try: + LearningRecord.objects.create( + student=student, + weight=objective, + achieved=True, + ) + except IntegrityError: + pass except Exception: return Response({'message': 'Updated, but no Slack message sent'}, status=status.HTTP_204_NO_CONTENT)