33 lines
1000 B
Python
33 lines
1000 B
Python
|
|
"""Shared pagination classes."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from collections import OrderedDict
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from rest_framework import pagination
|
||
|
|
from rest_framework.response import Response
|
||
|
|
|
||
|
|
|
||
|
|
class PageNumberPagination(pagination.PageNumberPagination):
|
||
|
|
"""Page/page_size pagination with a hard upper bound on page size."""
|
||
|
|
|
||
|
|
page_size = 20
|
||
|
|
page_size_query_param = "page_size"
|
||
|
|
max_page_size = 100
|
||
|
|
|
||
|
|
def get_paginated_response(self, data: Any) -> Response:
|
||
|
|
return Response(
|
||
|
|
OrderedDict(
|
||
|
|
[
|
||
|
|
("count", self.page.paginator.count),
|
||
|
|
("page", self.page.number),
|
||
|
|
("page_size", self.get_page_size(self.request)),
|
||
|
|
("total_pages", self.page.paginator.num_pages),
|
||
|
|
("next", self.get_next_link()),
|
||
|
|
("previous", self.get_previous_link()),
|
||
|
|
("results", data),
|
||
|
|
]
|
||
|
|
)
|
||
|
|
)
|