schon/evibes/pagination.py
Egor fureunoir Gorbunov 13e7af52aa Features: 1) Improved request processing in middleware by adding mutable QueryDict implementation; 2) Extended type annotations across various modules for enhanced type safety; 3) Refined JWT token lifetime configuration for environment-specific logic.
Fixes: 1) Addressed missing or incorrect imports and type hints with `# ty:ignore` markers; 2) Fixed search queryset error handling in filters module; 3) Resolved issues in viewsets with updated `@action` method usage.

Extra: Removed unused classes and dependencies (e.g., `BaseMutation`, `basedpyright`, and related packages); streamlined GraphQL mutation implementations; cleaned up unused arguments in model `save` methods.
2025-12-19 15:17:17 +03:00

68 lines
2.3 KiB
Python

from typing import Any
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
class CustomPagination(PageNumberPagination):
page_size_query_param: str = (
"page_size" # name of the query parameter, you can use any
)
def get_paginated_response(self, data: dict[str, Any]) -> Response:
if not self.page:
raise RuntimeError
return Response(
{
"links": {
"forward": self.get_next_link(),
"backward": self.get_previous_link(),
},
"counts": {
"total_pages": None or self.page.paginator.num_pages,
"page_size": None or self.page_size,
"total_items": None or self.page.paginator.count,
},
"data": data,
}
)
def get_paginated_response_schema(self, schema: dict[str, Any]) -> dict[str, Any]:
return {
"type": "object",
"properties": {
"links": {
"type": "object",
"properties": {
"forward": {
"type": "string",
"nullable": True,
"format": "uri",
"description": "URL to the next page",
},
"backward": {
"type": "string",
"nullable": True,
"format": "uri",
"description": "URL to the previous page",
},
},
},
"total_pages": {
"type": "integer",
"example": 10,
"description": "Total number of pages",
},
"page_size": {
"type": "integer",
"example": 10,
"description": "Number of items per page",
},
"total_items": {
"type": "integer",
"example": 100,
"description": "Total number of items",
},
"data": schema,
},
}