From 2c91b1ad261a6e0bac5ba3be5cc96a3d1a399e02 Mon Sep 17 00:00:00 2001 From: Fabricio Aguiar Date: Tue, 1 Sep 2020 12:11:49 -0300 Subject: [PATCH] Changed V3 pagination to match Galaxy V3 API pagination https://pulp.plan.io/issues/7435 closes #7435 --- CHANGES/7435.removal | 1 + pulp_ansible/app/galaxy/v3/pagination.py | 84 +++++++++++++++++++ pulp_ansible/app/galaxy/v3/views.py | 3 + pulp_ansible/app/galaxy/views.py | 1 + .../api/collection/v3/test_collection.py | 4 +- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 CHANGES/7435.removal create mode 100644 pulp_ansible/app/galaxy/v3/pagination.py diff --git a/CHANGES/7435.removal b/CHANGES/7435.removal new file mode 100644 index 000000000..1191e01cc --- /dev/null +++ b/CHANGES/7435.removal @@ -0,0 +1 @@ +Changed V3 pagination to match Galaxy V3 API pagination diff --git a/pulp_ansible/app/galaxy/v3/pagination.py b/pulp_ansible/app/galaxy/v3/pagination.py new file mode 100644 index 000000000..64c7806df --- /dev/null +++ b/pulp_ansible/app/galaxy/v3/pagination.py @@ -0,0 +1,84 @@ +from rest_framework import pagination +from rest_framework.response import Response +from rest_framework.utils.urls import remove_query_param, replace_query_param + + +class LimitOffsetPagination(pagination.LimitOffsetPagination): + """ + Pagination for V3. + + """ + + default_limit = 10 + max_limit = 100 + + def get_first_link(self): + """First link.""" + url = self.request.get_full_path() + url = replace_query_param(url, self.limit_query_param, self.limit) + + return replace_query_param(url, self.offset_query_param, 0) + + def get_last_link(self): + """Last link.""" + url = self.request.get_full_path() + url = replace_query_param(url, self.limit_query_param, self.limit) + + offset = self.count - self.limit if (self.count - self.limit) >= 0 else 0 + return replace_query_param(url, self.offset_query_param, offset) + + def get_next_link(self): + """Next link.""" + if self.offset + self.limit >= self.count: + return None + + url = self.request.get_full_path() + url = replace_query_param(url, self.limit_query_param, self.limit) + + offset = self.offset + self.limit + return replace_query_param(url, self.offset_query_param, offset) + + def get_previous_link(self): + """Previous link.""" + if self.offset <= 0: + return None + + url = self.request.get_full_path() + url = replace_query_param(url, self.limit_query_param, self.limit) + + if self.offset - self.limit <= 0: + return remove_query_param(url, self.offset_query_param) + + offset = self.offset - self.limit + return replace_query_param(url, self.offset_query_param, offset) + + def get_paginated_response(self, data): + """Returns paginated response.""" + return Response( + { + "meta": {"count": self.count}, + "links": { + "first": self.get_first_link(), + "previous": self.get_previous_link(), + "next": self.get_next_link(), + "last": self.get_last_link(), + }, + "data": data, + } + ) + + # Custom methods for working with pulp client + def init_from_request(self, request): + """Init form request.""" + self.request = request + self.offset = self.get_offset(request) + self.limit = self.get_limit(request) + + def paginate_proxy_response(self, data, count): + """Paginate proxy response.""" + self.count = count + + if self.count > self.limit and self.template is not None: + self.display_page_controls = True + + return self.get_paginated_response(data) diff --git a/pulp_ansible/app/galaxy/v3/views.py b/pulp_ansible/app/galaxy/v3/views.py index 9955330ac..91cf0136f 100644 --- a/pulp_ansible/app/galaxy/v3/views.py +++ b/pulp_ansible/app/galaxy/v3/views.py @@ -30,6 +30,7 @@ ) from pulp_ansible.app.galaxy.mixins import UploadGalaxyCollectionMixin +from pulp_ansible.app.galaxy.v3.pagination import LimitOffsetPagination from pulp_ansible.app.viewsets import CollectionVersionFilter @@ -73,6 +74,7 @@ class CollectionViewSet( authentication_classes = [] permission_classes = [] serializer_class = CollectionSerializer + pagination_class = LimitOffsetPagination def get_queryset(self): """ @@ -184,6 +186,7 @@ class CollectionVersionViewSet( permission_classes = [] serializer_class = CollectionVersionSerializer filterset_class = CollectionVersionFilter + pagination_class = LimitOffsetPagination lookup_field = "version" diff --git a/pulp_ansible/app/galaxy/views.py b/pulp_ansible/app/galaxy/views.py index 69b49d035..5ebbe40dc 100644 --- a/pulp_ansible/app/galaxy/views.py +++ b/pulp_ansible/app/galaxy/views.py @@ -167,6 +167,7 @@ class GalaxyCollectionVersionList(generics.ListAPIView): model = CollectionVersion serializer_class = GalaxyCollectionVersionSerializer + pagination_class = pagination.PageNumberPagination authentication_classes = [] permission_classes = [] diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_collection.py b/pulp_ansible/tests/functional/api/collection/v3/test_collection.py index 028dfe48f..1536a358d 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_collection.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_collection.py @@ -185,8 +185,8 @@ def test_collection_version_list(artifact, pulp_client, collection_detail): """Test the versions endpoint, listing the available versions of a given collection.""" # Version List Endpoint versions = pulp_client.using_handler(api.json_handler).get(collection_detail["versions_url"]) - assert versions["count"] == 1 - version = versions["results"][0] + assert versions["meta"]["count"] == 1 + version = versions["data"][0] assert version["version"] == "1.0.0" assert version["certification"] == "needs_review"