-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathpagination.py
40 lines (28 loc) · 1.15 KB
/
pagination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import logging
from collections import OrderedDict
from django.core.paginator import Paginator
from django.utils.functional import cached_property
from rest_framework.views import Response
from rest_framework_json_api.pagination import PageNumberPagination
from rest_framework.pagination import CursorPagination as DrfCursorPagination
logger = logging.getLogger(__name__)
class FuzzyPaginator(Paginator):
@cached_property
def count(self):
if not hasattr(self.object_list, 'fuzzy_count'):
logger.warning('%r has no fuzzy_count method. Falling back to a normal count', self.object_list)
return self.object_list.count()
return self.object_list.fuzzy_count()
class FuzzyPageNumberPagination(PageNumberPagination):
django_paginator_class = FuzzyPaginator
class CursorPagination(DrfCursorPagination):
ordering = '-id'
cursor_query_param = 'page[cursor]'
def get_paginated_response(self, data):
return Response({
'results': data,
'links': OrderedDict([
('next', self.get_next_link()),
('prev', self.get_previous_link()),
])
})