Skip to content

Commit

Permalink
Merge pull request #74 from AchilleAsh/master
Browse files Browse the repository at this point in the history
handle natural keys in views
  • Loading branch information
leplatrem committed Feb 8, 2016
2 parents ce74407 + b927df1 commit 8306006
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions djgeojson/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion djgeojson/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 8306006

Please sign in to comment.