Skip to content

Commit

Permalink
Fix serializing property in upper class
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jul 10, 2013
1 parent 82cfe4f commit 66093d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CHANGELOG
==================

- Support empty geometries (None or NULL in Db)

- Fix serializing property in upper class

2.0.1 (2013-07-10)
==================
Expand Down
2 changes: 1 addition & 1 deletion djgeojson/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
def hasattr_lazy(obj, name):
if isinstance(obj, dict):
return name in obj
return any(name in d for d in (obj.__dict__, obj.__class__.__dict__))
return name in dir(obj)


class DjangoGeoJSONEncoder(DjangoJSONEncoder):
Expand Down
20 changes: 10 additions & 10 deletions djgeojson/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
settings.SERIALIZATION_MODULES = {'geojson': 'djgeojson.serializers'}


class Route(models.Model):
class PictureMixin(object):
@property
def picture(self):
return 'image.png'


class Route(PictureMixin, models.Model):
name = models.CharField(max_length=20)
geom = models.LineStringField(spatial_index=False, srid=4326)

Expand Down Expand Up @@ -72,18 +78,12 @@ def test_basic(self):
Route(name='blue', geom="LINESTRING (0 0, 1 1)").save()
Route(name='red', geom="LINESTRING (0 0, 1 1)").save()

# Expected output
expect_geojson = """{"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "green"}, "id": 1}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "blue"}, "id": 2}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "red"}, "id": 3}]}"""
expect_geojson_with_prop = """{"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "upper_name": "GREEN", "name": "green"}, "id": 1}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "upper_name": "BLUE", "name": "blue"}, "id": 2}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "upper_name": "RED", "name": "red"}, "id": 3}]}"""
# Do the serialization
actual_geojson = serializers.serialize('geojson', Route.objects.all(),
properties=['name'])
self.assertEqual(actual_geojson, '{"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "green"}, "id": 1}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "blue"}, "id": 2}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "red"}, "id": 3}]}')
actual_geojson_with_prop = serializers.serialize('geojson', Route.objects.all(),
properties=['name', 'upper_name'])

# Did it work?
self.assertEqual(actual_geojson, expect_geojson)
self.assertEqual(actual_geojson_with_prop, expect_geojson_with_prop)
properties=['name', 'upper_name', 'picture'])
self.assertEqual(actual_geojson_with_prop, '{"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"picture": "image.png", "model": "djgeojson.route", "upper_name": "GREEN", "name": "green"}, "id": 1}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"picture": "image.png", "model": "djgeojson.route", "upper_name": "BLUE", "name": "blue"}, "id": 2}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"picture": "image.png", "model": "djgeojson.route", "upper_name": "RED", "name": "red"}, "id": 3}]}')

def test_precision(self):
serializer = Serializer()
Expand Down

0 comments on commit 66093d7

Please sign in to comment.