Initial Commit
This commit is contained in:
37
enhanced_drf_jsonapi/pagination.py
Normal file
37
enhanced_drf_jsonapi/pagination.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
from rest_framework.response import Response
|
||||
from rest_framework_json_api.pagination import JsonApiPageNumberPagination
|
||||
|
||||
|
||||
class NgxJsonApiPageNumberPagination(JsonApiPageNumberPagination):
|
||||
def get_paginated_response(self, data):
|
||||
next = None
|
||||
previous = None
|
||||
|
||||
if self.page.has_next():
|
||||
next = self.page.next_page_number()
|
||||
if self.page.has_previous():
|
||||
previous = self.page.previous_page_number()
|
||||
|
||||
return Response(
|
||||
{
|
||||
"results": data,
|
||||
"meta": OrderedDict(
|
||||
[
|
||||
("page", self.page.number),
|
||||
("pages", self.page.paginator.num_pages),
|
||||
("total_resources", self.page.paginator.count),
|
||||
("resources_per_page", self.page.paginator.per_page),
|
||||
]
|
||||
),
|
||||
"links": OrderedDict(
|
||||
[
|
||||
("first", self.build_link(1)),
|
||||
("last", self.build_link(self.page.paginator.num_pages)),
|
||||
("next", self.build_link(next)),
|
||||
("prev", self.build_link(previous)),
|
||||
]
|
||||
),
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user