"""
Pagination fields
"""
from collections import OrderedDict
from rest_framework import serializers
from rest_framework.views import Response
from rest_framework.pagination import PageNumberPagination
from rest_framework.templatetags.rest_framework import replace_query_param


class PageNumberPagination(PageNumberPagination):
    """
    A json-api compatible pagination format
    """

    page_size_query_param = 'page_size'
    max_page_size = 100

    def build_link(self, index):
        if not index:
            return None
        url = self.request and self.request.build_absolute_uri() or ''
        return replace_query_param(url, 'page', index)

    def get_paginated_response(self, data):
        next = None
        previous = None

        if self.page.has_next():
            next = self.page.next_page_number()
        if self.page.has_previous():
            previous = self.page.previous_page_number()

        return Response({
            'results': data,
            'meta': {
                'pagination': OrderedDict([
                    ('page', self.page.number),
                    ('pages', self.page.paginator.num_pages),
                    ('count', self.page.paginator.count),
                ])
            },
            'links': OrderedDict([
                ('first', self.build_link(1)),
                ('last', self.build_link(self.page.paginator.num_pages)),
                ('next', self.build_link(next)),
                ('prev', self.build_link(previous))
            ])
        })

@@ -194,16 +194,39 @@
 gination
+, LimitOffsetPagination
 %0Afrom re
@@ -1547,8 +1547,1545 @@
      %7D)%0A
+%0A%0Aclass LimitOffsetPagination(LimitOffsetPagination):%0A    %22%22%22%0A    A limit/offset based style. For example:%0A    http://api.example.org/accounts/?page%5Blimit%5D=100%0A    http://api.example.org/accounts/?page%5Boffset%5D=400&page%5Blimit%5D=100%0A    %22%22%22%0A    limit_query_param = 'page%5Blimit%5D'%0A    offset_query_param = 'page%5Boffset%5D'%0A%0A    def get_last_link(self):%0A        if self.count == 0:%0A            return None%0A%0A        url = self.request.build_absolute_uri()%0A        url = replace_query_param(url, self.limit_query_param, self.limit)%0A%0A        offset = self.count - self.limit%0A        return replace_query_param(url, self.offset_query_param, offset)%0A%0A    def get_first_link(self):%0A        if self.count == 0:%0A            return None%0A        %0A        url = self.request.build_absolute_uri()%0A        url = replace_query_param(url, self.limit_query_param, self.limit)%0A%0A        offset = 0%0A        return replace_query_param(url, self.offset_query_param, offset)%0A    %0A    def get_paginated_response(self, data):%0A        return Response(%7B%0A            'results': data,%0A            'meta': %7B%0A                'pagination': OrderedDict(%5B%0A                    ('count', self.count),%0A                    ('limit', self.limit),%0A                    ('offset', self.offset),%0A                %5D)%0A            %7D,%0A            'links': OrderedDict(%5B%0A                ('first', self.get_first_link()),%0A                ('last', self.get_last_link()),%0A                ('next', self.get_next_link()),%0A                ('prev', self.get_previous_link())%0A            %5D)%0A        %7D)

Add LimitOffsetPagination