summaryrefslogtreecommitdiff
path: root/chimere/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2016-09-20 18:08:06 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2016-09-20 18:08:06 +0200
commit5628412da85a36a655d2d5002bbfbfcb25ac7bcd (patch)
tree5fc4ddd09b16bfae9ed98a169d8245384d8a6f68 /chimere/models.py
parent90972102ac176f5cd8715cae15663f069a01f176 (diff)
downloadChimère-5628412da85a36a655d2d5002bbfbfcb25ac7bcd.tar.bz2
Chimère-5628412da85a36a655d2d5002bbfbfcb25ac7bcd.zip
Fix non aggregated routes getter - better performances for route getter
Diffstat (limited to 'chimere/models.py')
-rw-r--r--chimere/models.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/chimere/models.py b/chimere/models.py
index e99a9cf..825b4c0 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -1548,6 +1548,47 @@ class Route(GeographicItem):
dct['length'] = self.route.length
return dct
+ @classmethod
+ def getGeoJSONs(self, queryset, color="#000",
+ limit_to_categories=[]):
+ vals, default_color = [], color
+ q = queryset.select_related('categories').extra(
+ select={'json': 'ST_AsGeoJSON(route)'}).values(
+ 'json', 'name', 'pk', 'color', 'categories__pk')
+ added = []
+ current_categories = {}
+ for item in q.all():
+ if item['pk'] in added:
+ continue
+ if limit_to_categories and \
+ item["categories__pk"] not in limit_to_categories:
+ continue
+ color = default_color
+ if item["color"]:
+ color = item['color']
+ elif item["categories__pk"]:
+ if item["categories__pk"] not in current_categories:
+ cat = SubCategory.objects.get(pk=item["categories__pk"])
+ # [index, color list]
+ current_categories[item["categories__pk"]] = \
+ [0, list(Color.objects.filter(
+ color_theme=cat.color_theme))]
+ idx, colors = current_categories[item["categories__pk"]]
+ # category have a color theme
+ if colors:
+ c = colors[idx % len(colors)]
+ color = c.code
+ # index += 1
+ current_categories[item["categories__pk"]][0] += 1
+ vals.append({
+ "type": "Feature",
+ "geometry": json.loads(item['json']),
+ "properties": {"pk": item['pk'], "name": item['name'],
+ 'key': "route-{}".format(item['pk']),
+ 'color': color}})
+ added.append(item['pk'])
+ return vals
+
pre_save_route_values = {}