diff --git a/flashcards/models.py b/flashcards/models.py index b24a459..193af29 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -143,6 +143,11 @@ class UserFlashcard(Model): pulled = DateTimeField(auto_now_add=True, help_text="When the user pulled the card") flashcard = ForeignKey('Flashcard') + def get_mask(self): + if self.mask is None: + return self.flashcard.mask + return self.mask + class Meta: # There can be at most one UserFlashcard for each User and Flashcard unique_together = (('user', 'flashcard'),) diff --git a/flashcards/views.py b/flashcards/views.py index 93c2930..624ac61 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -342,6 +342,7 @@ class FlashcardViewSet(GenericViewSet, CreateModelMixin, RetrieveModelMixin): new_flashcard = flashcard.edit(user, new_flashcard) return Response(FlashcardSerializer(new_flashcard).data, status=HTTP_200_OK) + class UserFlashcardQuizViewSet(GenericViewSet, CreateModelMixin, UpdateModelMixin): permission_classes = [IsAuthenticated, IsFlashcardReviewer] queryset = UserFlashcardQuiz.objects.all() @@ -373,11 +374,10 @@ class UserFlashcardQuizViewSet(GenericViewSet, CreateModelMixin, UpdateModelMixi raise ValidationError("No matching flashcard found in your decks") user_flashcard = user_flashcard_filter.order_by('?').first() - mask = user_flashcard.mask.get_random_blank() + mask = user_flashcard.get_mask().get_random_blank() user_flashcard_quiz = UserFlashcardQuiz(user_flashcard=user_flashcard, blanked_word=user_flashcard.flashcard.text[slice(*mask)]) user_flashcard_quiz.save() - assert user_flashcard_quiz is not None response = QuizResponseSerializer(instance=user_flashcard_quiz, mask=mask) return Response(response.data, status=HTTP_200_OK)