-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackpopulate_favorites_lists.py
46 lines (39 loc) · 1.72 KB
/
backpopulate_favorites_lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Management command to create user Favorites lists"""
from django.contrib.auth.models import User
from django.core.management import BaseCommand
from learning_resources.constants import FAVORITES_TITLE
from learning_resources.models import UserList
from main.utils import now_in_utc
class Command(BaseCommand):
"""Create a Favorites userlist for every active user"""
help = "Create a Favorites userlist for every active user"
def add_arguments(self, parser):
parser.add_argument(
"--delete",
dest="delete",
action="store_true",
help="Delete all existing Favorites user lists",
)
super().add_arguments(parser)
def handle(self, *args, **options): # noqa: ARG002
"""Create a Favorites userlist for every active user"""
if options["delete"]:
self.stdout.write("Deleting all existing Favorites userlists")
UserList.objects.filter(title=FAVORITES_TITLE).delete()
else:
self.stdout.write("Creating Favorites lists for each user")
start = now_in_utc()
user_ids = UserList.objects.filter(title=FAVORITES_TITLE).values_list(
"author_id", flat=True
)
for user in User.objects.exclude(id__in=user_ids).exclude(is_active=False):
UserList.objects.get_or_create(
author=user,
title=FAVORITES_TITLE,
defaults={"description": "My Favorites"},
)
total_seconds = (now_in_utc() - start).total_seconds()
self.stdout.write(
"Population of user favorites list finished, "
f"took {total_seconds} seconds"
)