api: improve pagination to show relevant data used for interface
This commit is contained in:
parent
5c5adfcccc
commit
606e32603e
|
@ -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,
|
||||||
|
}
|
||||||
|
)
|
|
@ -97,3 +97,4 @@ class PolicyBindingViewSet(ModelViewSet):
|
||||||
queryset = PolicyBinding.objects.all()
|
queryset = PolicyBinding.objects.all()
|
||||||
serializer_class = PolicyBindingSerializer
|
serializer_class = PolicyBindingSerializer
|
||||||
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
|
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
|
||||||
|
search_fields = ["policy__name"]
|
||||||
|
|
|
@ -140,7 +140,7 @@ SWAGGER_SETTINGS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
|
"DEFAULT_PAGINATION_CLASS": "passbook.api.pagination.Pagination",
|
||||||
"PAGE_SIZE": 100,
|
"PAGE_SIZE": 100,
|
||||||
"DEFAULT_FILTER_BACKENDS": [
|
"DEFAULT_FILTER_BACKENDS": [
|
||||||
"rest_framework_guardian.filters.ObjectPermissionsFilter",
|
"rest_framework_guardian.filters.ObjectPermissionsFilter",
|
||||||
|
|
Reference in New Issue