diff --git a/backend/app/urls.py b/backend/app/urls.py index 1abb9cb1..faa1cee0 100644 --- a/backend/app/urls.py +++ b/backend/app/urls.py @@ -36,6 +36,7 @@ path('quiz/review//', quiz_views.get_quiz_review, name="review_quiz"), path('quiz/recommend//', quiz_views.get_quiz_recommendations, name="recommend_quiz"), path('quiz/review_latest//', quiz_views.get_latest_quiz_review, name="review_latest_quiz"), + path('quiz/cancel/', quiz_views.cancel_quiz, name="cancel_quiz"), path('create-post/',create_post, name='create_post'), diff --git a/backend/app/views_directory/quiz_views.py b/backend/app/views_directory/quiz_views.py index 2c8de1c4..eba37850 100644 --- a/backend/app/views_directory/quiz_views.py +++ b/backend/app/views_directory/quiz_views.py @@ -95,6 +95,21 @@ def submit_quiz(request): return Response({'result_url': result_url}, status=status.HTTP_200_OK) +@api_view(['POST']) +@permission_classes([IsAuthenticated]) +def cancel_quiz(request): + # check if quiz progress is completed + quiz_progress = get_object_or_404(QuizProgress, id=request.data['quiz_progress_id']) + if quiz_progress.completed: + return Response({'error': 'Quiz already submitted.'}, status=status.HTTP_400_BAD_REQUEST) + # check if quiz progress is user's + if quiz_progress.user != request.user: + return Response({'error': 'Unauthorized'}, status=status.HTTP_401_UNAUTHORIZED) + + quiz_progress.delete() + return Response({'message': 'Quiz progress deleted'}, status=status.HTTP_200_OK) + + @api_view(['GET']) @permission_classes([IsAuthenticated]) def get_quiz_results(request):