-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackpopulate_micromasters_data.py
50 lines (43 loc) · 1.86 KB
/
backpopulate_micromasters_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
50
"""Management command for populating micromasters course data"""
from django.core.management import BaseCommand
from learning_resources.etl.constants import ETLSource
from learning_resources.models import LearningResource
from learning_resources.tasks import get_micromasters_data
from learning_resources.utils import resource_delete_actions
from main.utils import now_in_utc
class Command(BaseCommand):
"""Populate micromasters programs and courses"""
help = "Populate micromasters programs and courses"
def add_arguments(self, parser):
parser.add_argument(
"--delete",
dest="delete",
action="store_true",
help="Delete all existing programs",
)
super().add_arguments(parser)
def handle(self, *args, **options): # noqa: ARG002
"""Populate micromasters courses"""
if options["delete"]:
self.stdout.write(
"Deleting all existing Micromasters programs from database and search"
)
# we only delete programs; courses are owned by the MIT edX integration
for program in LearningResource.objects.filter(
etl_source=ETLSource.micromasters.value
):
resource_delete_actions(program)
else:
task = get_micromasters_data.delay()
self.stdout.write(f"Started task {task} to get micromasters course data")
self.stdout.write("Waiting on task...")
start = now_in_utc()
count = task.get()
total_seconds = (now_in_utc() - start).total_seconds()
self.stdout.write(
"Population of micromasters data finished, "
f"took {total_seconds} seconds"
)
self.stdout.write(
f"Populated {count} resources. See celery logs for details."
)