diff --git a/docs/topics/api/overview.rst b/docs/topics/api/overview.rst index 0c8da45619..9e3f0314dd 100644 --- a/docs/topics/api/overview.rst +++ b/docs/topics/api/overview.rst @@ -98,6 +98,7 @@ The following properties will be available in paginated responses: * *next*: the URL for the next page in the pagination. * *previous*: the URL for the previous page in the pagination. * *page_size*: The number of items per page in the pagination. +* *page_count*: The number of pages available in the pagination. * *count*: the total number of records. * *results*: the array containing the results for this page. diff --git a/src/olympia/api/pagination.py b/src/olympia/api/pagination.py index 2decd97c84..e8fd7671a4 100644 --- a/src/olympia/api/pagination.py +++ b/src/olympia/api/pagination.py @@ -17,6 +17,7 @@ class CustomPageNumberPagination(PageNumberPagination): # Note that self.page_size doesn't work, it contains the default # page size. ('page_size', self.page.paginator.per_page), + ('page_count', self.page.paginator.num_pages), ('count', self.page.paginator.count), ('next', self.get_next_link()), ('previous', self.get_previous_link()), @@ -42,6 +43,7 @@ class OneOrZeroPageNumberPagination(CustomPageNumberPagination): # return next/prev links. return Response(OrderedDict([ ('page_size', 1), + ('page_count', 1), ('count', len(data)), ('next', None), ('previous', None), diff --git a/src/olympia/api/tests/test_pagination.py b/src/olympia/api/tests/test_pagination.py index 76b0cae18a..83652717c1 100644 --- a/src/olympia/api/tests/test_pagination.py +++ b/src/olympia/api/tests/test_pagination.py @@ -31,6 +31,7 @@ class TestCustomPageNumberPagination(TestCase): assert response.status_code == status.HTTP_200_OK assert response.data == { 'page_size': 10, + 'page_count': 10, 'results': range(11, 21), 'previous': 'http://testserver/?page_size=10', 'next': 'http://testserver/?page=3&page_size=10', @@ -43,6 +44,7 @@ class TestCustomPageNumberPagination(TestCase): assert response.status_code == status.HTTP_200_OK assert response.data == { 'page_size': 25, + 'page_count': 4, 'results': range(1, 26), 'previous': None, 'next': 'http://testserver/?page=2', @@ -66,6 +68,7 @@ class TestESPageNumberPagination(TestCustomPageNumberPagination): response = view(request) assert response.data == { 'page_size': 5, + 'page_count': 5000, 'results': mock.ANY, 'previous': 'http://testserver/?page=4998&page_size=5', 'next': 'http://testserver/?page=5000&page_size=5', @@ -76,6 +79,7 @@ class TestESPageNumberPagination(TestCustomPageNumberPagination): response = view(request) assert response.data == { 'page_size': 5, + 'page_count': 5000, 'results': mock.ANY, 'previous': 'http://testserver/?page=4999&page_size=5', 'next': None, @@ -99,6 +103,7 @@ class TestOneOrZeroPageNumberPagination(TestCase): response = self.view(request) assert response.data == { 'page_size': 1, + 'page_count': 1, 'results': range(1, 2), 'previous': None, 'next': None, @@ -115,6 +120,7 @@ class TestOneOrZeroPageNumberPagination(TestCase): response = self.view(request) assert response.data == { 'page_size': 1, + 'page_count': 1, 'results': [], 'previous': None, 'next': None,