From b927df1c17c55eda8ba7cc7e4615e04b65f3e97f Mon Sep 17 00:00:00 2001 From: AchilleAsh Date: Mon, 8 Feb 2016 10:12:44 +0100 Subject: [PATCH] handle natural keys in views (+ tests) --- README.rst | 2 +- djgeojson/tests.py | 26 ++++++++++++++++++++++++-- djgeojson/views.py | 5 ++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index c446e6c..f80f2dd 100644 --- a/README.rst +++ b/README.rst @@ -109,7 +109,7 @@ Options are : * **srid** : projection (*default*: 4326, for WGS84) * **bbox** : Allows you to set your own bounding box on feature collection level * **bbox_auto** : True/False (default false). Will automatically generate a bounding box on a per feature level. - +* **use_natural_keys** : serialize natural keys instead of primary keys (*default*: ``False``) Tiled GeoJSON layer view diff --git a/djgeojson/tests.py b/djgeojson/tests.py index 1d45f00..5d3ea6f 100644 --- a/djgeojson/tests.py +++ b/djgeojson/tests.py @@ -473,8 +473,9 @@ def test_geom_field_raises_attributeerror_if_unknown(self): class ViewsTest(TestCase): def setUp(self): - self.route = Route(name='green', geom="LINESTRING (0 0, 1 1)") - self.route.save() + self.route = Route.objects.create( + name='green', geom="LINESTRING (0 0, 1 1)") + Sign(label='A', route=self.route).save() def test_view_default_options(self): view = GeoJSONLayerView(model=Route) @@ -494,6 +495,27 @@ class FullGeoJSON(GeoJSONLayerView): self.assertEqual(geojson['features'][0]['properties']['name'], 'green') + def test_view_foreign(self): + class FullGeoJSON(GeoJSONLayerView): + properties = ['label', 'route'] + view = FullGeoJSON(model=Sign) + view.object_list = [] + response = view.render_to_response(context={}) + geojson = json.loads(smart_text(response.content)) + self.assertEqual(geojson['features'][0]['properties']['route'], + 1) + + def test_view_foreign_natural(self): + class FullGeoJSON(GeoJSONLayerView): + properties = ['label', 'route'] + use_natural_keys = True + view = FullGeoJSON(model=Sign) + view.object_list = [] + response = view.render_to_response(context={}) + geojson = json.loads(smart_text(response.content)) + self.assertEqual(geojson['features'][0]['properties']['route'], + 'green') + class TileEnvelopTest(TestCase): def setUp(self): diff --git a/djgeojson/views.py b/djgeojson/views.py index 1155652..814df6c 100644 --- a/djgeojson/views.py +++ b/djgeojson/views.py @@ -46,6 +46,8 @@ class GeoJSONResponseMixin(object): """ bbox auto """ bbox_auto = False + use_natural_keys = False + def render_to_response(self, context, **response_kwargs): """ Returns a JSON response, transforming 'context' to make the payload. @@ -61,7 +63,8 @@ def render_to_response(self, context, **response_kwargs): geometry_field=self.geometry_field, force2d=self.force2d, bbox=self.bbox, - bbox_auto=self.bbox_auto) + bbox_auto=self.bbox_auto, + use_natural_keys=self.use_natural_keys) serializer.serialize(queryset, stream=response, ensure_ascii=False, **options) return response