diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-03-11 10:47:30 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-03-11 10:47:30 +0100 |
commit | b054242c22970c42294dddda13ce9757b68147e7 (patch) | |
tree | 4b66655346f3ba976feca5ffaa97f9ce8d8fbffc /chimere/models.py | |
parent | 1f3356d52cd1fe9856156de101c135b19d24b65b (diff) | |
download | Chimère-b054242c22970c42294dddda13ce9757b68147e7.tar.bz2 Chimère-b054242c22970c42294dddda13ce9757b68147e7.zip |
Fetch other category on JSON request
Diffstat (limited to 'chimere/models.py')
-rw-r--r-- | chimere/models.py | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/chimere/models.py b/chimere/models.py index d00d0b6..27156bd 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -906,18 +906,30 @@ class GeographicItem(models.Model): select={'json': 'ST_AsGeoJSON({})'.format(cls.geom_attr)} ).values(*q_values) - vals, added, current_categories, icons = [], [], {}, {} + vals, current_categories, icons = {}, {}, {} start, end = 0, None if slice: start, end = slice has_next = False for item in q.all(): - if item['pk'] in added: + if item['pk'] in vals: + # multi-category for this item + value = vals[item['pk']] + icon, icons = cls._json_get_icons(item, current_categories) + colors, current_categories = \ + cls._json_get_colors(item, default_values, + current_categories) + if not icon and not colors: + # something went wrong when fetching icon / colors + continue + if icon not in value['properties']['extra_icons']: + value['properties']['extra_icons'].append(icon) + if colors not in value['properties']['extra_colors']: + value['properties']['extra_colors'].append(colors) continue if limit_to_categories and \ item["categories__pk"] not in limit_to_categories: continue - added.append(item['pk']) if start: start -= 1 end -= 1 @@ -926,7 +938,8 @@ class GeographicItem(models.Model): has_next = True break properties = {"pk": item['pk'], "name": item['name'], - 'key': "{}-{}".format(cls.geom_attr, item['pk'])} + 'key': "{}-{}".format(cls.geom_attr, item['pk']), + "extra_icons": [], "extra_colors": []} icon, icons = cls._json_get_icons(item, current_categories) colors, current_categories = \ cls._json_get_colors(item, default_values, current_categories) @@ -941,14 +954,14 @@ class GeographicItem(models.Model): except json.decoder.JSONDecodeError: continue - vals.append({ + vals[item['pk']] = { "type": "Feature", "geometry": geom, "properties": properties - }) + } if not check_next: - return vals - return vals, has_next + return vals.values() + return vals.values(), has_next @classmethod def search(cls, query, area=None, get_json=False, slice=None, |