-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransfer_list_resources.py
70 lines (62 loc) · 2.48 KB
/
transfer_list_resources.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Management command for migrating unpublished resource path/list relationships"""
from django.core.management import BaseCommand
from learning_resources.utils import transfer_list_resources
from main.utils import now_in_utc
class Command(BaseCommand):
"""
Migrate relationships in learningpaths and userlists from unpublished
resources to published replacement resources.
"""
help = "Migrate relationships from unpublished resources to published resources."
def add_arguments(self, parser):
parser.add_argument(
"from_resource_type", type=str, help="Resource type to migrate from"
)
parser.add_argument(
"to_resource_type", type=str, help="Resource type to migrate to"
)
parser.add_argument(
"match_field", type=str, help="Resource field to match resources by"
)
parser.add_argument(
"from_source", type=str, help="ETL Source for unpublished resources"
)
parser.add_argument(
"to_source", type=str, help="ETL Source for published resources"
)
parser.add_argument(
"--delete",
dest="delete",
action="store_true",
help="Delete unpublished resources after migrating relationships",
)
super().add_arguments(parser)
def handle(self, *args, **options): # noqa: ARG002
"""
Migrate relationships in learningpaths and userlists from unpublished
resources to published replacement resources.
"""
from_resource_type = options["from_resource_type"]
to_resource_type = options["to_resource_type"]
match_field = options["match_field"]
from_source = options["from_source"]
to_source = options["to_source"]
delete = options["delete"]
self.stdout.write(
f"Migrate {from_resource_type} relationships from {from_source}"
f" to {to_resource_type}:{to_source}, matching on {match_field}"
)
start = now_in_utc()
unpublished, matching = transfer_list_resources(
from_resource_type,
to_resource_type,
match_field,
from_source,
to_source,
delete_unpublished=delete,
)
total_seconds = (now_in_utc() - start).total_seconds()
self.stdout.write(
f"Processed {unpublished} resources and found {matching} "
f"published matches, took {total_seconds} seconds"
)