Skip to content

Commit

Permalink
Fix bug in quiz get or create when we just answered a quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
eleurent committed Oct 15, 2023
1 parent 3155ca6 commit de5ed52
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions backend/nature_go/university/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import generics, permissions, serializers, status
from rest_framework.response import Response
from .models import MultipleChoiceQuestion, Quiz
from .serializers import QuizSerializer, AdminMultipleChoiceQuestionSerializer
from .permissions import IsOwner
Expand Down Expand Up @@ -40,23 +41,22 @@ def get(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)


class QuizGetOrCreateView(generics.RetrieveAPIView):
class QuizGetOrCreateView(generics.RetrieveAPIView, generics.CreateAPIView):
queryset = Quiz.objects.all()
serializer_class = QuizSerializer
permission_classes = [permissions.IsAuthenticated, IsOwner]

def get_object(self):
def retrieve(self, request, *args, **kwargs):
recent_quizzes = Quiz.objects.filter(
user=self.request.user, datetime__gte=timezone.now() - timedelta(hours=24))
for quiz in recent_quizzes:
if not quiz.is_answered:
return quiz
response = QuizCreateView.as_view()(self.request._request)
if response.status_code == status.HTTP_200_OK:
return Quiz.objects.get(pk=response.data['id'])
quizzes = [quiz for quiz in recent_quizzes if not quiz.is_answered]

if quizzes:
serializer = self.get_serializer(quizzes[0])
return Response(serializer.data)
else:
raise serializers.ValidationError('The quiz could not be found or created.')
return QuizCreateView.as_view()(self.request._request)


class QuizRetrieveUpdateView(generics.RetrieveUpdateAPIView):
queryset = Quiz.objects.all()
Expand Down

0 comments on commit de5ed52

Please sign in to comment.