forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackfill_sso_availability.py
More file actions
79 lines (63 loc) · 2.75 KB
/
backfill_sso_availability.py
File metadata and controls
79 lines (63 loc) · 2.75 KB
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
71
72
73
74
75
76
77
78
79
from django.core.management.base import BaseCommand
from django.db import transaction
from django.db.models import Q
from osf.models.institution import Institution, SSOAvailability, IntegrationType
class Command(BaseCommand):
help = 'Backfill sso_availability using fast DB-level updates'
def add_arguments(self, parser):
parser.add_argument(
'--dry-run',
action='store_true',
help='Show how many rows would be updated without applying changes',
)
def handle(self, *args, **options):
dry_run = options['dry_run']
# Build querysets
qs_no_protocol = Institution.objects.filter(
delegation_protocol=IntegrationType.NONE.value
).exclude(
sso_availability=SSOAvailability.UNAVAILABLE.value
)
qs_inactive_with_protocol = Institution.objects.filter(
~Q(delegation_protocol=IntegrationType.NONE.value),
deactivated__isnull=False
).exclude(
sso_availability=SSOAvailability.HIDDEN.value
)
qs_active_with_protocol = Institution.objects.filter(
~Q(delegation_protocol=IntegrationType.NONE.value),
deactivated__isnull=True
).exclude(
sso_availability=SSOAvailability.PUBLIC.value
)
count_no_protocol = qs_no_protocol.count()
count_inactive = qs_inactive_with_protocol.count()
count_active = qs_active_with_protocol.count()
total = count_no_protocol + count_inactive + count_active
self.stdout.write('Planned updates:')
self.stdout.write(f" No protocol → UNAVAILABLE: {count_no_protocol}")
self.stdout.write(f" Inactive + protocol → HIDDEN: {count_inactive}")
self.stdout.write(f" Active + protocol → PUBLIC: {count_active}")
self.stdout.write(f" TOTAL: {total}")
if dry_run:
self.stdout.write(self.style.WARNING('Dry run, no changes applied.'))
return
with transaction.atomic():
updated_no_protocol = qs_no_protocol.update(
sso_availability=SSOAvailability.UNAVAILABLE.value
)
updated_inactive = qs_inactive_with_protocol.update(
sso_availability=SSOAvailability.HIDDEN.value
)
updated_active = qs_active_with_protocol.update(
sso_availability=SSOAvailability.PUBLIC.value
)
self.stdout.write(
self.style.SUCCESS(
'Done:\n'
f" UNAVAILABLE: {updated_no_protocol}\n"
f" HIDDEN: {updated_inactive}\n"
f" PUBLIC: {updated_active}\n"
f" TOTAL: {updated_no_protocol + updated_inactive + updated_active}"
)
)