|
| 1 | +from rest_framework import serializers |
| 2 | +from rest_framework import pagination |
| 3 | +from rest_framework.templatetags.rest_framework import replace_query_param |
| 4 | + |
| 5 | +from rest_framework.resource_name import get_resource_name |
| 6 | + |
| 7 | + |
| 8 | +class NextPageLinkField(serializers.Field): |
| 9 | + """ |
| 10 | + Field that returns a link to the next page in paginated results. |
| 11 | + """ |
| 12 | + page_field = 'page' |
| 13 | + |
| 14 | + def to_native(self, value): |
| 15 | + if not value.has_next(): |
| 16 | + return None |
| 17 | + page = value.next_page_number() |
| 18 | + request = self.context.get('request') |
| 19 | + url = request and request.build_absolute_uri() or '' |
| 20 | + return replace_query_param(url, self.page_field, page) |
| 21 | + |
| 22 | + |
| 23 | +class NextPageField(serializers.Field): |
| 24 | + """ |
| 25 | + Field that returns a link to the next page in paginated results. |
| 26 | + """ |
| 27 | + page_field = 'page' |
| 28 | + |
| 29 | + def to_native(self, value): |
| 30 | + if not value.has_next(): |
| 31 | + return None |
| 32 | + return value.next_page_number() |
| 33 | + |
| 34 | + |
| 35 | +class PreviousPageLinkField(serializers.Field): |
| 36 | + """ |
| 37 | + Field that returns a link to the previous page in paginated results. |
| 38 | + """ |
| 39 | + page_field = 'page' |
| 40 | + |
| 41 | + def to_native(self, value): |
| 42 | + if not value.has_previous(): |
| 43 | + return None |
| 44 | + page = value.previous_page_number() |
| 45 | + request = self.context.get('request') |
| 46 | + url = request and request.build_absolute_uri() or '' |
| 47 | + return replace_query_param(url, self.page_field, page) |
| 48 | + |
| 49 | + |
| 50 | +class PreviousPageField(serializers.Field): |
| 51 | + """ |
| 52 | + Field that returns a link to the previous page in paginated results. |
| 53 | + """ |
| 54 | + page_field = 'page' |
| 55 | + |
| 56 | + def to_native(self, value): |
| 57 | + if not value.has_previous(): |
| 58 | + return None |
| 59 | + return value.previous_page_number() |
| 60 | + |
| 61 | + |
| 62 | +class EmberPaginationSerializer(pagination.BasePaginationSerializer): |
| 63 | + next = NextPageField(source='*') |
| 64 | + next_link = NextPageLinkField(source='*') |
| 65 | + previous = PreviousPageField(source='*') |
| 66 | + previous_link = PreviousPageField(source='*') |
| 67 | + count = serializers.Field(source='paginator.count') |
| 68 | + |
| 69 | + def __init__(self, *args, **kwargs): |
| 70 | + super(pagination.BasePaginationSerializer, self).__init__( |
| 71 | + *args, **kwargs) |
| 72 | + |
| 73 | + # get the dynamic root key |
| 74 | + results_field = get_resource_name( |
| 75 | + kwargs.get('context').get('view')) |
| 76 | + object_serializer = self.opts.object_serializer_class |
| 77 | + |
| 78 | + if 'context' in kwargs: |
| 79 | + context_kwarg = {'context': kwargs['context']} |
| 80 | + else: |
| 81 | + context_kwarg = {} |
| 82 | + |
| 83 | + self.fields[results_field] = object_serializer(source='object_list', **context_kwarg) |
0 commit comments