Change Translation.set_ids to use a list of values

review1005002
This commit is contained in:
Cédric Krier committed 2013-09-02 17:38:11 +02:00
1 parent f55fbdaa33
commit a7e7b3b91e
2 files changed
+98 -57

No files matched your search

+87 -52
View File
@@ -16,6 +16,7 @@ except ImportError:
from md5 import md5
from functools import reduce
from lxml import etree
from itertools import izip
from ..model import ModelView, ModelSQL, fields
from ..wizard import Wizard, StateView, StateTransition, StateAction, \
@@ -254,12 +255,21 @@ class Translation(ModelSQL, ModelView):
return translations
@classmethod
def set_ids(cls, name, ttype, lang, ids, value):
def set_ids(cls, name, ttype, lang, ids, values):
"Set translation for each id"
pool = Pool()
ModelFields = pool.get('ir.model.field')
Model = pool.get('ir.model')
Config = pool.get('ir.configuration')
cursor = Transaction().cursor
in_max = cursor.IN_MAX
if len(ids) > in_max:
for i in range(0, len(ids), in_max):
sub_ids = ids[i:i + in_max]
sub_values = values[i:i + in_max]
cls.set_ids(name, ttype, lang, sub_ids, sub_values)
return
model_name, field_name = name.split(',')
if model_name in ('ir.model.field', 'ir.model'):
@@ -274,74 +284,99 @@ class Translation(ModelSQL, ModelView):
ttype = 'model'
with Transaction().set_context(language='en_US'):
records = Model.browse(ids)
for record in records:
def get_name(record):
if ttype in ('field', 'help'):
name = record.model.model + ',' + record.name
return record.model.model + ',' + record.name
else:
name = record.model + ',' + field_name
translation2 = cls.search([
('lang', '=', lang),
('type', '=', ttype),
('name', '=', name),
])
with Transaction().set_user(0):
if not translation2:
cls.create([{
'name': name,
'lang': lang,
'type': ttype,
'src': getattr(record, field_name),
'value': value,
'fuzzy': False,
}])
else:
cls.write(translation2, {
return record.model + ',' + field_name
translations = {}
for translation in cls.search([
('lang', '=', lang),
('type', '=', ttype),
('name', 'in', [get_name(r) for r in records]),
]):
translations[translation.name] = translation
to_create = []
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,
})
return len(ids)
else:
with Transaction().set_user(0):
cls.write([translation], {
'src': getattr(record, field_name),
'value': value,
'fuzzy': False,
})
if to_create:
with Transaction().set_user(0):
cls.create(to_create)
return
Model = pool.get(model_name)
with Transaction().set_context(language=Config.get_language()):
records = Model.browse(ids)
for record in records:
translation2 = cls.search([
('lang', '=', lang),
('type', '=', ttype),
('name', '=', name),
('res_id', '=', record.id),
])
with Transaction().set_user(0):
if not translation2:
cls.create([{
'name': name,
'lang': lang,
'type': ttype,
'res_id': record.id,
'value': value,
'src': getattr(record, field_name),
'fuzzy': False,
}])
else:
cls.write(translation2, {
translations = {}
for translation in cls.search([
('lang', '=', lang),
('type', '=', ttype),
('name', '=', name),
('res_id', 'in', ids),
]):
translations[translation.res_id] = translation
other_translations = {}
if (lang == Config.get_language()
and Transaction().context.get('fuzzy_translation', True)):
for translation in cls.search([
('lang', '!=', lang),
('type', '=', ttype),
('name', '=', name),
('res_id', 'in', ids),
]):
other_translations.setdefault(translation.res_id, []
).append(translation)
to_create = []
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,
})
if (lang == Config.get_language()
and Transaction().context.get('fuzzy_translation',
True)):
other_langs = cls.search([
('lang', '!=', lang),
('type', '=', ttype),
('name', '=', name),
('res_id', '=', record.id),
])
else:
with Transaction().set_user(0):
cls.write([translation], {
'value': value,
'src': getattr(record, field_name),
'fuzzy': False,
})
other_langs = other_translations.get(record.id)
if other_langs:
cls.write(other_langs, {
'src': getattr(record, field_name),
'fuzzy': True,
})
return len(ids)
if to_create:
with Transaction().set_user(0):
cls.create(to_create)
@classmethod
def delete_ids(cls, model, ttype, ids):
+11 -5
View File
@@ -223,6 +223,7 @@ class ModelSQL(ModelStorage):
super(ModelSQL, cls).create(vlist)
cursor = Transaction().cursor
pool = Pool()
Translation = pool.get('ir.translation')
if cls.table_query():
return False
@@ -349,16 +350,21 @@ class ModelSQL(ModelStorage):
create_records = Transaction().create_records.setdefault(cls.__name__,
set()).update(new_ids)
translation_values = {}
for values, new_id in izip(vlist, new_ids):
for fname, value in values.iteritems():
field = cls._fields[fname]
if getattr(field, 'translate', False):
pool.get('ir.translation').set_ids(
cls.__name__ + ',' + fname, 'model',
Transaction().language, [new_id], value)
translation_values.setdefault(cls.__name__ + ',' + fname,
{})[new_id] = value
if hasattr(field, 'set'):
field.set([new_id], cls, fname, value)
if translation_values:
for name, translations in translation_values.iteritems():
Translation.set_ids(name, 'model', Transaction().language,
translations.keys(), translations.values())
if cls._history:
columns = ['"' + str(x) + '"' for x in cls._fields
if not hasattr(cls._fields[x], 'set')]
@@ -801,8 +807,8 @@ class ModelSQL(ModelStorage):
for field in direct:
if getattr(cls._fields[field], 'translate', False):
Translation.set_ids(
cls.__name__ + ',' + field, 'model',
Transaction().language, ids, values[field])
cls.__name__ + ',' + field, 'model',
Transaction().language, ids, [values[field]] * len(ids))
# call the 'set' method of fields
for field in upd_todo: