Skip to content

Commit

Permalink
add institution relationship view
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Jun 21, 2024
1 parent 016482a commit 504ab6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions api/preprints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def to_internal_value(self, license_id):

class PreprintSerializer(TaxonomizableSerializerMixin, MetricsSerializerMixin, JSONAPISerializer):
filterable_fields = frozenset([
'affiliated_institutions',
'id',
'date_created',
'date_modified',
Expand Down Expand Up @@ -191,9 +192,9 @@ class PreprintSerializer(TaxonomizableSerializerMixin, MetricsSerializerMixin, J
))

affiliated_institutions = RelationshipField(
related_view='preprints:preprints-affiliated-institutions',
related_view='preprints:preprint-institution',
related_view_kwargs={'preprint_id': '<_id>'},
self_view='preprints:preprints-affiliated-institutions',
self_view='preprints:preprint-institution',
self_view_kwargs={'preprint_id': '<_id>'},
read_only=False,
required=False,
Expand Down
32 changes: 32 additions & 0 deletions api/preprints/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
from api.base.metrics import PreprintMetricsViewMixin
from osf.metrics import PreprintDownload, PreprintView

from api.institutions.serializers import InstitutionSerializer
from osf.models import Institution


class PreprintMixin(NodeMixin):
serializer_class = PreprintSerializer
preprint_lookup_url_kwarg = 'preprint_id'
Expand Down Expand Up @@ -614,3 +618,31 @@ def get_default_queryset(self):

def get_queryset(self):
return self.get_queryset_from_request()


class PreprintInstitutionsList(JSONAPIBaseView, generics.ListAPIView, ListFilterMixin, PreprintMixin):
"""The documentation for this endpoint can be found [here](https://developer.osf.io/#operation/nodes_institutions_list).
"""
permission_classes = (
drf_permissions.IsAuthenticatedOrReadOnly,
base_permissions.TokenHasScope,
AdminOrPublic,
)

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

model = Institution
view_category = 'preprint'
view_name = 'preprint-institutions'

ordering = ('-id',)

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

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

0 comments on commit 504ab6c

Please sign in to comment.