Commit 4f3df4b576fcaa7f2e491144e4ab36c75ceb4022

Authored by Rohan Rangray
1 parent 0b56c948b9
Exists in master

Optimized the query by doing a batch lookup

Showing 1 changed file with 5 additions and 3 deletions Inline Diff

flashcards/management/commands/notifyusers.py View file @ 4f3df4b
from django.core.management import BaseCommand 1 1 from django.core.management import BaseCommand
from flashcards.models import UserFlashcard, Now 2 2 from flashcards.models import UserFlashcard, User, Now
from django.utils.timezone import now 3 3 from django.utils.timezone import now
from datetime import timedelta 4 4 from datetime import timedelta
5 5
6 6
class Command(BaseCommand): 7 7 class Command(BaseCommand):
help = 'Notify the users if they have cards to be reviewed' 8 8 help = 'Notify the users if they have cards to be reviewed'
9 9
def handle(self, *args, **options): 10 10 def handle(self, *args, **options):
notify_list = UserFlashcard.objects.prefetch_related('user').filter( 11 11 notify_filter = UserFlashcard.objects.prefetch_related('user').filter(
next_review__lte=Now() 12 12 next_review__lte=Now()
).exclude( 13 13 ).exclude(
user__registration_id=None, 14 14 user__registration_id=None,
user__last_notified__range=(now()-timedelta(days=1), now()) 15 15 user__last_notified__range=(now()-timedelta(days=1), now())
).values_list('user').distinct().all() 16 16 ).values_list('user').distinct().all()
17 17
for user in notify_list: 18 18 notify_list = map(lambda x: x[0], notify_filter)
19
20 for user in User.objects.filter(pk__in=notify_list):