Skip to content

Commit 40b1a0c

Browse files
committed
feat #3168 Add country and location to data migration
1 parent 77fa89b commit 40b1a0c

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/core/migrations/0101_migrate_affiliation_institution.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,47 @@
44
from django.db.models import Q
55

66

7-
def naive_create_organization(apps, old_institution=None):
7+
def get_or_create_location_with_country_only(apps, old_country=None):
8+
if not old_country:
9+
return None
10+
11+
Location = apps.get_model("core", "Location")
12+
location, created = Location.objects.get_or_create(
13+
name='',
14+
country=old_country,
15+
)
16+
return location
17+
18+
19+
def create_organization(apps, old_institution=None, old_country=None):
820
if not old_institution:
921
return None
1022

1123
Organization = apps.get_model("core", "Organization")
1224
OrganizationName = apps.get_model("core", "OrganizationName")
1325

1426
organization = Organization.objects.create()
27+
location = get_or_create_location_with_country_only(apps, old_country)
28+
if location:
29+
organization.locations.add(location)
1530
OrganizationName.objects.create(
1631
value=old_institution,
1732
custom_label_for=organization,
1833
)
1934
return organization
2035

2136

22-
def naive_create_affiliation(
37+
def create_affiliation(
2338
apps,
2439
old_institution,
2540
old_department,
41+
old_country,
2642
account=None,
2743
frozen_author=None,
2844
preprint_author=None,
2945
):
3046
Affiliation = apps.get_model("core", "Affiliation")
31-
organization = naive_create_organization(apps, old_institution)
47+
organization = create_organization(apps, old_institution, old_country)
3248

3349
# Create or update the actual affiliation if the associated
3450
# account / frozen author / preprint author has been saved already
@@ -52,31 +68,34 @@ def migrate_affiliation_institution(apps, schema_editor):
5268
~Q(institution__exact='')
5369
| ~Q(department__exact='')
5470
):
55-
naive_create_affiliation(
71+
create_affiliation(
5672
apps,
5773
account.institution,
5874
account.department,
75+
account.country,
5976
account=account,
6077
)
6178

6279
for frozen_author in FrozenAuthor.objects.filter(
6380
~Q(institution__exact='')
6481
| ~Q(department__exact='')
6582
):
66-
naive_create_affiliation(
83+
create_affiliation(
6784
apps,
6885
frozen_author.institution,
6986
frozen_author.department,
87+
frozen_author.country,
7088
frozen_author=frozen_author,
7189
)
7290

7391
for preprint_author in PreprintAuthor.objects.filter(
7492
affiliation__isnull=True,
7593
):
76-
naive_create_affiliation(
94+
create_affiliation(
7795
apps,
7896
preprint_author.affiliation,
7997
'',
98+
None,
8099
preprint_author=preprint_author,
81100
)
82101

0 commit comments

Comments
 (0)