4
4
from django .db .models import Q
5
5
6
6
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 ):
8
20
if not old_institution :
9
21
return None
10
22
11
23
Organization = apps .get_model ("core" , "Organization" )
12
24
OrganizationName = apps .get_model ("core" , "OrganizationName" )
13
25
14
26
organization = Organization .objects .create ()
27
+ location = get_or_create_location_with_country_only (apps , old_country )
28
+ if location :
29
+ organization .locations .add (location )
15
30
OrganizationName .objects .create (
16
31
value = old_institution ,
17
32
custom_label_for = organization ,
18
33
)
19
34
return organization
20
35
21
36
22
- def naive_create_affiliation (
37
+ def create_affiliation (
23
38
apps ,
24
39
old_institution ,
25
40
old_department ,
41
+ old_country ,
26
42
account = None ,
27
43
frozen_author = None ,
28
44
preprint_author = None ,
29
45
):
30
46
Affiliation = apps .get_model ("core" , "Affiliation" )
31
- organization = naive_create_organization (apps , old_institution )
47
+ organization = create_organization (apps , old_institution , old_country )
32
48
33
49
# Create or update the actual affiliation if the associated
34
50
# account / frozen author / preprint author has been saved already
@@ -52,31 +68,34 @@ def migrate_affiliation_institution(apps, schema_editor):
52
68
~ Q (institution__exact = '' )
53
69
| ~ Q (department__exact = '' )
54
70
):
55
- naive_create_affiliation (
71
+ create_affiliation (
56
72
apps ,
57
73
account .institution ,
58
74
account .department ,
75
+ account .country ,
59
76
account = account ,
60
77
)
61
78
62
79
for frozen_author in FrozenAuthor .objects .filter (
63
80
~ Q (institution__exact = '' )
64
81
| ~ Q (department__exact = '' )
65
82
):
66
- naive_create_affiliation (
83
+ create_affiliation (
67
84
apps ,
68
85
frozen_author .institution ,
69
86
frozen_author .department ,
87
+ frozen_author .country ,
70
88
frozen_author = frozen_author ,
71
89
)
72
90
73
91
for preprint_author in PreprintAuthor .objects .filter (
74
92
affiliation__isnull = True ,
75
93
):
76
- naive_create_affiliation (
94
+ create_affiliation (
77
95
apps ,
78
96
preprint_author .affiliation ,
79
97
'' ,
98
+ None ,
80
99
preprint_author = preprint_author ,
81
100
)
82
101
0 commit comments