diff --git a/flashcards/models.py b/flashcards/models.py index a01ec31..99df078 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -59,6 +59,17 @@ class User(AbstractUser, SimpleEmailConfirmationUserMixin): user_card.pulled = datetime.now() user_card.save() + def unpull(self, flashcard): + if not self.is_in_section(flashcard.section): + raise ValueError("User not in the section this flashcard belongs to") + + try: + user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard) + except UserFlashcard.DoesNotExist: + raise ValueError('Cannot unpull card that is not pulled.') + + user_card.delete() + def get_deck(self, section): if not self.is_in_section(section): raise ObjectDoesNotExist("User not enrolled in section") diff --git a/flashcards/views.py b/flashcards/views.py index 2649aed..05aff1c 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -365,6 +365,19 @@ class FlashcardViewSet(GenericViewSet, UpdateModelMixin, CreateModelMixin, Retri user.pull(flashcard) return Response(status=HTTP_204_NO_CONTENT) + @detail_route(methods=['POST']) + def unpull(self, request, pk): + """ + Unpull a card from the user's deck + :param request: The request object + :param pk: The primary key + :return: A 204 response upon success. + """ + user = request.user + flashcard = self.get_object() + user.unpull(flashcard) + return Response(status=HTTP_204_NO_CONTENT) + def update(self, request, *args, **kwargs): """ Edit settings related to a card for the user.