7
7
)
8
8
from api .base .serializers import (
9
9
VersionedDateTimeField , HideIfRegistration , IDField ,
10
- JSONAPIRelationshipSerializer ,
11
10
JSONAPISerializer , LinksField ,
12
11
NodeFileHyperLinkField , RelationshipField ,
13
12
ShowIfVersion , TargetTypeField , TypeField ,
14
- WaterbutlerLink , relationship_diff , BaseAPISerializer ,
13
+ WaterbutlerLink , BaseAPISerializer ,
15
14
HideIfWikiDisabled , ShowIfAdminScopeOrAnonymous ,
16
15
ValuesListField , TargetField ,
17
16
)
21
20
get_user_auth , is_truthy ,
22
21
)
23
22
from api .base .versioning import get_kebab_snake_case_field
23
+ from api .institutions .utils import update_institutions
24
24
from api .taxonomies .serializers import TaxonomizableSerializerMixin
25
25
from django .apps import apps
26
26
from django .conf import settings
34
34
from addons .osfstorage .models import Region
35
35
from osf .exceptions import NodeStateError
36
36
from osf .models import (
37
- Comment , DraftRegistration , ExternalAccount , Institution ,
37
+ Comment , DraftRegistration , ExternalAccount ,
38
38
RegistrationSchema , AbstractNode , PrivateLink , Preprint ,
39
39
RegistrationProvider , OSFGroup , NodeLicense , DraftNode ,
40
40
Registration , Node ,
@@ -52,44 +52,6 @@ def to_internal_value(self, data):
52
52
return self .get_object (data )
53
53
54
54
55
- def get_institutions_to_add_remove (institutions , new_institutions ):
56
- diff = relationship_diff (
57
- current_items = {inst ._id : inst for inst in institutions .all ()},
58
- new_items = {inst ['_id' ]: inst for inst in new_institutions },
59
- )
60
-
61
- insts_to_add = []
62
- for inst_id in diff ['add' ]:
63
- inst = Institution .load (inst_id )
64
- if not inst :
65
- raise exceptions .NotFound (detail = f'Institution with id "{ inst_id } " was not found' )
66
- insts_to_add .append (inst )
67
-
68
- return insts_to_add , diff ['remove' ].values ()
69
-
70
-
71
- def update_institutions (node , new_institutions , user , post = False ):
72
- add , remove = get_institutions_to_add_remove (
73
- institutions = node .affiliated_institutions ,
74
- new_institutions = new_institutions ,
75
- )
76
-
77
- if not post :
78
- for inst in remove :
79
- if not user .is_affiliated_with_institution (inst ) and not node .has_permission (user , osf_permissions .ADMIN ):
80
- raise exceptions .PermissionDenied (
81
- detail = f'User needs to be affiliated with { inst .name } ' ,
82
- )
83
- node .remove_affiliated_institution (inst , user )
84
-
85
- for inst in add :
86
- if not user .is_affiliated_with_institution (inst ):
87
- raise exceptions .PermissionDenied (
88
- detail = f'User needs to be affiliated with { inst .name } ' ,
89
- )
90
- node .add_affiliated_institution (inst , user )
91
-
92
-
93
55
class RegionRelationshipField (RelationshipField ):
94
56
95
57
def to_internal_value (self , data ):
@@ -1479,13 +1441,10 @@ def get_storage_addons_url(self, obj):
1479
1441
},
1480
1442
)
1481
1443
1482
- class InstitutionRelated (JSONAPIRelationshipSerializer ):
1483
- id = ser .CharField (source = '_id' , required = False , allow_null = True )
1484
- class Meta :
1485
- type_ = 'institutions'
1486
-
1487
1444
1488
1445
class NodeInstitutionsRelationshipSerializer (BaseAPISerializer ):
1446
+ from api .institutions .serializers import InstitutionRelated # Avoid circular import
1447
+
1489
1448
data = ser .ListField (child = InstitutionRelated ())
1490
1449
links = LinksField ({
1491
1450
'self' : 'get_self_url' ,
0 commit comments