def _set_active_card_templates_for_subscribers(self, template_ids): from manabi.apps.flashcards.models import Card subscriber_cards = Card.objects.filter( fact__in=self.syncing_subscriber_facts, ) new_subscriber_cards = subscriber_cards.filter( last_reviewed_at__isnull=True, ) new_subscriber_cards.filter( template__in=template_ids, ).update(active=True) new_subscriber_cards.exclude( template__in=template_ids, ).update(active=False) for template_id in template_ids: facts_without_template = self.syncing_subscriber_facts.exclude( card__in=subscriber_cards.filter(template=template_id), ) missing_cards = [ Card( deck_id=deck_id, fact_id=fact_id, template=template_id, new_card_ordinal=Card.random_card_ordinal(), ) for fact_id, deck_id in facts_without_template.values_list('id', 'deck_id').iterator() ] Card.objects.bulk_create(missing_cards)
def set_active_card_templates(self, card_templates): ''' Creates or updates associated `Card`s. ''' from manabi.apps.flashcards.models import Card template_ids = { _card_template_string_to_id(template) for template in card_templates } for activated_card in ( self.card_set.filter(template__in=template_ids) ): activated_card.activate() for deactivated_card in ( self.card_set.exclude(template__in=template_ids) ): deactivated_card.deactivate() existing_template_ids = set(self.card_set.values_list( 'template', flat=True)) for template_id in template_ids - existing_template_ids: Card.objects.create( deck=self.deck, fact=self, template=template_id, new_card_ordinal=Card.random_card_ordinal(), ) self._set_active_card_templates_for_subscribers(template_ids)
def set_active_card_templates(self, card_templates): ''' Creates or updates associated `Card`s. ''' from manabi.apps.flashcards.models import Card template_ids = { _card_template_string_to_id(template) for template in card_templates } for activated_card in (self.card_set.filter( template__in=template_ids)): activated_card.activate() for deactivated_card in (self.card_set.exclude( template__in=template_ids)): deactivated_card.deactivate() existing_template_ids = set( self.card_set.values_list('template', flat=True)) for template_id in template_ids - existing_template_ids: Card.objects.create( deck=self.deck, fact=self, template=template_id, new_card_ordinal=Card.random_card_ordinal(), ) self._set_active_card_templates_for_subscribers(template_ids)
def _set_active_card_templates_for_subscribers(self, template_ids): from manabi.apps.flashcards.models import Card subscriber_cards = Card.objects.filter( fact__in=self.syncing_subscriber_facts, ) new_subscriber_cards = subscriber_cards.filter( last_reviewed_at__isnull=True, ) new_subscriber_cards.filter( template__in=template_ids, ).update(active=True) new_subscriber_cards.exclude( template__in=template_ids, ).update(active=False) for template_id in template_ids: facts_without_template = self.syncing_subscriber_facts.exclude( card__in=subscriber_cards.filter( template=template_id), ).select_related('deck') missing_cards = [ Card( owner_id=owner_id, deck_id=deck_id, deck_suspended=deck_suspended, fact_id=fact_id, template=template_id, new_card_ordinal=Card.random_card_ordinal(), ) for fact_id, deck_id, deck_suspended, owner_id in facts_without_template.values_list( 'id', 'deck_id', 'deck__suspended', 'deck__owner_id', ).iterator() ] Card.objects.bulk_create(missing_cards)