Skip to content

Commit

Permalink
Paginate dandiset upload response
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Nov 26, 2024
1 parent e2f5237 commit 6ccaeb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
13 changes: 9 additions & 4 deletions dandiapi/api/tests/test_dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,8 +1149,9 @@ def test_dandiset_rest_list_active_uploads(
response = authenticated_api_client.get(f'/api/dandisets/{ds.identifier}/uploads/')
assert response.status_code == 200
data = response.json()
assert len(data) == 1
assert data[0]['upload_id'] == upload.upload_id
assert data['count'] == 1
assert len(data['results']) == 1
assert data['results'][0]['upload_id'] == upload.upload_id


@pytest.mark.django_db
Expand Down Expand Up @@ -1187,10 +1188,14 @@ def test_dandiset_rest_clear_active_uploads(
assign_perm('owner', user, ds)
upload_factory(dandiset=ds)

assert len(authenticated_api_client.get(f'/api/dandisets/{ds.identifier}/uploads/').json()) == 1
response = authenticated_api_client.get(f'/api/dandisets/{ds.identifier}/uploads/').json()
assert response['count'] == 1
assert len(response['results']) == 1

response = authenticated_api_client.delete(f'/api/dandisets/{ds.identifier}/uploads/')
assert response.status_code == 204

assert ds.uploads.count() == 0
assert len(authenticated_api_client.get(f'/api/dandisets/{ds.identifier}/uploads/').json()) == 0
response = authenticated_api_client.get(f'/api/dandisets/{ds.identifier}/uploads/').json()
assert response['count'] == 0
assert len(response['results']) == 0
13 changes: 11 additions & 2 deletions dandiapi/api/views/dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
DandisetSearchQueryParameterSerializer,
DandisetSearchResultListSerializer,
DandisetUploadSerializer,
PaginationQuerySerializer,
UserSerializer,
VersionMetadataSerializer,
)
Expand Down Expand Up @@ -467,6 +468,7 @@ def users(self, request, dandiset__pk): # noqa: C901
@swagger_auto_schema(
methods=['GET'],
manual_parameters=[DANDISET_PK_PARAM],
query_serializer=PaginationQuerySerializer,
request_body=no_body,
operation_summary='List active/incomplete uploads in this dandiset.',
)
Expand All @@ -478,8 +480,15 @@ def uploads(self, request, dandiset__pk):
self.require_owner_perm(dandiset)

uploads: QuerySet[Upload] = dandiset.uploads.all()
serializer = DandisetUploadSerializer(instance=uploads, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

# Paginate and return
page = self.paginate_queryset(uploads)
if page is not None:
serializer = DandisetUploadSerializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = DandisetUploadSerializer(uploads, many=True)
return Response(serializer.data)

@swagger_auto_schema(
manual_parameters=[DANDISET_PK_PARAM],
Expand Down
5 changes: 5 additions & 0 deletions dandiapi/api/views/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ class AssetPathsQueryParameterSerializer(serializers.Serializer):
path_prefix = serializers.CharField(default='')


class PaginationQuerySerializer(serializers.Serializer):
page = serializers.IntegerField(default=1)
page_size = serializers.IntegerField(default=100)


class AssetFileSerializer(AssetSerializer):
class Meta(AssetSerializer.Meta):
fields = ['asset_id', 'url']
Expand Down

0 comments on commit 6ccaeb6

Please sign in to comment.