From f38a70d8431bbf73f7e87c85f7ab50d8218acd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 4 Sep 2021 19:31:30 +0200 Subject: [PATCH] Do not rebuild tree of MPTT on nested creation If the MPTT is updated before calling set of fields, there is no need to rebuild the tree. issue10670 review342941002 --- trytond/model/modelsql.py | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 46389d323b..c21f21fdcf 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -635,6 +635,11 @@ class ModelSQL(ModelStorage): transaction.create_records[cls.__name__].update(new_ids) + # Update mptt before fields_to_set which could create children + if cls._mptt_fields: + field_names = list(sorted(cls._mptt_fields)) + cls._update_mptt(field_names, repeat(new_ids, len(field_names))) + translation_values = {} fields_to_set = {} for values, new_id in zip(vlist, new_ids): @@ -666,10 +671,6 @@ class ModelSQL(ModelStorage): cls._insert_history(new_ids) - if cls._mptt_fields: - field_names = list(sorted(cls._mptt_fields)) - cls._update_mptt(field_names, repeat(new_ids, len(field_names))) - cls.__check_domain_rule(new_ids, 'create') records = cls.browse(new_ids) for sub_records in grouped_slice(records, cache_size()): @@ -1463,7 +1464,6 @@ class ModelSQL(ModelStorage): @classmethod def _update_mptt(cls, field_names, list_ids, values=None): - cursor = Transaction().connection.cursor() for field_name, ids in zip(field_names, list_ids): field = cls._fields[field_name] if (values is not None @@ -1472,20 +1472,7 @@ class ModelSQL(ModelStorage): 'You can not update fields: "%s", "%s"' % (field.left, field.right)) - # Nested creation require a rebuild - # because initial values are 0 - # and thus _update_tree can not find the children - table = cls.__table__() - parent = cls.__table__() - cursor.execute(*table.join(parent, - condition=Column(table, field_name) == parent.id - ).select(table.id, - where=(Column(parent, field.left) == 0) - & (Column(parent, field.right) == 0), - limit=1)) - nested_create = cursor.fetchone() - - if not nested_create and len(ids) < 2: + if len(ids) < 2: for id_ in ids: cls._update_tree(id_, field_name, field.left, field.right)