From 2ffea8264374e56f543dc34e318d113fec41397d Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 3 Aug 2016 16:08:44 +0200 Subject: JSON: prevent crash when geometry is not valid --- chimere/models.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/chimere/models.py b/chimere/models.py index 122d61f..0a2906c 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -706,7 +706,10 @@ class Marker(GeographicItem): if categories_id and cat.id not in categories_id: continue items = copy.deepcopy(json_tpl) - items['geometry'] = json.loads(self.point.geojson) + try: + items['geometry'] = json.loads(self.point.geojson) + except json.JSONDecodeError: + continue items['properties'].update({ 'pk':self.id, 'name':self.name, @@ -1190,8 +1193,12 @@ class Route(GeographicItem): ''' if '#' not in color: color = '#' + color + try: + geom = json.loads(self.route.geojson) + except json.JSONDecodeError: + return json.dumps('{}') attributes = {"type":"Feature", - "geometry":json.loads(self.route.geojson), + "geometry": geom, "properties":{"pk":self.id, "name":self.name, "color":color}} return json.dumps(attributes) @@ -1289,7 +1296,11 @@ class AggregatedRoute(models.Model): ''' if '#' not in color: color = '#' + color - attributes = {'color':color, 'geometry':json.loads(self.route.geojson), + try: + geom = json.loads(self.route.geojson) + except json.JSONDecodeError: + return json.dumps('{}') + attributes = {'color':color, 'geometry': geom, 'type':"Feature", "properties":{"pk": self.id, "name": u'Aggregated route',}} return json.dumps(attributes) -- cgit v1.2.3