def move_to_deck(self, deck): self.new_syncing_subscriber_facts.update(active=False) self.subscriber_facts.clear() self.deck = deck self.synchronized_with = None self.save(update_fields=['deck', 'synchronized_with']) if self.deck.shared: copy_facts_to_subscribers([self])
def save(self, update_fields=None, *args, **kwargs): ''' Set a random sorting index for new cards. Propagates changes down to subscriber facts. ''' self.modified_at = datetime.utcnow() also_update_fields = {'modified_at'} if self.roll_ordinal(): also_update_fields.add('new_fact_ordinal') if ( not self.forked and ( update_fields is None or set(update_fields) & {'expression', 'reading', 'meaning'} ) and self.synchronized_with is not None and ( self.synchronized_with.expression != self.expression or self.synchronized_with.reading != self.reading or self.synchronized_with.meaning != self.meaning ) ): self.forked = True also_update_fields.add('forked') if update_fields is not None: update_fields = list(set(update_fields) | also_update_fields) is_new = self.pk is None super(Fact, self).save(*args, **kwargs) if update_fields is None or ( set(update_fields) & {'expression', 'reading', 'meaning'} ): self.syncing_subscriber_facts.update( expression=self.expression, reading=self.reading, meaning=self.meaning, ) if is_new and self.deck.shared: copy_facts_to_subscribers([self]) if is_new: harvest_tweets.delay(self)
def share(self): ''' Shares this deck publicly. Resynchronizes with any preexisting subscribers. ''' if self.synchronized_with: raise TypeError( "Cannot share synchronized decks (decks which are already " "synchronized with shared decks).") self.shared = True self.shared_at = datetime.datetime.utcnow() self.save(update_fields=['shared', 'shared_at']) copy_facts_to_subscribers(self.facts.filter(active=True))
def save(self, update_fields=None, *args, **kwargs): ''' Set a random sorting index for new cards. Propagates changes down to subscriber facts. ''' self.modified_at = datetime.utcnow() also_update_fields = {'modified_at'} if self.deck.randomize_card_order and self.roll_ordinal(): also_update_fields.add('new_fact_ordinal') if (not self.forked and (update_fields is None or set(update_fields) & {'expression', 'reading', 'meaning'}) and self.synchronized_with is not None and (self.synchronized_with.expression != self.expression or self.synchronized_with.reading != self.reading or self.synchronized_with.meaning != self.meaning)): self.forked = True also_update_fields.add('forked') if update_fields is not None: update_fields = list(set(update_fields) | also_update_fields) is_new = self.pk is None super(Fact, self).save(update_fields=update_fields, *args, **kwargs) if update_fields is None or (set(update_fields) & {'expression', 'reading', 'meaning'}): self.syncing_subscriber_facts.update( expression=self.expression, reading=self.reading, meaning=self.meaning, ) if is_new and self.deck.shared: copy_facts_to_subscribers([self]) if (update_fields is None or {'deck', 'deck_id', 'suspended', 'active'} & set(update_fields)): self.deck.refresh_card_count() if is_new: harvest_tweets.delay(self)
def subscribe(self, user): ''' Subscribes to this shared deck for the given user. They will study this deck as their own, but will still receive updates to content. Returns the newly created deck. If the user was already subscribed to this deck, returns the existing deck. ''' from manabi.apps.flashcards.models import Card, Fact # Check if the user is already subscribed to this deck. subscriber_deck = self.get_subscriber_deck_for_user(user) if subscriber_deck: return subscriber_deck if not self.shared: raise TypeError( 'This is not a shared deck - cannot subscribe to it.') if self.synchronized_with: raise TypeError( 'Cannot share a deck that is already synchronized to a shared deck.' ) #TODO-OLD dont allow multiple subscriptions to same deck by same user deck = Deck.objects.create( synchronized_with=self, name=self.name, slug=self.slug, image=self.image, description=self.description, randomize_card_order=self.randomize_card_order, textbook_source=self.textbook_source, owner_id=user.id, ) copy_facts_to_subscribers(self.facts.all(), subscribers=[user]) return deck
def subscribe(self, user): ''' Subscribes to this shared deck for the given user. They will study this deck as their own, but will still receive updates to content. Returns the newly created deck. If the user was already subscribed to this deck, returns the existing deck. ''' from manabi.apps.flashcards.models import Card, Fact # Check if the user is already subscribed to this deck. subscriber_deck = self.get_subscriber_deck_for_user(user) if subscriber_deck: return subscriber_deck if not self.shared: raise TypeError('This is not a shared deck - cannot subscribe to it.') if self.synchronized_with: raise TypeError('Cannot share a deck that is already synchronized to a shared deck.') #TODO-OLD dont allow multiple subscriptions to same deck by same user deck = Deck.objects.create( synchronized_with=self, name=self.name, description=self.description, priority=self.priority, # TODO: Remove. textbook_source=self.textbook_source, owner_id=user.id, ) copy_facts_to_subscribers(self.facts.all(), subscribers=[user]) return deck
def save(self, update_fields=None, *args, **kwargs): ''' Set a random sorting index for new cards. Propagates changes down to subscriber facts. ''' self.modified_at = datetime.utcnow() also_update_fields = {'modified_at'} if self.deck.randomize_card_order and self.roll_ordinal(): also_update_fields.add('new_fact_ordinal') # Fork if a subscriber card is being edited. if ( not self.forked and ( update_fields is None or set(update_fields) & { 'expression', 'reading', 'meaning', 'example_sentence', 'jmdict_id', } ) and self.synchronized_with is not None and ( self.synchronized_with.expression != self.expression or self.synchronized_with.reading != self.reading or self.synchronized_with.meaning != self.meaning or self.synchronized_with.example_sentence != self.example_sentence or self.synchronized_with.jmdict_id != self.jmdict_id ) ): self.forked = True also_update_fields.add('forked') if update_fields is not None: update_fields = list(set(update_fields) | also_update_fields) is_new = self.pk is None super(Fact, self).save(update_fields=update_fields, *args, **kwargs) # Update subscriber cards as necessary. if update_fields is None or ( set(update_fields) & { 'expression', 'reading', 'meaning', 'example_sentence', 'jmdict_id', } ): self.syncing_subscriber_facts.update( expression=self.expression, reading=self.reading, meaning=self.meaning, example_sentence=self.example_sentence, jmdict_id=self.jmdict_id, modified_at=self.modified_at, ) if is_new and self.deck.shared: copy_facts_to_subscribers([self]) if ( update_fields is None or {'deck', 'deck_id', 'suspended', 'active'} & set(update_fields) ): self.deck.refresh_card_count() if update_fields is None or 'jmdict_id' in update_fields: self.card_set.exclude( jmdict_id=self.jmdict_id, ).update( jmdict_id=self.jmdict_id, created_or_modified_at=self.modified_at, ) if is_new: harvest_tweets.delay(self)