Skip to content

Commit

Permalink
Changed V3 pagination to match Galaxy V3 API pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
fao89 committed Sep 1, 2020
1 parent 94b15f1 commit 2c91b1a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/7435.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed V3 pagination to match Galaxy V3 API pagination
84 changes: 84 additions & 0 deletions pulp_ansible/app/galaxy/v3/pagination.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions pulp_ansible/app/galaxy/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -73,6 +74,7 @@ class CollectionViewSet(
authentication_classes = []
permission_classes = []
serializer_class = CollectionSerializer
pagination_class = LimitOffsetPagination

def get_queryset(self):
"""
Expand Down Expand Up @@ -184,6 +186,7 @@ class CollectionVersionViewSet(
permission_classes = []
serializer_class = CollectionVersionSerializer
filterset_class = CollectionVersionFilter
pagination_class = LimitOffsetPagination

lookup_field = "version"

Expand Down
1 change: 1 addition & 0 deletions pulp_ansible/app/galaxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class GalaxyCollectionVersionList(generics.ListAPIView):

model = CollectionVersion
serializer_class = GalaxyCollectionVersionSerializer
pagination_class = pagination.PageNumberPagination
authentication_classes = []
permission_classes = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2c91b1a

Please sign in to comment.