Commit 0b56c948b9d0ce0a3b069264d938d4e5b6fb2c4f
1 parent
0b6a759a6e
Exists in
master
Prefetching users to optimize the query
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
flashcards/management/commands/notifyusers.py
View file @
0b56c94
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, 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.filter( | 11 | 11 | notify_list = 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() |