summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-10-01 12:50:19 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-10-01 12:50:19 +0200
commit52b2e992917ce313ae9d696d7901a24fad755bce (patch)
tree5dd6daa2584916cdc4990d3423ecda0ff311e420
parenta2e517d1a8d373fe997c22a4e1fdd764f6f54208 (diff)
downloadChimère-52b2e992917ce313ae9d696d7901a24fad755bce.tar.bz2
Chimère-52b2e992917ce313ae9d696d7901a24fad755bce.zip
Search: display icon for search result
-rw-r--r--chimere/models.py100
1 files changed, 55 insertions, 45 deletions
diff --git a/chimere/models.py b/chimere/models.py
index 451596b..45dedfb 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -847,8 +847,43 @@ class GeographicItem(models.Model):
(float((max_weight - min_weight)) or 1), 5)
@classmethod
- def _json_get_icon_and_colors(cls, item, default_values,
- current_categories):
+ def _json_get_icons(cls, item, categories_icons):
+ """
+ Utility function for getGeoJSONs.
+ Update icons.
+
+ :param item: queried item
+ :param categories_icons: dict containing categories and how many items
+ these category have been reached (for color rotation)
+ :return: tuple of new properties and updated current icon dict
+ """
+ if item['categories__pk'] not in categories_icons:
+ try:
+ cat = SubCategory.objects.get(
+ available=True, pk=item['categories__pk'])
+ except SubCategory.DoesNotExist:
+ return None, categories_icons
+ categories_icons[item['categories__pk']] = {
+ 'icon_path': str(cat.icon.image),
+ 'icon_hover_path': str(cat.hover_icon.image)
+ if cat.hover_icon else '',
+ 'icon_offset_x': cat.icon.offset_x,
+ 'icon_offset_y': cat.icon.offset_y,
+ 'icon_popup_offset_x': cat.icon.popup_offset_x,
+ 'icon_popup_offset_y': cat.icon.popup_offset_y,
+ 'category_name': cat.name}
+ try:
+ categories_icons[item['categories__pk']].update(
+ {'icon_width': cat.icon.image.width,
+ 'icon_height': cat.icon.image.height,
+ })
+ except IOError:
+ # image is not available
+ pass
+ return categories_icons[item['categories__pk']], categories_icons
+
+ @classmethod
+ def _json_get_colors(cls, item, default_values, current_categories):
"""
Utility function for getGeoJSONs specific for each geometry type.
Update colors and icons.
@@ -859,7 +894,7 @@ class GeographicItem(models.Model):
these category have been reached (for color rotation)
:return: tuple of new properties and updated current categories dict
"""
- raise NotImplementedError()
+ return {}, current_categories
@classmethod
def getGeoJSONs(cls, queryset, limit_to_categories=None, slice=None,
@@ -871,7 +906,7 @@ class GeographicItem(models.Model):
select={'json': 'ST_AsGeoJSON({})'.format(cls.geom_attr)}
).values(*q_values)
- vals, added, current_categories = [], [], {}
+ vals, added, current_categories, icons = [], [], {}, {}
start, end = 0, None
if slice:
start, end = slice
@@ -892,13 +927,14 @@ class GeographicItem(models.Model):
break
properties = {"pk": item['pk'], "name": item['name'],
'key': "{}-{}".format(cls.geom_attr, item['pk'])}
- icon_and_colors, current_categories = \
- cls._json_get_icon_and_colors(item, default_values,
- current_categories)
- if not icon_and_colors:
+ 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
- properties.update(icon_and_colors)
+ properties.update(icon)
+ properties.update(colors)
try:
# bad geometry are not displayed
geom = json.loads(item['json'])
@@ -1117,34 +1153,6 @@ class Marker(GeographicItem):
return json.dumps(jsons)
- @classmethod
- def _json_get_icon_and_colors(cls, item, default_values,
- current_categories):
- if item['categories__pk'] not in current_categories:
- try:
- cat = SubCategory.objects.get(
- available=True, pk=item['categories__pk'])
- except SubCategory.DoesNotExist:
- return None, current_categories
- current_categories[item['categories__pk']] = {
- 'icon_path': str(cat.icon.image),
- 'icon_hover_path': str(cat.hover_icon.image)
- if cat.hover_icon else '',
- 'icon_offset_x': cat.icon.offset_x,
- 'icon_offset_y': cat.icon.offset_y,
- 'icon_popup_offset_x': cat.icon.popup_offset_x,
- 'icon_popup_offset_y': cat.icon.popup_offset_y,
- 'category_name': cat.name}
- try:
- current_categories[item['categories__pk']].update(
- {'icon_width': cat.icon.image.width,
- 'icon_height': cat.icon.image.height,
- })
- except IOError:
- # image is not available
- pass
- return current_categories[item['categories__pk']], current_categories
-
@property
def default_category(self):
# Should we select only available ones ?
@@ -1283,20 +1291,22 @@ class Polygon(GeographicItem):
return json.dumps(attributes)
@classmethod
- def _json_get_icon_and_colors(cls, item, default_values,
- current_categories):
+ def _json_get_colors(cls, item, default_values, current_categories):
inner_color = None
color = default_values['color']
+ json_decoder = json.JSONDecoder()
if item["color"]:
color = item['color']
elif item["categories__pk"]:
- if item["categories__pk"] not in current_categories:
+ key = "{}-{}".format(json_decoder.decode(item["json"])["type"],
+ item["categories__pk"])
+ if key not in current_categories:
cat = SubCategory.objects.get(pk=item["categories__pk"])
# [index, color list]
- current_categories[item["categories__pk"]] = \
+ current_categories[key] = \
[0, list(Color.objects.filter(
color_theme=cat.color_theme))]
- idx, colors = current_categories[item["categories__pk"]]
+ idx, colors = current_categories[key]
# category have a color theme
if colors:
c = colors[idx % len(colors)]
@@ -1304,7 +1314,7 @@ class Polygon(GeographicItem):
if c.inner_code:
inner_color = c.inner_code
# index += 1
- current_categories[item["categories__pk"]][0] += 1
+ current_categories[key][0] += 1
if item["inner_color"]:
inner_color = item["inner_color"]
elif not inner_color:
@@ -1731,8 +1741,7 @@ class Route(GeographicItem):
return dct
@classmethod
- def _json_get_icon_and_colors(cls, item, default_values,
- current_categories):
+ def _json_get_colors(cls, item, default_values, current_categories):
color = default_values['color']
if item["color"]:
color = item['color']
@@ -1752,6 +1761,7 @@ class Route(GeographicItem):
current_categories[item["categories__pk"]][0] += 1
return {"color": color}, current_categories
+
pre_save_route_values = {}