Skip to content

Commit 26aad32

Browse files
authored
Add backfill of user organizations (#3022)
1 parent 5cd2e50 commit 26aad32

File tree

2 files changed

+86
-30
lines changed

2 files changed

+86
-30
lines changed

b2b/constants.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,44 @@
2121
CONTRACT_MEMBERSHIP_SSO,
2222
]
2323

24-
CONTRACT_MEMBERSHIP_CHOICES = zip(
25-
[
26-
CONTRACT_MEMBERSHIP_SSO,
27-
CONTRACT_MEMBERSHIP_NONSSO,
28-
CONTRACT_MEMBERSHIP_MANAGED,
29-
CONTRACT_MEMBERSHIP_CODE,
30-
CONTRACT_MEMBERSHIP_AUTO,
31-
],
32-
[
33-
CONTRACT_MEMBERSHIP_SSO_NAME,
34-
CONTRACT_MEMBERSHIP_NONSSO_NAME,
35-
CONTRACT_MEMBERSHIP_MANAGED_NAME,
36-
CONTRACT_MEMBERSHIP_CODE_NAME,
37-
CONTRACT_MEMBERSHIP_AUTO_NAME,
38-
],
24+
CONTRACT_MEMBERSHIP_CHOICES = list(
25+
zip(
26+
[
27+
CONTRACT_MEMBERSHIP_SSO,
28+
CONTRACT_MEMBERSHIP_NONSSO,
29+
CONTRACT_MEMBERSHIP_MANAGED,
30+
CONTRACT_MEMBERSHIP_CODE,
31+
CONTRACT_MEMBERSHIP_AUTO,
32+
],
33+
[
34+
CONTRACT_MEMBERSHIP_SSO_NAME,
35+
CONTRACT_MEMBERSHIP_NONSSO_NAME,
36+
CONTRACT_MEMBERSHIP_MANAGED_NAME,
37+
CONTRACT_MEMBERSHIP_CODE_NAME,
38+
CONTRACT_MEMBERSHIP_AUTO_NAME,
39+
],
40+
)
3941
)
4042
# When the integration_type field is removed, this should be removed as well.
4143
# It is just here so we can have the same choices in both integration_type and
4244
# membership_type fields. (Using the constant for `choices` consumes it.)
43-
CONTRACT_MEMBERSHIP_TYPE_CHOICES = zip(
44-
[
45-
CONTRACT_MEMBERSHIP_SSO,
46-
CONTRACT_MEMBERSHIP_NONSSO,
47-
CONTRACT_MEMBERSHIP_MANAGED,
48-
CONTRACT_MEMBERSHIP_CODE,
49-
CONTRACT_MEMBERSHIP_AUTO,
50-
],
51-
[
52-
CONTRACT_MEMBERSHIP_SSO_NAME,
53-
CONTRACT_MEMBERSHIP_NONSSO_NAME,
54-
CONTRACT_MEMBERSHIP_MANAGED_NAME,
55-
CONTRACT_MEMBERSHIP_CODE_NAME,
56-
CONTRACT_MEMBERSHIP_AUTO_NAME,
57-
],
45+
CONTRACT_MEMBERSHIP_TYPE_CHOICES = list(
46+
zip(
47+
[
48+
CONTRACT_MEMBERSHIP_SSO,
49+
CONTRACT_MEMBERSHIP_NONSSO,
50+
CONTRACT_MEMBERSHIP_MANAGED,
51+
CONTRACT_MEMBERSHIP_CODE,
52+
CONTRACT_MEMBERSHIP_AUTO,
53+
],
54+
[
55+
CONTRACT_MEMBERSHIP_SSO_NAME,
56+
CONTRACT_MEMBERSHIP_NONSSO_NAME,
57+
CONTRACT_MEMBERSHIP_MANAGED_NAME,
58+
CONTRACT_MEMBERSHIP_CODE_NAME,
59+
CONTRACT_MEMBERSHIP_AUTO_NAME,
60+
],
61+
)
5862
)
5963

6064
B2B_RUN_TAG_FORMAT = "{year}_C{contract_id}"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Generated by Django 4.2.25 on 2025-10-20 17:55
2+
3+
from django.db import migrations
4+
5+
6+
def noop_reverse(apps, schema_editor):
7+
"""No-op the reverse - we won't be able to figure out what orgs were there before."""
8+
9+
return
10+
11+
12+
def backfill_user_org_memberships(apps, schema_editor):
13+
"""
14+
Backfill each user's B2B organization memberships from their contracts.
15+
16+
0009 adds in a hard FK between orgs and users. We were calculating this before,
17+
which has the side effect that everyone starts from zero. This is fine for
18+
users who're in orgs that are in Keycloak already - they'll get added back in
19+
as part of the authentication process - but everyone who has used an
20+
enrollment code is caught out. So, do the calculation one last time and fill
21+
out the FK table.
22+
23+
We will set these with the "keep_until_seen" flag set to True, since there's
24+
no guarantee the orgs we're backfilling exist in Keycloak at this point.
25+
"""
26+
27+
User = apps.get_model("users", "User")
28+
OrganizationPage = apps.get_model("b2b", "OrganizationPage")
29+
30+
for user in User.objects.all():
31+
user_org_ids = (
32+
user.b2b_contracts.values_list("organization", flat=True).distinct().all()
33+
)
34+
user_orgs = OrganizationPage.objects.filter(pk__in=user_org_ids).all()
35+
36+
for org in user_orgs:
37+
user.b2b_organizations.add(
38+
org,
39+
through_defaults={
40+
"keep_until_seen": True,
41+
},
42+
)
43+
44+
45+
class Migration(migrations.Migration):
46+
dependencies = [
47+
("b2b", "0009_contractpage_membership_type_and_more"),
48+
]
49+
50+
operations = [
51+
migrations.RunPython(backfill_user_org_memberships, noop_reverse),
52+
]

0 commit comments

Comments
 (0)