aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/pagination.py
diff options
context:
space:
mode:
authorTom Christie2012-10-30 14:32:31 +0000
committerTom Christie2012-10-30 14:32:31 +0000
commit9b30dab4f772f67a626e176dc4fae0a3ef9c2c81 (patch)
treeca138abf4792f58ffa28684f784f201ee1eef6d7 /rest_framework/pagination.py
parent7e5b1501b5cede61a9391fb1a751d2ebcdb37031 (diff)
parent4e7805cb24d73e7f706318b5e5a27e3f9ba39d14 (diff)
downloaddjango-rest-framework-9b30dab4f772f67a626e176dc4fae0a3ef9c2c81.tar.bz2
Merge branch 'restframework2' into rest-framework-2-merge2.0.0
Conflicts: .gitignore .travis.yml AUTHORS README.rst djangorestframework/mixins.py djangorestframework/renderers.py djangorestframework/resources.py djangorestframework/serializer.py djangorestframework/templates/djangorestframework/base.html djangorestframework/templates/djangorestframework/login.html djangorestframework/templatetags/add_query_param.py djangorestframework/tests/accept.py djangorestframework/tests/authentication.py djangorestframework/tests/content.py djangorestframework/tests/reverse.py djangorestframework/tests/serializer.py djangorestframework/views.py docs/examples.rst docs/examples/blogpost.rst docs/examples/modelviews.rst docs/examples/objectstore.rst docs/examples/permissions.rst docs/examples/pygments.rst docs/examples/views.rst docs/howto/alternativeframeworks.rst docs/howto/mixin.rst docs/howto/reverse.rst docs/howto/usingurllib2.rst docs/index.rst docs/topics/release-notes.md examples/sandbox/views.py rest_framework/__init__.py rest_framework/compat.py rest_framework/utils/breadcrumbs.py setup.py
Diffstat (limited to 'rest_framework/pagination.py')
-rw-r--r--rest_framework/pagination.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py
new file mode 100644
index 00000000..131718fd
--- /dev/null
+++ b/rest_framework/pagination.py
@@ -0,0 +1,80 @@
+from rest_framework import serializers
+
+# TODO: Support URLconf kwarg-style paging
+
+
+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.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.get('request')
+ relative_url = '?page=%d' % page
+ if request:
+ return request.build_absolute_uri('?page=%d' % page)
+ return relative_url
+
+
+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):
+ """
+ Prevent default behaviour of iterating over elements, and serializing
+ 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='*')