|
1 | 1 | """ |
2 | 2 | Pagination fields |
3 | 3 | """ |
4 | | -# pylint: disable=no-init, too-few-public-methods, no-self-use |
5 | | -from collections import OrderedDict |
6 | | - |
7 | 4 | from rest_framework import serializers |
8 | | -from rest_framework import pagination |
9 | 5 | from rest_framework.views import Response |
| 6 | +from rest_framework.compat import OrderedDict |
| 7 | +from rest_framework.pagination import PageNumberPagination |
10 | 8 | from rest_framework.templatetags.rest_framework import replace_query_param |
11 | 9 |
|
12 | | -# DRF 2.4.X compatibility. |
13 | | -ReadOnlyField = getattr(serializers, 'ReadOnlyField', serializers.Field) |
14 | | - |
15 | | - |
16 | | -class NextPageLinkField(ReadOnlyField): |
17 | | - """ |
18 | | - Field that returns a link to the next page in paginated results. |
19 | | - """ |
20 | | - page_field = 'page' |
21 | | - |
22 | | - def to_representation(self, value): |
23 | | - if not value.has_next(): |
24 | | - return None |
25 | | - page = value.next_page_number() |
26 | | - request = self.context.get('request') |
27 | | - url = request and request.build_absolute_uri() or '' |
28 | | - return replace_query_param(url, self.page_field, page) |
29 | | - |
30 | | - |
31 | | -class NextPageField(ReadOnlyField): |
32 | | - """ |
33 | | - Field that returns a the next page number in paginated results. |
34 | | - """ |
35 | | - page_field = 'page' |
36 | | - |
37 | | - def to_representation(self, value): |
38 | | - if not value.has_next(): |
39 | | - return None |
40 | | - return value.next_page_number() |
41 | | - |
42 | | - |
43 | | -class PreviousPageLinkField(ReadOnlyField): |
44 | | - """ |
45 | | - Field that returns a link to the previous page in paginated results. |
46 | | - """ |
47 | | - page_field = 'page' |
48 | | - |
49 | | - def to_representation(self, value): |
50 | | - if not value.has_previous(): |
51 | | - return None |
52 | | - page = value.previous_page_number() |
53 | | - request = self.context.get('request') |
54 | | - url = request and request.build_absolute_uri() or '' |
55 | | - return replace_query_param(url, self.page_field, page) |
56 | | - |
57 | | - |
58 | | -class PreviousPageField(ReadOnlyField): |
59 | | - """ |
60 | | - Field that returns the previous page number in paginated results. |
61 | | - """ |
62 | | - page_field = 'page' |
63 | | - |
64 | | - def to_representation(self, value): |
65 | | - if not value.has_previous(): |
66 | | - return None |
67 | | - return value.previous_page_number() |
68 | | - |
69 | | - |
70 | | -class PageField(ReadOnlyField): |
71 | | - """ |
72 | | - Field that returns the current page number in paginated results. |
73 | | - """ |
74 | | - page_field = 'page' |
75 | | - |
76 | | - def to_representation(self, value): |
77 | | - return value.number |
78 | | - |
79 | | - |
80 | | -# compatibility for DRF 3.0 and older |
81 | | -try: |
82 | | - BasePagination = pagination.PageNumberPagination |
83 | | -except: |
84 | | - BasePagination = pagination.BasePaginationSerializer |
85 | | - |
86 | | - |
87 | | -class PaginationSerializer(BasePagination): |
88 | | - """ |
89 | | - Pagination serializer. |
90 | | - """ |
91 | | - next = NextPageField(source='*') |
92 | | - next_link = NextPageLinkField(source='*') |
93 | | - page = PageField(source='*') |
94 | | - previous = PreviousPageField(source='*') |
95 | | - previous_link = PreviousPageLinkField(source='*') |
96 | | - count = ReadOnlyField(source='paginator.count') |
97 | | - total = ReadOnlyField(source='paginator.num_pages') |
98 | | - |
99 | | - |
100 | | -class EmberPaginationSerializer(PaginationSerializer): |
101 | | - """ |
102 | | - Backwards compatibility for name change |
103 | | - """ |
104 | | - pass |
105 | | - |
106 | 10 |
|
107 | | -class PageNumberPagination(BasePagination): |
| 11 | +class PageNumberPagination(PageNumberPagination): |
108 | 12 | """ |
109 | 13 | A json-api compatible pagination format |
110 | 14 | """ |
|
0 commit comments