diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-02-10 19:44:05 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:21:00 +0100 |
commit | 39551b72a41b5ad00f8d200db2b49e4f3e02515f (patch) | |
tree | 10428f79f2c1f1f4e92ddaf3ba0f7d7efedb642c /ishtar_common/utils.py | |
parent | f6563b7ddb44b180edd45c03bd4cf0fb6d48b331 (diff) | |
download | Ishtar-39551b72a41b5ad00f8d200db2b49e4f3e02515f.tar.bz2 Ishtar-39551b72a41b5ad00f8d200db2b49e4f3e02515f.zip |
Geodata redesign: new model
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r-- | ishtar_common/utils.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 80d5af9d6..eab25a56f 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -740,6 +740,66 @@ def get_srid_obj_from_point(point): ) +def post_save_geodata(sender, **kwargs): + instance = kwargs.get("instance", None) + if not instance: + return + if hasattr(instance, "_no_geo_check") and instance._no_geo_check: + return + if not settings.USE_BACKGROUND_TASK: + return _post_save_geodata(sender, **kwargs) + sender, kwargs = serialize_args_for_tasks( + sender, instance, kwargs, EXTRA_KWARGS_TRIGGER + ) + task_item = _post_save_geodata.delay(sender, **kwargs) + revoke_old_task(kwargs, "post_save_geodata", task_item.id, instance.__class__) + return task_item + + +@task() +def _post_save_geodata(sender, **kwargs): + """ + Save cached_x, cached_y, cached_z using display srid + """ + sender, instance = deserialize_args_for_tasks(sender, kwargs, EXTRA_KWARGS_TRIGGER) + if not instance: + return + + if getattr(instance, "_post_saved_geo", False): + return + + modified = False + + cached_x, cached_y, cached_z = None, None, None + coords = instance.display_coordinates(rounded=False, dim=3) + if coords: + cached_x, cached_y, cached_z = coords + else: + coords = instance.display_coordinates(rounded=False, dim=2) + if not coords: + coords = instance.get_coordinates_from_polygon(rounded=False) + if coords: + cached_x, cached_y = coords + + if instance.cached_x != cached_x or instance.cached_y != cached_y \ + or instance.cached_z != cached_z: + modified = True + instance.cached_x = cached_x + instance.cached_y = cached_y + instance.cached_z = cached_z + + if hasattr(instance, "need_update") and instance.need_update: + instance.need_update = False + modified = True + + if modified: + instance._post_saved_geo = True + instance.save() + cache_key, __ = get_cache(sender, ("post_save_geo", instance.pk)) + cache.set(cache_key, None, settings.CACHE_TASK_TIMEOUT) + return + + def post_save_geo(sender, **kwargs): """ Convert raw x, y, z point to real geo field |