Files
odoo_source/addons/website_gengo/controllers/main.py
T
Martin Trigaux 66ef641f33 [REF] base: remove field source of ir.translation
Instead, make sure the field src is always up to date
Add tests

Changes in _write:
- Replace _set_ids (to be deprecated) call by _upsert_translations as it works
  in batch
- Call _upsert_translations for any language, including en_US
  In case an English translation already existed for a record, only the master
  value (on the reference record) was updated but the user kept seeing the
  translation value (was revealed 73a7534bfc).
- Read src_trans without language
  Similar as above, if an English translation already existed, the translation
  was used for the result of the read and not the new value that has just been
  inserted into the database
- Add _set_source method
  When updating a master record of a translated field, the src field must be
  updated, including in different languages.
  Before it was ok that the src field was out of date as the source was computed

Changes in copy_translation:
- set src as the new value without lang
  update the comment to reflect reality since 489494e733
  src will contain the English version if no changes were made and it will
  contain the modified value if copy was overriden

Changes in upsert_translations:
- Do not force a module, comment, state
  Only src, res_id, name, value and lang must be given. Optional values will no
  longer be set to null if not given
2019-08-01 09:06:25 +00:00

64 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import http
from odoo.http import request
GENGO_DEFAULT_LIMIT = 20
class WebsiteGengo(http.Controller):
@http.route('/website/get_translated_length', type='json', auth='user', website=True)
def get_translated_length(self, translated_ids, lang):
result = {"done": 0}
gengo_translation_ids = request.env['ir.translation'].search([('id', 'in', translated_ids), ('gengo_translation', '!=', False)])
for trans in gengo_translation_ids:
result['done'] += len(trans.src.split())
return result
@http.route('/website/check_gengo_set', type='json', auth='user', website=True)
def check_gengo_set(self):
company = request.env.user.sudo().company_id
company_flag = 0
if not company.gengo_public_key or not company.gengo_private_key:
company_flag = company.id
return company_flag
@http.route('/website/set_gengo_config', type='json', auth='user', website=True)
def set_gengo_config(self, config):
request.env.company.write(config)
return True
@http.route('/website/post_gengo_jobs', type='json', auth='user', website=True)
def post_gengo_jobs(self):
request.env['base.gengo.translations']._sync_request(limit=GENGO_DEFAULT_LIMIT)
return True
@http.route('/website_gengo/set_translations', type='json', auth='user', website=True)
def set_translations(self, data, lang):
IrTranslation = request.env['ir.translation']
for term in data:
initial_content = term['initial_content'].strip()
translation_ids = term['translation_id']
if not translation_ids:
translations = IrTranslation.search_read([('lang', '=', lang), ('src', '=', initial_content)], fields=['id'])
if translations:
translation_ids = [t_id['id'] for t_id in translations]
vals = {
'gengo_comment': term['gengo_comment'],
'gengo_translation': term['gengo_translation'],
'state': 'to_translate',
}
if translation_ids:
IrTranslation.browse(translation_ids).write(vals)
else:
vals.update({
'name': 'website',
'lang': lang,
'source': initial_content,
})
IrTranslation.create(vals)
return True