Skip to content

Commit

Permalink
Merge pull request #761 from bounswe/BACKEND-759
Browse files Browse the repository at this point in the history
feat(backend): Finalize guest-quiz requirements.
  • Loading branch information
arastasci authored Dec 12, 2024
2 parents 1bfb9aa + d6bf19e commit c88f4b0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
3 changes: 1 addition & 2 deletions backend/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 3 additions & 4 deletions backend/app/views_directory/quiz_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit c88f4b0

Please sign in to comment.