Commit 3f97f29f084459082d6060d166cdfd28c4220e33
1 parent
31a37c406b
Exists in
master
improve error handling for card pull/push
Showing 2 changed files with 25 additions and 8 deletions Side-by-side Diff
flashcards/models.py
View file @
3f97f29
... | ... | @@ -63,6 +63,15 @@ |
63 | 63 | return self._create_user(email, password, True, True, **extra_fields) |
64 | 64 | |
65 | 65 | |
66 | +class FlashcardAlreadyPulledException(Exception): | |
67 | + pass | |
68 | + | |
69 | + | |
70 | + | |
71 | +class FlashcardNotInDeckException(Exception): | |
72 | + pass | |
73 | + | |
74 | + | |
66 | 75 | class User(AbstractUser, SimpleEmailConfirmationUserMixin): |
67 | 76 | """ |
68 | 77 | An extension of Django's default user model. |
... | ... | @@ -81,7 +90,10 @@ |
81 | 90 | if not self.is_in_section(flashcard.section): |
82 | 91 | raise ValueError("User not in the section this flashcard belongs to") |
83 | 92 | |
84 | - user_card = UserFlashcard.objects.create(user=self, flashcard=flashcard) | |
93 | + try: | |
94 | + user_card = UserFlashcard.objects.create(user=self, flashcard=flashcard) | |
95 | + except IntegrityError: | |
96 | + raise FlashcardAlreadyPulledException() | |
85 | 97 | user_card.save() |
86 | 98 | |
87 | 99 | import flashcards.notifications |
88 | 100 | |
... | ... | @@ -95,12 +107,11 @@ |
95 | 107 | |
96 | 108 | try: |
97 | 109 | import flashcards.notifications |
98 | - | |
99 | 110 | user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard) |
100 | 111 | user_card.delete() |
101 | 112 | flashcards.notifications.notify_score_change(flashcard) |
102 | 113 | except UserFlashcard.DoesNotExist: |
103 | - raise ValueError('Cannot unpull card that is not pulled.') | |
114 | + raise FlashcardNotInDeckException() | |
104 | 115 | |
105 | 116 | def get_deck(self, section): |
106 | 117 | if not self.is_in_section(section): |
flashcards/views.py
View file @
3f97f29
... | ... | @@ -5,7 +5,8 @@ |
5 | 5 | from django.utils.log import getLogger |
6 | 6 | from flashcards.api import StandardResultsSetPagination, IsEnrolledInAssociatedSection, IsFlashcardReviewer, \ |
7 | 7 | IsAuthenticatedAndConfirmed |
8 | -from flashcards.models import Section, User, Flashcard, FlashcardHide, UserFlashcard, UserFlashcardQuiz | |
8 | +from flashcards.models import Section, User, Flashcard, FlashcardHide, UserFlashcard, UserFlashcardQuiz, \ | |
9 | + FlashcardAlreadyPulledException | |
9 | 10 | from flashcards.notifications import notify_new_card |
10 | 11 | from flashcards.serializers import SectionSerializer, UserUpdateSerializer, RegistrationSerializer, UserSerializer, \ |
11 | 12 | PasswordResetSerializer, PasswordResetRequestSerializer, EmailPasswordSerializer, FlashcardSerializer, \ |
... | ... | @@ -276,6 +277,7 @@ |
276 | 277 | return Response(status=HTTP_204_NO_CONTENT) |
277 | 278 | |
278 | 279 | |
280 | + | |
279 | 281 | class FlashcardViewSet(GenericViewSet, CreateModelMixin, RetrieveModelMixin): |
280 | 282 | queryset = Flashcard.objects.all() |
281 | 283 | serializer_class = FlashcardSerializer |
... | ... | @@ -333,7 +335,7 @@ |
333 | 335 | request.user.pull(self.get_object()) |
334 | 336 | log_event(request, str(self.get_object())) |
335 | 337 | return Response(status=HTTP_204_NO_CONTENT) |
336 | - except IntegrityError, e: | |
338 | + except FlashcardAlreadyPulledException: | |
337 | 339 | raise ValidationError('Cannot pull a card already in deck') |
338 | 340 | |
339 | 341 | @detail_route(methods=['POST']) |
... | ... | @@ -345,9 +347,13 @@ |
345 | 347 | """ |
346 | 348 | user = request.user |
347 | 349 | flashcard = self.get_object() |
348 | - user.unpull(flashcard) | |
349 | - log_event(request, str(self.get_object())) | |
350 | - return Response(status=HTTP_204_NO_CONTENT) | |
350 | + try: | |
351 | + user.unpull(flashcard) | |
352 | + log_event(request, str(self.get_object())) | |
353 | + return Response(status=HTTP_204_NO_CONTENT) | |
354 | + except FlashcardNotInDeckException: | |
355 | + raise ValidationError('Cannot unpull a card not in deck') | |
356 | + | |
351 | 357 | |
352 | 358 | def partial_update(self, request, *args, **kwargs): |
353 | 359 | """ |