Skip to content

Commit 04fd1bf

Browse files
committed
Added GeoJsonPagination
1 parent 0e315be commit 04fd1bf

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

README.rst

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Compatibility with DRF, Django and Python
2525

2626
=============== ============================ ==================== ==================================
2727
DRF-gis version DRF version Django version Python version
28+
**0.9.3** **3.1.X** **1.5.x** to **1.8** **2.6** to **3.4**
2829
**0.9.2** **3.1.X** **1.5.x** to **1.8** **2.6** to **3.4**
2930
**0.9.1** **3.1.X** **1.5.x** to **1.8** **2.6** to **3.4**
3031
**0.9** **3.1.X** **1.5.x** to **1.8** **2.6**, **2.7**, **3.3**, **3.4**
@@ -138,9 +139,6 @@ the above example, the ``GeoFeatureModelSerializer`` will output:
138139
If you are serializing an object list, ``GeoFeatureModelSerializer``
139140
will create a ``FeatureCollection``:
140141

141-
(**NOTE:** This currenty does not work with the default pagination
142-
serializer)
143-
144142
.. code-block:: javascript
145143
146144
{
@@ -294,6 +292,54 @@ be saved as Polygons. Example:
294292
geo_field = 'geometry'
295293
bbox_geo_field = 'bbox_geometry'
296294
295+
Pagination
296+
----------
297+
298+
We provide a ``GeoJsonPagination`` class.
299+
300+
GeoJsonPagination
301+
~~~~~~~~~~~~~~~~~
302+
303+
Based on ``rest_framework.pagination.PageNumberPagination``.
304+
305+
Code example:
306+
307+
.. code-block:: python
308+
309+
from rest_framework_gis.pagination import GeoJsonPagination
310+
# --- other omitted imports --- #
311+
312+
class GeojsonLocationList(generics.ListCreateAPIView):
313+
# -- other omitted view attributes --- #
314+
pagination_class = GeoJsonPagination
315+
316+
Example result response (cut to one element only instead of 10):
317+
318+
.. code-block:: javascript
319+
320+
{
321+
"type": "FeatureCollection",
322+
"count": 25,
323+
"next": "http://localhost:8000/geojson/?page=2",
324+
"previous": null,
325+
"features": [
326+
{
327+
"type": "Feature",
328+
"geometry": {
329+
"type": "Point",
330+
"coordinates": [
331+
42.0,
332+
50.0
333+
]
334+
},
335+
"properties": {
336+
"name": "test"
337+
}
338+
}
339+
]
340+
}
341+
342+
297343
Filters
298344
-------
299345

rest_framework_gis/pagination.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
try:
2+
from collections import OrderedDict
3+
# python 2.6
4+
except ImportError: # pragma: no cover
5+
from ordereddict import OrderedDict
6+
7+
from rest_framework import pagination
8+
from rest_framework.response import Response
9+
10+
11+
class GeoJsonPagination(pagination.PageNumberPagination):
12+
"""
13+
A geoJSON implementation of a pagination serializer.
14+
"""
15+
def get_paginated_response(self, data):
16+
return Response(OrderedDict([
17+
('type', 'FeatureCollection'),
18+
('count', self.page.paginator.count),
19+
('next', self.get_next_link()),
20+
('previous', self.get_previous_link()),
21+
('features', data['features'])
22+
]))

tests/django_restframework_gis_tests/serializers.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
'LocationGeoFeatureSerializer',
1313
'LocationGeoFeatureSlugSerializer',
1414
'LocationGeoFeatureFalseIDSerializer',
15-
'PaginatedLocationGeoFeatureSerializer',
1615
'LocatedFileGeoFeatureSerializer',
1716
'BoxedLocationGeoFeatureSerializer',
1817
'LocationGeoFeatureBboxSerializer',
@@ -68,15 +67,6 @@ class Meta:
6867
id_field = False
6968

7069

71-
class PaginatedLocationGeoFeatureSerializer(pagination.PageNumberPagination):
72-
page_size_query_param = 'limit'
73-
page_size = 40
74-
max_page_size = 10000
75-
76-
class Meta:
77-
object_serializer_class = LocationGeoFeatureSerializer
78-
79-
8070
class LocatedFileGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer):
8171
""" located file geo serializer """
8272
details = serializers.HyperlinkedIdentityField(view_name='api_geojson_located_file_details')

tests/django_restframework_gis_tests/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,14 @@ class Meta:
524524
exclude = ('geometry', )
525525
with self.assertRaises(ImproperlyConfigured):
526526
LocationGeoFeatureSerializer(instance=self.l1)
527+
528+
def test_geojson_pagination(self):
529+
self._create_locations()
530+
response = self.client.get(self.geojson_location_list_url)
531+
self.assertEqual(response.data['type'], 'FeatureCollection')
532+
self.assertEqual(len(response.data['features']), 2)
533+
response = self.client.get('{0}?page_size=1'.format(self.geojson_location_list_url))
534+
self.assertEqual(response.data['type'], 'FeatureCollection')
535+
self.assertEqual(len(response.data['features']), 1)
536+
self.assertIn('next', response.data)
537+
self.assertIn('previous', response.data)

tests/django_restframework_gis_tests/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from rest_framework import generics
22
from rest_framework.filters import DjangoFilterBackend
33
from rest_framework_gis.filters import *
4+
from rest_framework_gis.pagination import GeoJsonPagination
45

56
from .models import *
67
from .serializers import *
@@ -28,7 +29,8 @@ class GeojsonLocationList(generics.ListCreateAPIView):
2829
model = Location
2930
serializer_class = LocationGeoFeatureSerializer
3031
queryset = Location.objects.all()
31-
pagination_class = PaginatedLocationGeoFeatureSerializer
32+
pagination_class = GeoJsonPagination
33+
paginate_by_param = 'page_size'
3234

3335
geojson_location_list = GeojsonLocationList.as_view()
3436

0 commit comments

Comments
 (0)