From 750af8a0f32f39ba3bccba940fb5f40162cde986 Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Mon, 1 Jun 2015 19:39:15 -0700 Subject: [PATCH] add unpull event --- flashcards/models.py | 24 +++++++++--------------- flashcards/notifications.py | 8 ++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/flashcards/models.py b/flashcards/models.py index 486116d..0dfa3ae 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -1,6 +1,6 @@ from math import log1p from math import exp -from datetime import datetime, timedelta +from datetime import timedelta, datetime from django.contrib.auth.models import AbstractUser, UserManager from django.contrib.auth.tokens import default_token_generator @@ -18,13 +18,6 @@ from fields import MaskField from cached_property import cached_property from flashy.settings import IN_PRODUCTION - - - - - - - # Hack to fix AbstractUser before subclassing it AbstractUser._meta.get_field('email')._unique = True @@ -105,7 +98,6 @@ class User(AbstractUser, SimpleEmailConfirmationUserMixin): user_card = UserFlashcard.objects.create(user=self, flashcard=flashcard) except IntegrityError: raise FlashcardAlreadyPulledException() - user_card.save() import flashcards.notifications @@ -117,13 +109,15 @@ class User(AbstractUser, SimpleEmailConfirmationUserMixin): raise ValueError("User not in the section this flashcard belongs to") try: - import flashcards.notifications - user_card = UserFlashcard.objects.get(user=self, flashcard=flashcard) - user_card.delete() - flashcards.notifications.notify_score_change(flashcard) except UserFlashcard.DoesNotExist: raise FlashcardNotInDeckException() + user_card.delete() + + import flashcards.notifications + + flashcards.notifications.notify_score_change(flashcard) + flashcards.notifications.notify_unpull(flashcard, self) def get_deck(self, section): if not self.is_in_section(section): @@ -205,8 +199,8 @@ class UserFlashcard(Model): if self.last_interval == 1: self.last_interval = 6 else: - self.last_response_factor = min(1.3, self.last_response_factor+(0.1-(5-q)*(0.08+(5-q)*0.02))) - self.last_interval = int(round(self.last_interval*self.last_response_factor)) + self.last_response_factor = min(1.3, self.last_response_factor + (0.1 - (5 - q) * (0.08 + (5 - q) * 0.02))) + self.last_interval = int(round(self.last_interval * self.last_response_factor)) self.next_review = user_flashcard_quiz.when + timedelta(days=self.last_interval) self.save() diff --git a/flashcards/notifications.py b/flashcards/notifications.py index 3573515..2a1490b 100644 --- a/flashcards/notifications.py +++ b/flashcards/notifications.py @@ -29,3 +29,11 @@ def notify_pull(flashcard, user): ) message = RedisMessage(ws_message) redis_publisher.publish_message(message) + +def notify_unpull(flashcard, user): + redis_publisher = RedisPublisher(facility='deck/%d' % flashcard.section_id, users=[user]) + ws_message = JSONRenderer().render( + {'event_type': 'unpull_card', 'flashcard': serializers.FlashcardSerializer(flashcard).data} + ) + message = RedisMessage(ws_message) + redis_publisher.publish_message(message) -- 1.9.1