-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackpopulate_podcast_data.py
49 lines (42 loc) · 1.76 KB
/
backpopulate_podcast_data.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
47
48
49
"""Management command for populating see course data"""
from django.core.management import BaseCommand
from learning_resources.constants import LearningResourceType
from learning_resources.models import LearningResource
from learning_resources.tasks import get_podcast_data
from learning_resources.utils import resource_delete_actions
from main.utils import now_in_utc
class Command(BaseCommand):
"""Populate podcasts"""
help = "Populate podcasts"
def add_arguments(self, parser):
parser.add_argument(
"--delete",
dest="delete",
action="store_true",
help="Delete all existing records first",
)
super().add_arguments(parser)
def handle(self, *args, **options): # noqa: ARG002
"""Run Populate Podcast data"""
if options["delete"]:
self.stdout.write(
"Deleting all existing podcasts and episodes from database"
)
for episode in LearningResource.objects.filter(
resource_type=LearningResourceType.podcast_episode.name
).iterator():
resource_delete_actions(episode)
for podcast in LearningResource.objects.filter(
resource_type=LearningResourceType.podcast.name
).iterator():
resource_delete_actions(podcast)
else:
task = get_podcast_data.delay()
self.stdout.write(f"Started task {task} to get podcast data")
self.stdout.write("Waiting on task...")
start = now_in_utc()
task.get()
total_seconds = (now_in_utc() - start).total_seconds()
self.stdout.write(
f"Population of podcast data finished, took {total_seconds} seconds"
)