Commit b048e96a242d79e056a9b5dfdbefbd1446404f3e
1 parent
5d1399ce32
Exists in
master
unpull flashcard
Showing 2 changed files with 24 additions and 0 deletions Side-by-side Diff
flashcards/models.py
View file @
b048e96
... | ... | @@ -59,6 +59,17 @@ |
59 | 59 | user_card.pulled = datetime.now() |
60 | 60 | user_card.save() |
61 | 61 | |
62 | + def unpull(self, flashcard): | |
63 | + if not self.is_in_section(flashcard.section): | |
64 | + raise ValueError("User not in the section this flashcard belongs to") | |
65 | + | |
66 | + try: | |
67 | + user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard) | |
68 | + except UserFlashcard.DoesNotExist: | |
69 | + raise ValueError('Cannot unpull card that is not pulled.') | |
70 | + | |
71 | + user_card.delete() | |
72 | + | |
62 | 73 | def get_deck(self, section): |
63 | 74 | if not self.is_in_section(section): |
64 | 75 | raise ObjectDoesNotExist("User not enrolled in section") |
flashcards/views.py
View file @
b048e96
... | ... | @@ -365,6 +365,19 @@ |
365 | 365 | user.pull(flashcard) |
366 | 366 | return Response(status=HTTP_204_NO_CONTENT) |
367 | 367 | |
368 | + @detail_route(methods=['POST']) | |
369 | + def unpull(self, request, pk): | |
370 | + """ | |
371 | + Unpull a card from the user's deck | |
372 | + :param request: The request object | |
373 | + :param pk: The primary key | |
374 | + :return: A 204 response upon success. | |
375 | + """ | |
376 | + user = request.user | |
377 | + flashcard = self.get_object() | |
378 | + user.unpull(flashcard) | |
379 | + return Response(status=HTTP_204_NO_CONTENT) | |
380 | + | |
368 | 381 | def update(self, request, *args, **kwargs): |
369 | 382 | """ |
370 | 383 | Edit settings related to a card for the user. |