diff options
| author | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-04-09 01:58:59 +0200 |
|---|---|---|
| committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-04-09 01:58:59 +0200 |
| commit | 04e654a925565f75a9efdb845de88d166bfbd76e (patch) | |
| tree | 5098c9bf08633077e7a45fc00e766a13fbdc2dda | |
| parent | 54facb137f45e9ea5fc774203c45d508184973d5 (diff) | |
| download | Chimère-04e654a925565f75a9efdb845de88d166bfbd76e.tar.bz2 Chimère-04e654a925565f75a9efdb845de88d166bfbd76e.zip | |
Improve JSON generation performance
| -rw-r--r-- | chimere/models.py | 20 | ||||
| -rw-r--r-- | chimere/views.py | 33 |
2 files changed, 48 insertions, 5 deletions
diff --git a/chimere/models.py b/chimere/models.py index c269439..7db530a 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -624,6 +624,19 @@ class Marker(GeographicItem): val = values[unicode(propertymodel.id)] self.setProperty(propertymodel, val) + def _getItems(self, base_dct={"properties":{}}): + '''Return a dict representation for json + ''' + item = base_dct + item["geometry"] = {"type": "Point", + "coordinates": [ self.point.x, self.point.y ] + } + item["properties"]['pk'] = self.pk + item["properties"]['name'] = self.name + if self.weight: + item["properties"]['weight'] = self.weight + return item + def getGeoJSON(self, categories_id=[]): '''Return a GeoJSON string ''' @@ -1117,6 +1130,13 @@ class Route(GeographicItem): properties.append(property) return properties + def _getItems(self, dct={'properties':{}}): + dct['geometry'] = { "type": "LineString", + "coordinates": [[point.x, point.y] + for point in self.route]} + dct['properties'].update({'id':self.id, 'name':self.name}) + return dct + def getGeoJSON(self, color="#000"): '''Return a GeoJSON string ''' diff --git a/chimere/views.py b/chimere/views.py index 7c02216..79d4ac2 100644 --- a/chimere/views.py +++ b/chimere/views.py @@ -604,10 +604,17 @@ def getGeoObjects(request, area_name, category_ids, status): idx = 0 current_cat = c_cat colors = list(Color.objects.filter(color_theme = c_cat.color_theme)) + color = '000' if colors: - jsons.append(route.getGeoJSON(color=colors[idx % len(colors)].code)) - else: - jsons.append(route.getGeoJSON(color='000')) + color = colors[idx % len(colors)].code + if '#' not in color: + color = '#' + color + base_dct = {"type":"Feature", + "properties":{ + "color":color + } + } + jsons.append(route._getItems(base_dct)) idx += 1 try: q = checkDate(Q(status__in=status, categories__in=category_ids)) @@ -615,10 +622,26 @@ def getGeoObjects(request, area_name, category_ids, status): except: return HttpResponse('no results') category_ids = [int(cat_id) for cat_id in category_ids] - jsons += [geo_object.getGeoJSON(category_ids) for geo_object in list(query)] + for category_id in category_ids: + cat = SubCategory.objects.get(pk=category_id) + base_dct = {"type":"Feature", + "properties":{ + "icon_path":unicode(cat.icon.image), + "icon_hover_path":cat.hover_icon.image \ + if cat.hover_icon else '', + "icon_width":cat.icon.image.width, + 'icon_height':cat.icon.image.height, + 'category_name':cat.name} + } + for obj in query.filter(categories__pk=category_id).all(): + dct = base_dct.copy() + dct['properties'] = base_dct['properties'].copy() + jsons.append(obj._getItems(base_dct=dct)) if not jsons: return HttpResponse('no results') - data = '{"type": "FeatureCollection", "features":[%s]}' % ",".join(jsons) + data = {"type": "FeatureCollection", "features":jsons} + data = json.dumps(data) + return HttpResponse(data) def get_all_categories(request, area_name=None): |
