Skip to content

Commit

Permalink
Merge branch 'preprint-institutions-list-api' of https://github.com/j…
Browse files Browse the repository at this point in the history
…ohnetordoff/osf.io into institutional-affilation-clean-up

* 'preprint-institutions-list-api' of https://github.com/johnetordoff/osf.io:
  add preprint institution affiliation endpoint

# Conflicts:
#	api/preprints/permissions.py
#	api/preprints/serializers.py
#	api/preprints/urls.py
#	api/preprints/views.py
#	api_tests/preprints/views/test_preprint_institutions.py
  • Loading branch information
John Tordoff committed Jun 27, 2024
2 parents bc18f60 + 0678dc2 commit e0fa094
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
1 change: 0 additions & 1 deletion api/preprints/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def has_object_permission(self, request, view, obj):

class PreprintInstitutionPermission(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
print(obj, obj.is_public)
if obj.is_public:
return True

Expand Down
3 changes: 2 additions & 1 deletion api/preprints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from api.base.exceptions import Conflict, JSONAPIException
from api.base.serializers import (
BaseAPISerializer,
JSONAPISerializer,
IDField,
TypeField,
Expand Down Expand Up @@ -542,7 +543,7 @@ def update(self, instance, validated_data):
})


class PreprintsInstitutionsSerializer(BaseAPISerializer):
class PreprintsInstitutionRelationshipSerializer(BaseAPISerializer):
id = IDField(read_only=True, source='_id')
name = ser.CharField(read_only=True)
type = ser.SerializerMethodField(read_only=True)
Expand Down
3 changes: 1 addition & 2 deletions api/preprints/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
re_path(r'^(?P<preprint_id>\w+)/requests/$', views.PreprintRequestListCreate.as_view(), name=views.PreprintRequestListCreate.view_name),
re_path(r'^(?P<preprint_id>\w+)/subjects/$', views.PreprintSubjectsList.as_view(), name=views.PreprintSubjectsList.view_name),
re_path(r'^(?P<preprint_id>\w+)/institutions/$', views.PreprintInstitutionsList.as_view(), name=views.PreprintInstitutionsList.view_name),
re_path(r'^(?P<preprint_id>\w+)/relationships/institutions/$', views.PreprintInstitutionsList.as_view(), name=views.PreprintInstitutionsList.view_name),

re_path(r'^(?P<preprint_id>\w+)/relationships/institutions/$', views.PreprintInstitutionsRelationshipList.as_view(), name=views.PreprintInstitutionsRelationshipList.view_name),
]
43 changes: 36 additions & 7 deletions api/preprints/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
from api.base.metrics import PreprintMetricsViewMixin
from osf.metrics import PreprintDownload, PreprintView
from api.institutions.serializers import InstitutionSerializer
from api.base.parsers import JSONAPIRelationshipParser
from api.base.parsers import JSONAPIRelationshipParserForRegularJSON
from api.preprints.serializers import PreprintsInstitutionRelationshipSerializer
from api.nodes.permissions import WriteOrPublicForRelationshipInstitutions


class PreprintMixin(NodeMixin):
Expand Down Expand Up @@ -633,19 +637,44 @@ class PreprintInstitutionsList(JSONAPIBaseView, generics.ListAPIView, ListFilter
PreprintInstitutionPermission,
)

required_read_scopes = [CoreScopes.NODE_BASE_READ, CoreScopes.INSTITUTION_READ]
required_read_scopes = [CoreScopes.PREPRINTS_READ, CoreScopes.INSTITUTION_READ]
required_write_scopes = [CoreScopes.NULL]
serializer_class = InstitutionSerializer

model = Institution
view_category = 'nodes'
view_name = 'node-institutions'

ordering = ('-id',)
view_category = 'preprints'
view_name = 'preprints-institutions'

def get_resource(self):
return self.get_preprint()

def get_queryset(self):
resource = self.get_resource()
return resource.affiliated_institutions.all() or []
return self.get_resource().affiliated_institutions.all()


class PrprintInstitutionsRelationship(JSONAPIBaseView, generics.RetrieveUpdateDestroyAPIView, generics.CreateAPIView, PreprintMixin):
""" """
permission_classes = (
drf_permissions.IsAuthenticatedOrReadOnly,
base_permissions.TokenHasScope,
WriteOrPublicForRelationshipInstitutions,
)
required_read_scopes = [CoreScopes.NODE_BASE_READ]
required_write_scopes = [CoreScopes.NODE_BASE_WRITE]
serializer_class = PreprintsInstitutionRelationshipSerializer
parser_classes = (JSONAPIRelationshipParser, JSONAPIRelationshipParserForRegularJSON, )

view_category = 'preprints'
view_name = 'preprint-relationships-institutions'

def get_resource(self):
return self.get_preprint(check_object_permissions=False)

def get_object(self):
preprint = self.get_resource()
obj = {
'data': preprint.affiliated_institutions.all(),
'self': preprint,
}
self.check_object_permissions(self.request, obj)
return obj
4 changes: 4 additions & 0 deletions api_tests/preprints/views/test_preprint_institutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def url(self, public_preprint):
def user(self):
return AuthUserFactory()

@pytest.fixture()
def institution(self):
return InstitutionFactory()

@pytest.fixture()
def public_preprint(self):
return PreprintFactory()
Expand Down

0 comments on commit e0fa094

Please sign in to comment.