changeset eacddbeb7b4a in trytond:5.6
details: https://hg.tryton.org/trytond?cmd=changeset;node=eacddbeb7b4a
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
(grafted from 162c79d3b45b5a21d304427f2a85d164f4ae45da)
diffstat:
trytond/model/modelstorage.py | 21 +++++++++++----------
1 files changed, 11 insertions(+), 10 deletions(-)
diffs (52 lines):
diff -r 0a1855c67b4d -r eacddbeb7b4a trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py Thu Jan 07 10:52:08 2021 +0100
+++ b/trytond/model/modelstorage.py Mon Jan 18 23:20:15 2021 +0100
@@ -390,20 +390,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:
@@ -411,16 +409,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)