|
| 1 | +import json |
| 2 | + |
1 | 3 | from django.db import transaction |
| 4 | +from django.conf import settings |
2 | 5 | from django.core.management.base import BaseCommand |
3 | 6 | from django.contrib.auth import get_user_model |
4 | 7 |
|
@@ -32,13 +35,30 @@ def migrate_organization_to_education() -> int: |
32 | 35 | """ |
33 | 36 | Migrate old field `organization` to new model `Education`. |
34 | 37 | Returns count migrated users. |
| 38 | + Stored migrated info into `BASE_DIR / "log" / "migrated_users.json"` |
35 | 39 | """ |
36 | | - users_with_irganization = CustomUser.objects.exclude(organization=None).exclude(organization="") |
| 40 | + user_with_education_ids: list[int] = UserEducation.objects.values_list("user__id", flat=True) |
| 41 | + users_with_organization_without_education = ( |
| 42 | + CustomUser.objects |
| 43 | + .exclude(organization=None) |
| 44 | + .exclude(organization="") |
| 45 | + .exclude(id__in=user_with_education_ids) |
| 46 | + ) |
37 | 47 | UserEducation.objects.bulk_create([ |
38 | 48 | UserEducation( |
39 | 49 | user=user, |
40 | 50 | organization_name=user.organization, |
41 | 51 | ) |
42 | | - for user in users_with_irganization |
| 52 | + for user in users_with_organization_without_education |
43 | 53 | ]) |
44 | | - return users_with_irganization.count() |
| 54 | + |
| 55 | + data = [ |
| 56 | + {"user_id": user.id, "user_organization_field": user.organization} |
| 57 | + for user in users_with_organization_without_education |
| 58 | + ] |
| 59 | + |
| 60 | + file_dump = settings.BASE_DIR / "log" / "migrated_users.json" |
| 61 | + with open(file_dump, "w", encoding="utf-8") as file: |
| 62 | + json.dump(data, file, indent=4, ensure_ascii=False) |
| 63 | + |
| 64 | + return users_with_organization_without_education.count() |
0 commit comments