From 3f1de491d60943b8d8f82088febe2af89a8801de Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 8 Aug 2023 12:20:07 +0200 Subject: ⚡️ optimise post-treatments: prevent unnecessary cascade update (refs #5617) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/utils.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'ishtar_common/utils.py') diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 2becd20ce..bbefd085e 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -544,7 +544,26 @@ def load_task(task_func, task_name, checks, sender, **kwargs): def cached_label_changed(sender, **kwargs): - load_task(_external_id_changed, "external_id_changed", None, sender, **kwargs) + if "instance" not in kwargs: + return + instance = kwargs["instance"] + if not instance: + return + if hasattr(instance, "external_id") and hasattr(instance, "auto_external_id") \ + and hasattr(instance, "SLUG") and not getattr(instance, "_external_id_checked", None): + changed = load_task(_external_id_changed, "external_id_changed", None, sender, **kwargs) + if changed and ( + not settings.USE_BACKGROUND_TASK + or not instance.pk + or not sender.objects.filter(pk=instance.pk).count() + ): # cached_label_changed re-triggered + return + + force_update = kwargs.get("force_update", False) + if getattr(instance, "need_update", False): + force_update = True + if not force_update and getattr(instance, "_cached_label_checked", False): + return return load_task(_cached_label_changed, "cached_label_changed", None, sender, **kwargs) @@ -627,6 +646,15 @@ def regenerate_all_cached_labels(model): def external_id_changed(sender, **kwargs): + if "instance" not in kwargs: + return + instance = kwargs["instance"] + if not instance or not hasattr(instance, "external_id") \ + or not hasattr(instance, "auto_external_id") \ + or not hasattr(instance, "SLUG"): + return + if getattr(instance, "_external_id_checked", None): + return return load_task(_external_id_changed, "external_id_changed", ["_external_id_changed"], sender, **kwargs) @@ -651,7 +679,13 @@ def _external_id_changed(sender, **kwargs): updated |= instance.regenerate_all_ids(save=False) or False instance._external_id_checked = True if updated: + if settings.USE_BACKGROUND_TASK and hasattr(instance, "no_post_process"): + instance.no_post_process() + else: + instance._no_move = True + instance.skip_history_when_saving = True instance.save() + return True def shortify(lbl, number=20): @@ -849,6 +883,13 @@ def post_save_geo(sender, **kwargs): """ Convert raw x, y, z point to real geo field """ + if "instance" not in kwargs: + return + instance = kwargs["instance"] + if not instance: + return + if getattr(instance, "_post_saved_geo", False): + return return load_task(_post_save_geo, "post_save_geo", ["_no_geo_check"], sender, **kwargs) @@ -880,8 +921,11 @@ def _post_save_geo(sender, **kwargs): modified = True if modified: - instance.skip_history_when_saving = True - instance._post_saved_geo = True + if hasattr(instance, "no_post_process"): + instance.no_post_process() + else: + instance.skip_history_when_saving = True + instance._post_saved_geo = True instance._cached_label_checked = False instance.save() if hasattr(instance, "cascade_update"): -- cgit v1.2.3