api: improve pagination to show relevant data used for interface

This commit is contained in:
Jens Langhammer 2020-11-29 22:14:03 +01:00
parent 5c5adfcccc
commit 606e32603e
3 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,29 @@
"""Pagination which includes total pages and current page"""
from rest_framework import pagination
from rest_framework.response import Response
class Pagination(pagination.PageNumberPagination):
"""Pagination which includes total pages and current page"""
def get_paginated_response(self, data):
previous_page_number = 0
if self.page.has_previous():
previous_page_number = self.page.previous_page_number()
next_page_number = 0
if self.page.has_next():
next_page_number = self.page.next_page_number()
return Response(
{
"pagination": {
"next": next_page_number,
"previous": previous_page_number,
"count": self.page.paginator.count,
"current": self.page.number,
"total_pages": self.page.paginator.num_pages,
"start_index": self.page.start_index(),
"end_index": self.page.end_index(),
},
"results": data,
}
)

View File

@ -97,3 +97,4 @@ class PolicyBindingViewSet(ModelViewSet):
queryset = PolicyBinding.objects.all()
serializer_class = PolicyBindingSerializer
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
search_fields = ["policy__name"]

View File

@ -140,7 +140,7 @@ SWAGGER_SETTINGS = {
}
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"DEFAULT_PAGINATION_CLASS": "passbook.api.pagination.Pagination",
"PAGE_SIZE": 100,
"DEFAULT_FILTER_BACKENDS": [
"rest_framework_guardian.filters.ObjectPermissionsFilter",