X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/89495af70dd1b941c4f5889f10fc33e8450eeb70..53a026f73c20061e531b6a8d5fbf70c297b1a14a:/src/catalogue/management/commands/update_popularity.py diff --git a/src/catalogue/management/commands/update_popularity.py b/src/catalogue/management/commands/update_popularity.py index 3a9905673..d2af0d4e9 100644 --- a/src/catalogue/management/commands/update_popularity.py +++ b/src/catalogue/management/commands/update_popularity.py @@ -1,9 +1,6 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from __future__ import print_function, unicode_literals - from django.core.management.base import BaseCommand from django.db.models import Count @@ -17,8 +14,10 @@ class Command(BaseCommand): BookPopularity.objects.all().delete() books_with_popularity = Book.objects.filter(tag_relations__tag__category='set').only('id').distinct()\ .annotate(pop=Count('tag_relations__tag__user', distinct=True)) + pop_list = [] for book in books_with_popularity: - BookPopularity.objects.create(book=book, count=book.pop) + pop_list.append(BookPopularity(book=book, count=book.pop)) books_without_popularity = Book.objects.exclude(tag_relations__tag__category='set') for book in books_without_popularity: - BookPopularity.objects.create(book=book, count=0) + pop_list.append(BookPopularity(book=book, count=0)) + BookPopularity.objects.bulk_create(pop_list, batch_size=512)