Commit 750af8a0f32f39ba3bccba940fb5f40162cde986
1 parent
6c31b00541
Exists in
master
add unpull event
Showing 2 changed files with 17 additions and 15 deletions Side-by-side Diff
flashcards/models.py
View file @
750af8a
1 | 1 | from math import log1p |
2 | 2 | from math import exp |
3 | -from datetime import datetime, timedelta | |
3 | +from datetime import timedelta, datetime | |
4 | 4 | |
5 | 5 | from django.contrib.auth.models import AbstractUser, UserManager |
6 | 6 | from django.contrib.auth.tokens import default_token_generator |
... | ... | @@ -18,13 +18,6 @@ |
18 | 18 | from cached_property import cached_property |
19 | 19 | from flashy.settings import IN_PRODUCTION |
20 | 20 | |
21 | - | |
22 | - | |
23 | - | |
24 | - | |
25 | - | |
26 | - | |
27 | - | |
28 | 21 | # Hack to fix AbstractUser before subclassing it |
29 | 22 | |
30 | 23 | AbstractUser._meta.get_field('email')._unique = True |
... | ... | @@ -105,7 +98,6 @@ |
105 | 98 | user_card = UserFlashcard.objects.create(user=self, flashcard=flashcard) |
106 | 99 | except IntegrityError: |
107 | 100 | raise FlashcardAlreadyPulledException() |
108 | - user_card.save() | |
109 | 101 | |
110 | 102 | import flashcards.notifications |
111 | 103 | |
112 | 104 | |
113 | 105 | |
114 | 106 | |
... | ... | @@ -117,14 +109,16 @@ |
117 | 109 | raise ValueError("User not in the section this flashcard belongs to") |
118 | 110 | |
119 | 111 | try: |
120 | - import flashcards.notifications | |
121 | - | |
122 | 112 | user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard) |
123 | - user_card.delete() | |
124 | - flashcards.notifications.notify_score_change(flashcard) | |
125 | 113 | except UserFlashcard.DoesNotExist: |
126 | 114 | raise FlashcardNotInDeckException() |
115 | + user_card.delete() | |
127 | 116 | |
117 | + import flashcards.notifications | |
118 | + | |
119 | + flashcards.notifications.notify_score_change(flashcard) | |
120 | + flashcards.notifications.notify_unpull(flashcard, self) | |
121 | + | |
128 | 122 | def get_deck(self, section): |
129 | 123 | if not self.is_in_section(section): |
130 | 124 | raise ObjectDoesNotExist("User not enrolled in section") |
... | ... | @@ -205,8 +199,8 @@ |
205 | 199 | if self.last_interval == 1: |
206 | 200 | self.last_interval = 6 |
207 | 201 | else: |
208 | - self.last_response_factor = min(1.3, self.last_response_factor+(0.1-(5-q)*(0.08+(5-q)*0.02))) | |
209 | - self.last_interval = int(round(self.last_interval*self.last_response_factor)) | |
202 | + self.last_response_factor = min(1.3, self.last_response_factor + (0.1 - (5 - q) * (0.08 + (5 - q) * 0.02))) | |
203 | + self.last_interval = int(round(self.last_interval * self.last_response_factor)) | |
210 | 204 | self.next_review = user_flashcard_quiz.when + timedelta(days=self.last_interval) |
211 | 205 | self.save() |
212 | 206 |
flashcards/notifications.py
View file @
750af8a
... | ... | @@ -29,4 +29,12 @@ |
29 | 29 | ) |
30 | 30 | message = RedisMessage(ws_message) |
31 | 31 | redis_publisher.publish_message(message) |
32 | + | |
33 | +def notify_unpull(flashcard, user): | |
34 | + redis_publisher = RedisPublisher(facility='deck/%d' % flashcard.section_id, users=[user]) | |
35 | + ws_message = JSONRenderer().render( | |
36 | + {'event_type': 'unpull_card', 'flashcard': serializers.FlashcardSerializer(flashcard).data} | |
37 | + ) | |
38 | + message = RedisMessage(ws_message) | |
39 | + redis_publisher.publish_message(message) |