changeset 2be0f68b7ac5 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=2be0f68b7ac5
description:
        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
diffstat:

 trytond/model/modelsql.py |  25 ++++++-------------------
 1 files changed, 6 insertions(+), 19 deletions(-)

diffs (56 lines):

diff -r daa8529c51a7 -r 2be0f68b7ac5 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Thu Sep 02 23:56:13 2021 +0200
+++ b/trytond/model/modelsql.py Sat Sep 04 19:31:30 2021 +0200
@@ -635,6 +635,11 @@
 
         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 @@
 
         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 @@
 
     @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 @@
                     '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)

Reply via email to