django-rest-framework/rest_framework/pagination.py

90 lines
2.9 KiB
Python
Raw Normal View History

2013-04-25 15:47:34 +04:00
"""
Pagination serializers determine the structure of the output that should
be used for paginated responses.
"""
from __future__ import unicode_literals
2012-09-30 20:31:28 +04:00
from rest_framework import serializers
from rest_framework.templatetags.rest_framework import replace_query_param
2012-09-30 20:31:28 +04:00
class NextPageField(serializers.Field):
2012-10-01 18:49:19 +04:00
"""
Field that returns a link to the next page in paginated results.
"""
page_field = 'page'
def to_representation(self, value):
2012-09-30 20:31:28 +04:00
if not value.has_next():
return None
page = value.next_page_number()
2012-10-01 18:49:19 +04:00
request = self.context.get('request')
url = request and request.build_absolute_uri() or ''
return replace_query_param(url, self.page_field, page)
2012-09-30 20:31:28 +04:00
class PreviousPageField(serializers.Field):
2012-10-01 18:49:19 +04:00
"""
Field that returns a link to the previous page in paginated results.
"""
page_field = 'page'
def to_representation(self, value):
2012-09-30 20:31:28 +04:00
if not value.has_previous():
return None
page = value.previous_page_number()
2012-10-01 18:49:19 +04:00
request = self.context.get('request')
url = request and request.build_absolute_uri() or ''
return replace_query_param(url, self.page_field, page)
2012-09-30 20:31:28 +04:00
class DefaultObjectSerializer(serializers.ReadOnlyField):
"""
If no object serializer is specified, then this serializer will be applied
as the default.
"""
def __init__(self, source=None, many=None, context=None):
# Note: Swallow context and many kwargs - only required for
# eg. ModelSerializer.
super(DefaultObjectSerializer, self).__init__(source=source)
2012-10-01 18:49:19 +04:00
class BasePaginationSerializer(serializers.Serializer):
"""
A base class for pagination serializers to inherit from,
to make implementing custom serializers more easy.
"""
results_field = 'results'
2012-10-01 18:49:19 +04:00
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
2014-09-02 20:41:23 +04:00
try:
object_serializer = self.Meta.object_serializer_class
except AttributeError:
object_serializer = DefaultObjectSerializer
try:
list_serializer_class = object_serializer.Meta.list_serializer_class
except AttributeError:
list_serializer_class = serializers.ListSerializer
self.fields[results_field] = list_serializer_class(
2014-09-02 20:41:23 +04:00
child=object_serializer(),
source='object_list'
)
self.fields[results_field].bind(field_name=results_field, parent=self)
2012-09-30 20:31:28 +04:00
2012-10-01 18:49:19 +04:00
class PaginationSerializer(BasePaginationSerializer):
"""
A default implementation of a pagination serializer.
"""
count = serializers.ReadOnlyField(source='paginator.count')
2012-10-01 18:49:19 +04:00
next = NextPageField(source='*')
previous = PreviousPageField(source='*')