File: pagination.py

package info (click to toggle)
djangorestframework-gis 1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: python: 4,093; sh: 14; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 1,086 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from collections import OrderedDict

from rest_framework import pagination
from rest_framework.response import Response


class GeoJsonPagination(pagination.PageNumberPagination):
    """
    A geoJSON implementation of a pagination serializer.
    """

    page_size_query_param = 'page_size'

    def get_paginated_response(self, data):
        return Response(
            OrderedDict(
                [
                    ('type', 'FeatureCollection'),
                    ('count', self.page.paginator.count),
                    ('next', self.get_next_link()),
                    ('previous', self.get_previous_link()),
                    ('features', data['features']),
                ]
            )
        )

    def get_paginated_response_schema(self, view):
        schema = super().get_paginated_response_schema(view)
        schema["properties"]["features"] = schema["properties"].pop("results")
        schema["properties"] = {
            "type": {"type": "string", "enum": ["FeatureCollection"]},
            **schema["properties"],
        }
        return schema