From b048e96a242d79e056a9b5dfdbefbd1446404f3e Mon Sep 17 00:00:00 2001
From: CKW <ckw018@ucsd.edu>
Date: Sat, 16 May 2015 23:58:29 -0700
Subject: [PATCH] unpull flashcard

---
 flashcards/models.py | 11 +++++++++++
 flashcards/views.py  | 13 +++++++++++++
 2 files changed, 24 insertions(+)

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.
-- 
1.9.1