66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
"""
|
|
Custom pagination class for DRF to support dynamic page_size query parameter
|
|
and unified response format
|
|
"""
|
|
from rest_framework.pagination import PageNumberPagination
|
|
from .response import get_request_id
|
|
|
|
|
|
class CustomPageNumberPagination(PageNumberPagination):
|
|
"""
|
|
Custom pagination class that allows clients to override the page size
|
|
via the page_size query parameter.
|
|
|
|
Default page size: 10
|
|
Max page size: 100
|
|
|
|
Returns unified format with success field
|
|
"""
|
|
page_size = 10
|
|
page_size_query_param = 'page_size'
|
|
max_page_size = 100
|
|
|
|
def paginate_queryset(self, queryset, request, view=None):
|
|
"""
|
|
Override to store request for later use in get_paginated_response
|
|
"""
|
|
self.request = request
|
|
return super().paginate_queryset(queryset, request, view)
|
|
|
|
def get_paginated_response(self, data):
|
|
"""
|
|
Return a paginated response with unified format including success field
|
|
"""
|
|
from rest_framework.response import Response
|
|
|
|
response_data = {
|
|
'success': True,
|
|
'count': self.page.paginator.count,
|
|
'next': self.get_next_link(),
|
|
'previous': self.get_previous_link(),
|
|
'results': data
|
|
}
|
|
|
|
# Add request_id if request is available
|
|
if hasattr(self, 'request') and self.request:
|
|
response_data['request_id'] = get_request_id(self.request)
|
|
|
|
return Response(response_data)
|
|
|
|
|
|
class LargeTablePagination(CustomPageNumberPagination):
|
|
"""
|
|
Pagination class for large reference tables (e.g., SeedKeywords).
|
|
|
|
Default page size: 25
|
|
Max page size: 500
|
|
|
|
Important: Server-side sorting/filtering is applied to ALL records first,
|
|
then only the requested page is returned. This ensures:
|
|
- Sorting by volume returns the 500 highest/lowest volume records globally
|
|
- Filters apply to all records, not just the visible page
|
|
- Pagination shows accurate total counts
|
|
"""
|
|
page_size = 25
|
|
max_page_size = 500
|