Commit 390189eb63a6a8f4c0bc4fd584072d4834da7173
1 parent
19f62c6f7e
Exists in
master
handle pulling a card that's already in deck more gracefully
Showing 2 changed files with 10 additions and 5 deletions Side-by-side Diff
flashcards/models.py
View file @
390189e
... | ... | @@ -77,14 +77,16 @@ |
77 | 77 | def pull(self, flashcard): |
78 | 78 | if not self.is_in_section(flashcard.section): |
79 | 79 | raise ValueError("User not in the section this flashcard belongs to") |
80 | + | |
80 | 81 | user_card = UserFlashcard.objects.create(user=self, flashcard=flashcard) |
81 | - user_card.pulled = now() | |
82 | 82 | user_card.save() |
83 | + | |
83 | 84 | import flashcards.notifications |
84 | 85 | |
85 | 86 | flashcards.notifications.notify_score_change(flashcard) |
86 | 87 | flashcards.notifications.notify_pull(flashcard) |
87 | 88 | |
89 | + | |
88 | 90 | def unpull(self, flashcard): |
89 | 91 | if not self.is_in_section(flashcard.section): |
90 | 92 | raise ValueError("User not in the section this flashcard belongs to") |
... | ... | @@ -395,7 +397,7 @@ |
395 | 397 | return '%s %s' % (self.department_abbreviation, self.course_num) |
396 | 398 | |
397 | 399 | def get_feed_for_user(self, user): |
398 | - cards = list(self.get_cards_for_user(user).exclude(Q(userflashcard__user=user) & (~Q(author=user)))) | |
400 | + cards = list(self.get_cards_for_user(user)) | |
399 | 401 | cards.sort(key=lambda x: -x.score) |
400 | 402 | return cards |
401 | 403 |
flashcards/views.py
View file @
390189e
1 | 1 | import django |
2 | 2 | from django.contrib import auth |
3 | +from django.db import IntegrityError | |
3 | 4 | from django.shortcuts import get_object_or_404 |
4 | 5 | from flashcards.api import StandardResultsSetPagination, IsEnrolledInAssociatedSection, IsFlashcardReviewer |
5 | 6 | from flashcards.models import Section, User, Flashcard, FlashcardHide, UserFlashcard, UserFlashcardQuiz |
... | ... | @@ -305,9 +306,11 @@ |
305 | 306 | --- |
306 | 307 | view_mocker: flashcards.api.mock_no_params |
307 | 308 | """ |
308 | - | |
309 | - request.user.pull(self.get_object()) | |
310 | - return Response(status=HTTP_204_NO_CONTENT) | |
309 | + try: | |
310 | + request.user.pull(self.get_object()) | |
311 | + return Response(status=HTTP_204_NO_CONTENT) | |
312 | + except IntegrityError, e: | |
313 | + raise ValidationError('Cannot pull a card already in deck') | |
311 | 314 | |
312 | 315 | @detail_route(methods=['POST']) |
313 | 316 | def unpull(self, request, pk): |