From 6fa589fefd48d98e4f0a11548b6c3e5ced58e31e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sun, 30 Sep 2012 17:31:28 +0100 Subject: Pagination support --- rest_framework/pagination.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rest_framework/pagination.py (limited to 'rest_framework/pagination.py') diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py new file mode 100644 index 00000000..398e6f3d --- /dev/null +++ b/rest_framework/pagination.py @@ -0,0 +1,34 @@ +from rest_framework import serializers + +# TODO: Support URLconf kwarg-style paging + + +class NextPageField(serializers.Field): + def to_native(self, value): + if not value.has_next(): + return None + page = value.next_page_number() + request = self.context['request'] + return request.build_absolute_uri('?page=%d' % page) + + +class PreviousPageField(serializers.Field): + def to_native(self, value): + if not value.has_previous(): + return None + page = value.previous_page_number() + request = self.context['request'] + return request.build_absolute_uri('?page=%d' % page) + + +class PaginationSerializer(serializers.Serializer): + count = serializers.Field(source='paginator.count') + next = NextPageField(source='*') + previous = PreviousPageField(source='*') + + def to_native(self, obj): + """ + Prevent default behaviour of iterating over elements, and serializing + each in turn. + """ + return self.convert_object(obj) -- cgit v1.2.3 From b16fb5777168246b1e217640b818a82eb6e2141b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 1 Oct 2012 15:49:19 +0100 Subject: Expand pagination support, add docs --- rest_framework/pagination.py | 62 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 8 deletions(-) (limited to 'rest_framework/pagination.py') diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 398e6f3d..f8b1fd1a 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -4,27 +4,64 @@ from rest_framework import serializers class NextPageField(serializers.Field): + """ + Field that returns a link to the next page in paginated results. + """ def to_native(self, value): if not value.has_next(): return None page = value.next_page_number() - request = self.context['request'] - return request.build_absolute_uri('?page=%d' % page) + request = self.context.get('request') + relative_url = '?page=%d' % page + if request: + return request.build_absolute_uri(relative_url) + return relative_url class PreviousPageField(serializers.Field): + """ + Field that returns a link to the previous page in paginated results. + """ def to_native(self, value): if not value.has_previous(): return None page = value.previous_page_number() - request = self.context['request'] - return request.build_absolute_uri('?page=%d' % page) + request = self.context.get('request') + relative_url = '?page=%d' % page + if request: + return request.build_absolute_uri('?page=%d' % page) + return relative_url -class PaginationSerializer(serializers.Serializer): - count = serializers.Field(source='paginator.count') - next = NextPageField(source='*') - previous = PreviousPageField(source='*') +class PaginationSerializerOptions(serializers.SerializerOptions): + """ + An object that stores the options that may be provided to a + pagination serializer by using the inner `Meta` class. + + Accessible on the instance as `serializer.opts`. + """ + def __init__(self, meta): + super(PaginationSerializerOptions, self).__init__(meta) + self.object_serializer_class = getattr(meta, 'object_serializer_class', + serializers.Field) + + +class BasePaginationSerializer(serializers.Serializer): + """ + A base class for pagination serializers to inherit from, + to make implementing custom serializers more easy. + """ + _options_class = PaginationSerializerOptions + _results_field = 'results' + + def __init__(self, *args, **kwargs): + """ + Override init to add in the object serializer field on-the-fly. + """ + super(BasePaginationSerializer, self).__init__(*args, **kwargs) + results_field = self._results_field + object_serializer = self.opts.object_serializer_class + self.fields[results_field] = object_serializer(source='object_list') def to_native(self, obj): """ @@ -32,3 +69,12 @@ class PaginationSerializer(serializers.Serializer): each in turn. """ return self.convert_object(obj) + + +class PaginationSerializer(BasePaginationSerializer): + """ + A default implementation of a pagination serializer. + """ + count = serializers.Field(source='paginator.count') + next = NextPageField(source='*') + previous = PreviousPageField(source='*') -- cgit v1.2.3 From ae8a8270042820f92b9d43597bde50d72c300513 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 2 Oct 2012 10:40:43 +0100 Subject: Make 'results_field' attribute of BasePaginationSerializer public. --- rest_framework/pagination.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rest_framework/pagination.py') diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index f8b1fd1a..131718fd 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -52,14 +52,14 @@ class BasePaginationSerializer(serializers.Serializer): to make implementing custom serializers more easy. """ _options_class = PaginationSerializerOptions - _results_field = 'results' + results_field = 'results' def __init__(self, *args, **kwargs): """ Override init to add in the object serializer field on-the-fly. """ super(BasePaginationSerializer, self).__init__(*args, **kwargs) - results_field = self._results_field + results_field = self.results_field object_serializer = self.opts.object_serializer_class self.fields[results_field] = object_serializer(source='object_list') -- cgit v1.2.3