diff --git a/backend/app/serializers.py b/backend/app/serializers.py index a748fcbe..e4c36594 100644 --- a/backend/app/serializers.py +++ b/backend/app/serializers.py @@ -188,12 +188,11 @@ def to_representation(self, instance): total_score = representation.pop('total_score') representation['average_score'] = total_score / instance.times_taken if instance.times_taken > 0 else 0 - # Transform tags to a list of names representation['tags'] = [tag['name'] for tag in representation['tags']] representation['author'] = { 'id' : instance.author.id, 'username' : instance.author.username } request = self.context.get('request') - if request and request.user.is_authenticated: + if request and request.user and request.user.is_authenticated: representation['is_bookmarked'] = instance.bookmarked_by.filter(id=request.user.id).exists() representation['is_liked'] = instance.liked_by.filter(id=request.user.id).exists() diff --git a/backend/app/views_directory/quiz_views.py b/backend/app/views_directory/quiz_views.py index 57e4154e..634d8be5 100644 --- a/backend/app/views_directory/quiz_views.py +++ b/backend/app/views_directory/quiz_views.py @@ -40,7 +40,6 @@ def create_quiz(request): return Response(quizSerializer.data, status=status.HTTP_201_CREATED) @api_view(['GET']) -@permission_classes([IsAuthenticated]) def view_quizzes(request): quizzes = Quiz.objects.all() # TODO: paginate the results @@ -226,13 +225,13 @@ def get_question(request): @api_view(['GET']) -@permission_classes([IsAuthenticated]) def get_quiz(request, quiz_id): quiz = get_object_or_404(Quiz, id=quiz_id) serializer = QuizSerializer(quiz, context = {'request': request}) data = {'quiz': serializer.data} - data['is_solved'] = QuizProgress.objects.filter(quiz=quiz, user=request.user, completed=True).exists() - data['quiz_result_id'] = QuizResults.objects.filter(quiz=quiz, user=request.user).order_by('-id').first().id if data['is_solved'] else None + if request.user.is_authenticated: + data['is_solved'] = QuizProgress.objects.filter(quiz=quiz, user=request.user, completed=True).exists() + data['quiz_result_id'] = QuizResults.objects.filter(quiz=quiz, user=request.user).order_by('-id').first().id if data['is_solved'] else None return Response(data, status=status.HTTP_200_OK)