[FIX] orm: when duplicating translated object, get more coherent source and destination values if default value is specified

user's language: old (untranslated) -> new (translated)
other language: old (untranslated) -> old (translated)

This allows to have coherent behaviour if copy() method is overwritten to change the text (usually applying _('%s (copy)')). The current user will see the translated terms with modification while the translations are kept for others (and need to be updated).
We prefer keeping slightly irrelevant translations (without translated version of '%s (copy)') for other languages than losing it.
The copy_data method does not modify context to get untranslatated values, done fully in copy_translation

bzr revid: mat@openerp.com-20131125122347-5p5oyxab5rf1kk7u
This commit is contained in:
Martin Trigaux 2013-11-25 13:23:47 +01:00
commit 04908a9911
2 changed files with 14 additions and 10 deletions

View File

@ -340,10 +340,10 @@ class test_translation(common.TransactionCase):
self.new_fr_cat_id = self.res_category.copy(cr, uid, self.cat_id, default={'name': 'Clients (copie)'}, context={'lang':'fr_FR'})
no_context_cat = self.res_category.browse(cr, uid, self.new_fr_cat_id)
self.assertEqual(no_context_cat.name, 'Clients (copie)', "Duplication with default value not applied")
self.assertEqual(no_context_cat.name, 'Customers', "Duplication erased original untranslated value")
fr_context_cat = self.res_category.browse(cr, uid, self.new_fr_cat_id, context={'lang':'fr_FR'})
self.assertEqual(fr_context_cat.name, 'Clients', "Did not found translation for initial value")
self.assertEqual(fr_context_cat.name, 'Clients (copie)', "Did not used default value for translated value")
if __name__ == '__main__':

View File

@ -4930,10 +4930,7 @@ class BaseModel(object):
else:
default['state'] = self._defaults['state']
context_wo_lang = context.copy()
if 'lang' in context:
del context_wo_lang['lang']
data = self.read(cr, uid, [id,], context=context_wo_lang)
data = self.read(cr, uid, [id,], context=context)
if data:
data = data[0]
else:
@ -4994,14 +4991,16 @@ class BaseModel(object):
fields = self.fields_get(cr, uid, context=context)
for field_name, field_def in fields.items():
# removing the lang to compare untranslated values
context_wo_lang = dict(context, lang=None)
old_record, new_record = self.browse(cr, uid, [old_id, new_id], context=context_wo_lang)
# we must recursively copy the translations for o2o and o2m
if field_def['type'] == 'one2many':
target_obj = self.pool.get(field_def['relation'])
old_record, new_record = self.read(cr, uid, [old_id, new_id], [field_name], context=context)
# here we rely on the order of the ids to match the translations
# as foreseen in copy_data()
old_children = sorted(old_record[field_name])
new_children = sorted(new_record[field_name])
old_children = sorted(r.id for r in old_record[field_name])
new_children = sorted(r.id for r in new_record[field_name])
for (old_child, new_child) in zip(old_children, new_children):
target_obj.copy_translations(cr, uid, old_child, new_child, context=context)
# and for translatable fields we keep them for copy
@ -5014,7 +5013,6 @@ class BaseModel(object):
trans_name = self._inherit_fields[field_name][0] + "," + field_name
# get the id of the parent record to set the translation
inherit_field_name = self._inherit_fields[field_name][1]
old_record, new_record = self.browse(cr, uid, [old_id, new_id], context=context)
target_id = new_record[inherit_field_name].id
source_id = old_record[inherit_field_name].id
else:
@ -5024,11 +5022,17 @@ class BaseModel(object):
('name', '=', trans_name),
('res_id', '=', source_id)
])
user_lang = context.get('lang')
for record in trans_obj.read(cr, uid, trans_ids, context=context):
del record['id']
# remove source to avoid triggering _set_src
del record['source']
record.update({'res_id': target_id})
if user_lang and user_lang == record['lang']:
# 'source' to force the call to _set_src
# 'value' needed if value is changed in copy(), want to see the new_value
record['source'] = old_record[field_name]
record['value'] = new_record[field_name]
trans_obj.create(cr, uid, record, context=context)