From f22e4e077c46932b57f6cf30ad795b80b0263e7e Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Thu, 9 Jun 2016 17:32:17 +0200 Subject: [PATCH] Fix antipatern of create/write in loop issue3516 review14931003 --- trytond/ir/action.py | 5 ++- trytond/ir/model.py | 5 ++- trytond/ir/property.py | 4 ++- trytond/ir/translation.py | 70 +++++++++++++++++---------------------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/trytond/ir/action.py b/trytond/ir/action.py index 9bfb28272c..5627a6c5c8 100644 --- a/trytond/ir/action.py +++ b/trytond/ir/action.py @@ -300,6 +300,7 @@ class ActionMixin(ModelSQL): Action = pool.get('ir.action') ir_action = cls.__table__() new_records = [] + to_write = [] for values in vlist: later = {} action_values = {} @@ -331,7 +332,9 @@ class ActionMixin(ModelSQL): transaction.connection, cls._table, action.id) record = cls(action.id) new_records.append(record) - cls.write([record], later) + to_write.extend(([record], later)) + if to_write: + cls.write(*to_write) return new_records @classmethod diff --git a/trytond/ir/model.py b/trytond/ir/model.py index d16510447c..e09904c376 100644 --- a/trytond/ir/model.py +++ b/trytond/ir/model.py @@ -934,6 +934,7 @@ class ModelData(ModelSQL, ModelView): def sync(cls, records): pool = Pool() to_write = [] + models_to_write = defaultdict(list) for data in records: Model = pool.get(data.model) values = cls.load_values(data.values) @@ -942,11 +943,13 @@ class ModelData(ModelSQL, ModelView): # if they come from version < 3.2 if values != fs_values: record = Model(data.db_id) - Model.write([record], fs_values) + models_to_write[Model].extend(([record], fs_values)) values = fs_values to_write.extend([[data], { 'values': cls.dump_values(values), }]) + for Model, values_to_write in models_to_write.iteritems(): + Model.write(*values_to_write) if to_write: cls.write(*to_write) diff --git a/trytond/ir/property.py b/trytond/ir/property.py index f4d0221cbb..ead53e92ea 100644 --- a/trytond/ir/property.py +++ b/trytond/ir/property.py @@ -166,6 +166,8 @@ class Property(ModelSQL, ModelView): raise Exception('Not implemented') if (val != default_val): + to_create = [] for res_id in ids: vals = cls._set_values(model, res_id, val, model_field.id) - cls.create([vals]) + to_create.append(vals) + cls.create(to_create) diff --git a/trytond/ir/translation.py b/trytond/ir/translation.py index e57e20f704..3fbfff70e5 100644 --- a/trytond/ir/translation.py +++ b/trytond/ir/translation.py @@ -518,28 +518,20 @@ class Translation(ModelSQL, ModelView): ]): translations[translation.name] = translation - to_create = [] + to_save = [] for record, value in izip(records, values): translation = translations.get(get_name(record)) if not translation: - to_create.append({ - 'name': name, - 'lang': lang, - 'type': ttype, - 'src': getattr(record, field_name), - 'value': value, - 'fuzzy': False, - }) - else: - with Transaction().set_context(_check_access=False): - cls.write([translation], { - 'src': getattr(record, field_name), - 'value': value, - 'fuzzy': False, - }) - if to_create: - with Transaction().set_context(_check_access=False): - cls.create(to_create) + translation = cls() + translation.name = name + translation.lang = lang + translation.type = ttype + translation.src = getattr(record, field_name) + translation.value = value + translation.fuzzy = False + to_save.append(translation) + with Transaction().set_context(_check_access=False): + cls.save(to_save) return Model = pool.get(model_name) @@ -567,19 +559,15 @@ class Translation(ModelSQL, ModelView): other_translations.setdefault(translation.res_id, [] ).append(translation) - to_create = [] + to_save = [] for record, value in izip(records, values): translation = translations.get(record.id) if not translation: - to_create.append({ - 'name': name, - 'lang': lang, - 'type': ttype, - 'res_id': record.id, - 'value': value, - 'src': getattr(record, field_name), - 'fuzzy': False, - }) + translation = cls() + translation.name = name + translation.lang = lang + translation.type = ttype + translation.res_id = record.id else: with Transaction().set_context(_check_access=False): cls.write([translation], { @@ -589,13 +577,16 @@ class Translation(ModelSQL, ModelView): }) other_langs = other_translations.get(record.id) if other_langs: - cls.write(other_langs, { - 'src': getattr(record, field_name), - 'fuzzy': True, - }) - if to_create: - with Transaction().set_context(_check_access=False): - cls.create(to_create) + for other_lang in other_langs: + other_lang.src = getattr(record, field_name) + other_lang.fuzzy = True + to_save.append(other_lang) + translation.value = value + translation.src = getattr(record, field_name) + translation.fuzzy = False + to_save.append(translation) + with Transaction().set_context(_check_access=False): + cls.save(to_save) @classmethod def delete_ids(cls, model, ttype, ids): @@ -845,7 +836,7 @@ class Translation(ModelSQL, ModelView): translation.value = new_translation.value translation.overriding_module = module translation.fuzzy = new_translation.fuzzy - translation.save() + return translation # Make a first loop to retreive translation ids in the right order to # get better read locality and a full usage of the cache. @@ -869,7 +860,8 @@ class Translation(ModelSQL, ModelView): noupdate = False if '.' in res_id: - override_translation(res_id, translation) + to_save.append(override_translation(res_id, + translation)) continue model = translation.name.split(',')[0] @@ -907,7 +899,7 @@ class Translation(ModelSQL, ModelView): to_save.append(old_translation) else: translations.add(old_translation) - cls.save(to_save) + cls.save(filter(None, to_save)) translations |= set(to_save) if translations: