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 Side-by-side Diff

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