diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-20 12:37:21 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-03-20 13:04:05 +0100 |
commit | b1d246d7045e07200b21049a714027dd72060f84 (patch) | |
tree | 8549ee5ced0ff1a90eedb1d14fd450d71ac16e96 | |
parent | f3926450e8b990a8b773aa76c5d58c63fdc5758d (diff) | |
download | Ishtar-b1d246d7045e07200b21049a714027dd72060f84.tar.bz2 Ishtar-b1d246d7045e07200b21049a714027dd72060f84.zip |
✨ wizards: improve performance when many are attached
-rw-r--r-- | archaeological_operations/forms.py | 4 | ||||
-rw-r--r-- | archaeological_operations/models.py | 2 | ||||
-rw-r--r-- | archaeological_operations/wizards.py | 10 | ||||
-rw-r--r-- | ishtar_common/wizards.py | 36 |
4 files changed, 27 insertions, 25 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 72d2eaf35..6a76c4197 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -717,9 +717,7 @@ class OperationFormGeneral(CustomForm, ManageOldType): address = forms.CharField(label=_("Address / Locality"), required=False, max_length=500, widget=forms.Textarea) town = widgets.Select2MultipleField( - label=_("Towns"), - model=Town, - required=False, remote=True) + label=_("Towns"), model=Town, long_widget=True, required=False, remote=True) year = forms.IntegerField(label=_("Year"), initial=lambda: datetime.datetime.now().year, validators=[validators.MinValueValidator(1000), diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index d933944d1..fe6ed584e 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -2724,6 +2724,8 @@ def operation_town_m2m_changed(sender, **kwargs): operation = kwargs.get("instance", None) if not operation: return + if getattr(operation, "_no_m2m_process", None): + return operation._prevent_loop = False operation.regenerate_all_ids() geotown_attached_changed(sender, **kwargs) diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index 74840e552..94d6b49f9 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -87,7 +87,6 @@ class OperationWizard(Wizard): Return extra context for templates """ context = super(OperationWizard, self).get_context_data(form, **kwargs) - step = self.steps.current # reminder of the current file reminder = self.get_reminder() if reminder: @@ -198,15 +197,6 @@ class OperationModificationWizard(OperationWizard): modification = True filter_owns = {"selec-operation_modification": ["pk"]} - def get_form_kwargs(self, step, **kwargs): - kwargs = super(OperationModificationWizard, self).get_form_kwargs( - step, **kwargs - ) - if step != "relations-operation_modification": - return kwargs - kwargs["left_record"] = self.get_current_object() - return kwargs - class OperationClosingWizard(ClosingWizard): model = models.Operation diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index ddf701ce7..8308deed3 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -846,10 +846,18 @@ class Wizard(IshtarWizard): # material_index management for baseitems obj._cached_label_checked = False obj.save() + + m2m_dict = {} + for k, v in m2m: + if k not in m2m_dict: + m2m_dict[k] = [] + if not hasattr(v, "pk"): + m2m_dict[k].append(v) + continue + m2m_dict[k].append(v.pk) m2m_items = {} - # clear - # TODO! perf - to be really optimized old_m2ms = {} + obj._no_m2m_process = True for model in whole_associated_models: try: related_model = getattr(obj, model + "s") @@ -865,16 +873,20 @@ class Wizard(IshtarWizard): ).lower() if hasattr(obj, related_set_name): related_model = getattr(obj, related_set_name) - # clear real m2m - if hasattr(related_model, "clear"): - old_m2ms[model] = [] - # stock items in order to not recreate them - for old_item in related_model.all(): - old_m2ms[model].append(old_item) - related_model.clear() - else: - for r in related_model.all(): + old_m2ms[model] = [] + for r in related_model.all(): + old_m2ms[model].append(r) + if not hasattr(r, "pk"): + print(f"no pk for {model} - to be managed in wizards") + continue + if model in m2m_dict and r.pk in m2m_dict[model]: + # value kept + continue + if hasattr(related_model, "remove"): + related_model.remove(r) + else: r.delete() + obj._no_m2m_process = False need_update = False for key, value in m2m: try: @@ -943,7 +955,7 @@ class Wizard(IshtarWizard): val_empty = False if k.startswith("__empty-"): val_empty = True - k = k[len("__empty-") :] + k = k[len("__empty-"):] if not hasattr(old_item, k): continue old_val = getattr(old_item, k) |