diff options
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r-- | ishtar_common/utils.py | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 3d9cfc13f..828a03da3 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -364,14 +364,17 @@ def cached_label_changed(sender, **kwargs): if not kwargs.get('instance'): return instance = kwargs.get('instance') - force_update = kwargs.get('force_update', False) if hasattr(instance, 'test_obj'): instance.test_obj.reached(sender, **kwargs) - if not force_update and hasattr(instance, '_cached_label_checked') \ - and instance._cached_label_checked: + cache_key, value = get_cache( + sender, ["cached_label_changed", kwargs['instance'].pk] + ) + if value: # multiple request too quick return + cache.set(cache_key, True, settings.CACHE_TASK_TIMEOUT) + if not settings.USE_BACKGROUND_TASK: return _cached_label_changed(sender, **kwargs) @@ -389,6 +392,10 @@ def _cached_label_changed(sender, **kwargs): EXTRA_KWARGS_TRIGGER) if not instance: return + force_update = kwargs.get('force_update', False) + + if not force_update and getattr(instance, '_cached_label_checked', False): + return force_update = kwargs.get('force_update', False) @@ -399,17 +406,16 @@ def _cached_label_changed(sender, **kwargs): cached_labels = ['cached_label'] if hasattr(sender, 'CACHED_LABELS'): cached_labels = sender.CACHED_LABELS - changed = False + changed = [] for cached_label in cached_labels: lbl = getattr(instance, '_generate_' + cached_label)() if lbl != getattr(instance, cached_label): - setattr(instance, cached_label, lbl) - changed = True + changed.append((cached_label, lbl)) if changed: instance._search_updated = False if hasattr(instance, '_cascade_change') and instance._cascade_change: instance.skip_history_when_saving = True - instance.save() + sender.objects.filter(pk=instance.pk).update(**dict(changed)) updated = False if force_update or hasattr(instance, 'update_search_vector'): updated = instance.update_search_vector() @@ -421,6 +427,10 @@ def _cached_label_changed(sender, **kwargs): if hasattr(instance, 'test_obj'): item.test_obj = instance.test_obj cached_label_changed(item.__class__, instance=item) + cache_key, __ = get_cache( + sender, ["cached_label_changed", instance.pk] + ) + cache.set(cache_key, None, settings.CACHE_TASK_TIMEOUT) def regenerate_all_cached_labels(model): @@ -570,9 +580,13 @@ def post_save_geo(sender, **kwargs): """ Convert raw x, y, z point to real geo field """ - if not kwargs.get('instance') or getattr(kwargs['instance'], - '_post_saved_geo', False): + if not kwargs.get('instance'): return + cache_key, value = get_cache( + sender, ["post_save_geo", kwargs['instance'].pk]) + if value: # multiple request too quick + return + cache.set(cache_key, True, settings.CACHE_TASK_TIMEOUT) if not settings.USE_BACKGROUND_TASK: return _post_save_geo(sender, **kwargs) @@ -603,6 +617,9 @@ def _post_save_geo(sender, **kwargs): or "Warehouse" in kls_name): return + if getattr(instance, '_post_saved_geo', False): + return + # print(sender, "post_save_geo") current_source = "default" @@ -621,7 +638,6 @@ def _post_save_geo(sender, **kwargs): # should be a db source instance.multi_polygon_source = 'P' instance.multi_polygon_source_item = current_source - modified = True elif instance.multi_polygon_source != 'P': precise_poly = instance.get_precise_polygons() if precise_poly: @@ -755,7 +771,12 @@ def _post_save_geo(sender, **kwargs): if modified: instance.skip_history_when_saving = True instance._post_saved_geo = True + instance._cached_label_checked = False instance.save() + cache_key, __ = get_cache( + sender, ["post_save_geo", instance.pk] + ) + cache.set(cache_key, None, settings.CACHE_TASK_TIMEOUT) return |