changeset 162c79d3b45b in trytond:default details: https://hg.tryton.org/trytond?cmd=changeset;node=162c79d3b45b description: Update translate field of all copied records
The same record can be passed multiple times in the list. So we must update all of the new records with the translated value of the fields. issue9866 review353091002 diffstat: trytond/model/modelstorage.py | 21 +++++++++++---------- 1 files changed, 11 insertions(+), 10 deletions(-) diffs (52 lines): diff -r 54c45d32566f -r 162c79d3b45b trytond/model/modelstorage.py --- a/trytond/model/modelstorage.py Wed Jan 13 00:26:53 2021 +0100 +++ b/trytond/model/modelstorage.py Mon Jan 18 23:20:15 2021 +0100 @@ -400,20 +400,18 @@ or isinstance(f, fields.MultiValue)) and n not in mptt] ids = list(map(int, records)) - datas = cls.read(ids, fields_names=fields_names) - datas = dict((d['id'], d) for d in datas) + values = {d['id']: d for d in cls.read(ids, fields_names=fields_names)} field_defs = cls.fields_get(fields_names=fields_names) to_create = [] - for id in ids: - data = convert_data(field_defs, datas[id]) + for id_ in ids: + data = convert_data(field_defs, values[id_]) to_create.append(data) new_records = cls.create(to_create) - id2new_record = dict(zip(ids, new_records)) fields_translate = {} for field_name, field in field_defs.items(): - if field_name in cls._fields and \ - getattr(cls._fields[field_name], 'translate', False): + if (field_name in cls._fields + and getattr(cls._fields[field_name], 'translate', False)): fields_translate[field_name] = field if fields_translate: @@ -421,16 +419,19 @@ ('translatable', '=', True), ]) if langs: + id2new_records = defaultdict(list) + for id_, new_record in zip(ids, new_records): + id2new_records[id_].append(new_record) fields_names = list(fields_translate.keys()) + ['id'] for lang in langs: # Prevent fuzzing translations when copying as the terms # should be the same. with Transaction().set_context(language=lang.code, fuzzy_translation=False): - datas = cls.read(ids, fields_names=fields_names) + values = cls.read(ids, fields_names=fields_names) to_write = [] - for data in datas: - to_write.append([id2new_record[data['id']]]) + for data in values: + to_write.append(id2new_records[data['id']]) to_write.append( convert_data(fields_translate, data)) cls.write(*to_write)