Skip to content

Commit

Permalink
add samplesheets api pagination (#1994)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 10, 2024
1 parent 185955a commit 5a2aca1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@
'knox.auth.TokenAuthentication',
),
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
'DEFAULT_PAGINATION_CLASS': (
'rest_framework.pagination.PageNumberPagination'
),
'PAGE_SIZE': env.int('SODAR_API_PAGE_SIZE', 100),
}

Expand Down
78 changes: 65 additions & 13 deletions samplesheets/tests/test_views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,17 +739,14 @@ def test_get(self):
with self.login(self.user_contributor):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
local_date_created = self.ticket.date_created.astimezone(
timezone.get_current_timezone()
)
expected = {
'sodar_uuid': str(self.ticket.sodar_uuid),
'label': self.ticket.label,
'ticket': self.ticket.ticket,
'study': str(self.study.sodar_uuid),
'assay': str(self.assay.sodar_uuid),
'path': self.ticket.path,
'date_created': local_date_created.isoformat(),
'date_created': self.get_drf_datetime(self.ticket.date_created),
'date_expires': self.ticket.date_expires,
'user': self.get_serialized_user(self.user),
'is_active': self.ticket.is_active(),
Expand All @@ -771,23 +768,49 @@ def test_get_active(self):
with self.login(self.user_contributor):
response = self.client.get(self.url + '?active=1')
self.assertEqual(response.status_code, 200)
local_date_created = self.ticket.date_created.astimezone(
timezone.get_current_timezone()
)
expected = {
'sodar_uuid': str(self.ticket.sodar_uuid),
'label': self.ticket.label,
'ticket': self.ticket.ticket,
'study': str(self.study.sodar_uuid),
'assay': str(self.assay.sodar_uuid),
'path': self.ticket.path,
'date_created': local_date_created.isoformat(),
'date_created': self.get_drf_datetime(self.ticket.date_created),
'date_expires': self.ticket.date_expires,
'user': self.get_serialized_user(self.user),
'is_active': self.ticket.is_active(),
}
self.assertEqual(json.loads(response.content), [expected])

def test_get_pagination(self):
"""Test GET with pagination"""
url = self.url + '?page=1'
with self.login(self.user_contributor):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
expected = {
'count': 1,
'next': None,
'previous': None,
'results': [
{
'sodar_uuid': str(self.ticket.sodar_uuid),
'label': self.ticket.label,
'ticket': self.ticket.ticket,
'study': str(self.study.sodar_uuid),
'assay': str(self.assay.sodar_uuid),
'path': self.ticket.path,
'date_created': self.get_drf_datetime(
self.ticket.date_created
),
'date_expires': self.ticket.date_expires,
'user': self.get_serialized_user(self.user),
'is_active': self.ticket.is_active(),
}
],
}
self.assertEqual(json.loads(response.content), expected)


class TestIrodsDataRequestRetrieveAPIView(
IrodsDataRequestMixin, SampleSheetAPIViewTestBase
Expand Down Expand Up @@ -868,7 +891,7 @@ def setUp(self):
)

def test_get(self):
"""Test retrieving iRODS data request list"""
"""Test IrodsDataRequestListAPIView GET"""
response = self.request_knox(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)
Expand All @@ -887,24 +910,53 @@ def test_get(self):
}
self.assertEqual(response_data[0], expected)

def test_get_pagination(self):
"""Test GET with pagination"""
url = self.url + '?page=1'
response = self.request_knox(url)
self.assertEqual(response.status_code, 200)
response_data = json.loads(response.content)
expected = {
'count': 1,
'next': None,
'previous': None,
'results': [
{
'project': str(self.project.sodar_uuid),
'action': IRODS_REQUEST_ACTION_DELETE,
'path': self.request.path,
'target_path': '',
'user': self.get_serialized_user(self.user_contributor),
'status': IRODS_REQUEST_STATUS_ACTIVE,
'status_info': '',
'description': self.request.description,
'date_created': self.get_drf_datetime(
self.request.date_created
),
'sodar_uuid': str(self.request.sodar_uuid),
}
],
}
self.assertEqual(response_data, expected)

def test_get_failed_as_superuser(self):
"""Test retrieving list as superuser with failed request"""
"""Test GET as superuser with failed request"""
self.request.status = IRODS_REQUEST_STATUS_FAILED
self.request.save()
response = self.request_knox(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)

def test_get_accepted_as_superuser(self):
"""Test retrieving list as superuser with accepted request"""
"""Test GET as superuser with accepted request"""
self.request.status = IRODS_REQUEST_STATUS_ACCEPTED
self.request.save()
response = self.request_knox(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 0)

def test_get_accepted_as_owner(self):
"""Test retrieving list as owner with accepted request"""
"""Test GET as owner with accepted request"""
self.request.status = IRODS_REQUEST_STATUS_ACCEPTED
self.request.save()
response = self.request_knox(
Expand All @@ -914,7 +966,7 @@ def test_get_accepted_as_owner(self):
self.assertEqual(len(response.data), 0)

def test_get_accepted_as_request_creator(self):
"""Test retrieving list as request creator with accepted request"""
"""Test GET as request creator with accepted request"""
self.request.status = IRODS_REQUEST_STATUS_ACCEPTED
self.request.save()
response = self.request_knox(
Expand Down
17 changes: 17 additions & 0 deletions samplesheets/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
SODARAPIBaseMixin,
SODARAPIBaseProjectMixin,
SODARAPIGenericProjectMixin,
SODARPageNumberPagination,
)
from projectroles.utils import build_secret

Expand Down Expand Up @@ -388,17 +389,23 @@ class IrodsAccessTicketListAPIView(
"""
List iRODS access tickets for a project.
Supports optional pagination for listing by providing the ``page`` query
string. This will return results in the Django Rest Framework
``PageNumberPagination`` format.
**URL:** ``/samplesheets/api/irods/ticket/list/{Project.sodar_uuid}``
**Methods:** ``GET``
**Query parameters:**
- ``active`` (boolean, optional, default=false)
- ``page``: Page number for paginated results (int, optional)
**Returns:** List of ticket dicts, see ``IrodsAccessTicketRetrieveAPIView``
"""

pagination_class = SODARPageNumberPagination
permission_required = 'samplesheets.edit_sheet'
serializer_class = IrodsAccessTicketSerializer

Expand Down Expand Up @@ -584,13 +591,22 @@ class IrodsDataRequestListAPIView(
all requests with the status of ACTIVE or FAILED. If called as a
contributor, returns the user's own requests regardless of the state.
Supports optional pagination for listing by providing the ``page`` query
string. This will return results in the Django Rest Framework
``PageNumberPagination`` format.
**URL:** ``/samplesheets/api/irods/requests/{Project.sodar_uuid}``
**Methods:** ``GET``
**Query parameters:**
- ``page``: Page number for paginated results (int, optional)
**Returns:** List of iRODS data requests (list of dicts)
"""

pagination_class = SODARPageNumberPagination
permission_required = 'samplesheets.edit_sheet'
serializer_class = IrodsDataRequestSerializer

Expand Down Expand Up @@ -879,6 +895,7 @@ def get(self, request, *args, **kwargs):
return Response(ret, status=status.HTTP_200_OK)


# TODO: Standardize output and add pagination
class ProjectIrodsFileListAPIView(
SamplesheetsAPIVersioningMixin, SODARAPIBaseProjectMixin, APIView
):
Expand Down

0 comments on commit 5a2aca1

Please sign in to comment.