77)
88from api .base .serializers import (
99 VersionedDateTimeField , HideIfRegistration , IDField ,
10- JSONAPIRelationshipSerializer ,
1110 JSONAPISerializer , LinksField ,
1211 NodeFileHyperLinkField , RelationshipField ,
1312 ShowIfVersion , TargetTypeField , TypeField ,
14- WaterbutlerLink , relationship_diff , BaseAPISerializer ,
13+ WaterbutlerLink , BaseAPISerializer ,
1514 HideIfWikiDisabled , ShowIfAdminScopeOrAnonymous ,
1615 ValuesListField , TargetField ,
1716)
2120 get_user_auth , is_truthy ,
2221)
2322from api .base .versioning import get_kebab_snake_case_field
23+ from api .institutions .utils import update_institutions
2424from api .taxonomies .serializers import TaxonomizableSerializerMixin
2525from django .apps import apps
2626from django .conf import settings
3434from addons .osfstorage .models import Region
3535from osf .exceptions import NodeStateError
3636from osf .models import (
37- Comment , DraftRegistration , ExternalAccount , Institution ,
37+ Comment , DraftRegistration , ExternalAccount ,
3838 RegistrationSchema , AbstractNode , PrivateLink , Preprint ,
3939 RegistrationProvider , OSFGroup , NodeLicense , DraftNode ,
4040 Registration , Node ,
@@ -52,44 +52,6 @@ def to_internal_value(self, data):
5252 return self .get_object (data )
5353
5454
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-
9355class RegionRelationshipField (RelationshipField ):
9456
9557 def to_internal_value (self , data ):
@@ -1479,13 +1441,10 @@ def get_storage_addons_url(self, obj):
14791441 },
14801442 )
14811443
1482- class InstitutionRelated (JSONAPIRelationshipSerializer ):
1483- id = ser .CharField (source = '_id' , required = False , allow_null = True )
1484- class Meta :
1485- type_ = 'institutions'
1486-
14871444
14881445class NodeInstitutionsRelationshipSerializer (BaseAPISerializer ):
1446+ from api .institutions .serializers import InstitutionRelated # Avoid circular import
1447+
14891448 data = ser .ListField (child = InstitutionRelated ())
14901449 links = LinksField ({
14911450 'self' : 'get_self_url' ,
0 commit comments