[IMP]: Improve send and receive queue
bzr revid: aja@tinyerp.com-20120824132125-a74lw82hm77al1zh
This commit is contained in:
parent
bc0b3c6cc5
commit
fedd324e6d
|
@ -25,17 +25,18 @@ import logging
|
|||
_logger = logging.getLogger(__name__)
|
||||
|
||||
cron_vals = {
|
||||
'name': 'Gengo Sync',
|
||||
'active': True,
|
||||
'interval_number': 30,
|
||||
'interval_type': 'minutes',
|
||||
'numbercall': -1,
|
||||
'model': "'base.update.translations'",
|
||||
'function': ""
|
||||
'name': 'Gengo Sync',
|
||||
'active': True,
|
||||
'interval_number': 30,
|
||||
'interval_type': 'minutes',
|
||||
'numbercall': -1,
|
||||
'model': "'base.update.translations'",
|
||||
'function': ""
|
||||
}
|
||||
|
||||
|
||||
class base_update_translation(osv.osv_memory):
|
||||
|
||||
def _update_terms(self, cr, uid, ids, response, tier, context):
|
||||
translation_pool = self.pool.get('ir.translation')
|
||||
for jobs in response['jobs']:
|
||||
|
@ -48,25 +49,30 @@ class base_update_translation(osv.osv_memory):
|
|||
translation_pool.write(cr, uid, [t_id], vals, context)
|
||||
return
|
||||
|
||||
|
||||
def _send_translation_terms(self, cr, uid, ids, trg_lang, term_ids, context):
|
||||
def _send_translation_terms(self, cr, uid, ids, term_ids, context):
|
||||
"""
|
||||
Lazy Polling will be perform when user or cron request for the trnalstion.
|
||||
"""
|
||||
meta = self.pool.get('jobs.meta')
|
||||
user = self.pool.get('res.users').browse(cr, uid, uid, context)
|
||||
gengo = meta.gengo_authentication(cr, uid, ids, context)
|
||||
request = meta.pack_jobs_request(cr, uid, term_ids, trg_lang, context)
|
||||
result = gengo.postTranslationJobs(jobs=request)
|
||||
if result['opstat'] == 'ok':
|
||||
self._update_terms(cr, uid, ids, result['response'], user.company_id.gengo_tier, context)
|
||||
flag, gengo = meta.gengo_authentication(cr, uid, context)
|
||||
if flag:
|
||||
|
||||
request = meta.pack_jobs_request(cr, uid, term_ids, context)
|
||||
if request:
|
||||
result = gengo.postTranslationJobs(jobs=request)
|
||||
if result['opstat'] == 'ok':
|
||||
self._update_terms(cr, uid, ids, result['response'], user.company_id.gengo_tier, context)
|
||||
else:
|
||||
_logger.error(gengo)
|
||||
return True
|
||||
|
||||
def do_check_schedular(self, cr, uid, xml_id, name, fn, context=None):
|
||||
cron_pool = self.pool.get('ir.cron')
|
||||
try:
|
||||
res = self.pool.get('ir.model.data')._get_id(cr, uid, 'base_gengo', xml_id)
|
||||
cron_pool.write(cr, uid, [res], {'active': True}, context)
|
||||
res = []
|
||||
model, res = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base_gengo', xml_id)
|
||||
cron_pool.write(cr, uid, [res], {'active': True}, context=context)
|
||||
except:
|
||||
cron_vals.update({'name': name, "function": fn})
|
||||
return cron_pool.create(cr, uid, cron_vals, context)
|
||||
|
@ -75,46 +81,77 @@ class base_update_translation(osv.osv_memory):
|
|||
if context == None:
|
||||
context = {}
|
||||
lang_pool = self.pool.get('res.lang')
|
||||
for res in self.browse(cr, uid, ids, context):
|
||||
lang_id = lang_pool.search(cr, uid, [('code', '=', res.lang)])
|
||||
lang_pool.write(cr, uid, lang_id, {'gengo_sync': True})
|
||||
res = super(base_update_translation, self).act_update(cr, uid, ids, context)
|
||||
self.do_check_schedular(cr, uid, 'gengo_sync_send_request_scheduler', 'gengo_sync_send_request_scheduler', '_sync_request', context)
|
||||
self.do_check_schedular(cr, uid, 'gengo_sync_receive_request_scheduler', 'gengo_sync_send_request_scheduler', '_sync_response', context)
|
||||
return {}
|
||||
super(base_update_translation, self).act_update(cr, uid, ids, context)
|
||||
msg = "1. Translation file loaded succesfully.\n2. Processing Gengo Translation:\n"
|
||||
meta = self.pool.get('jobs.meta')
|
||||
flag, gengo = meta.gengo_authentication(cr, uid, context)
|
||||
if not flag:
|
||||
msg += gengo
|
||||
else:
|
||||
for res in self.browse(cr, uid, ids, context):
|
||||
lang_id = lang_pool.search(cr, uid, [('code', '=', res.lang)])
|
||||
lang_search = lang_pool.search(cr, uid, [('gengo_sync', '=', True),
|
||||
('id', '=', lang_id[0])])
|
||||
if lang_search:
|
||||
msg += 'This language %s is alreay in queue for processing.' % (res.lang)
|
||||
else:
|
||||
msg += "Translation for language %s is queued for processing." % (res.lang)
|
||||
lang_pool.write(cr, uid, lang_id, {'gengo_sync': True})
|
||||
_logger.info("Your translation request for language '%s' has been send sucessfully.", res.lang)
|
||||
|
||||
def _sync_response(self, cr, uid, ids=0, context=None):
|
||||
self.do_check_schedular(cr, uid, 'gengo_sync_send_request_scheduler', 'Gengo Sync Translation (Request)', '_sync_request', context)
|
||||
self.do_check_schedular(cr, uid, 'gengo_sync_receive_request_scheduler', 'Gengo Sync Translation (Response)', '_sync_response', context)
|
||||
self._sync_request(cr, uid, ids, context)
|
||||
context.update({'message': msg})
|
||||
obj_model = self.pool.get('ir.model.data')
|
||||
model_data_ids = obj_model.search(cr, uid, [('model', '=', 'ir.ui.view'), ('name', '=', 'update_translation_wizard_view_confirm')])
|
||||
view_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
|
||||
return {
|
||||
'view_type': 'form',
|
||||
'view_mode': 'form',
|
||||
'res_model': 'gengo.update.message',
|
||||
'views': [(view_id, 'form')],
|
||||
'type': 'ir.actions.act_window',
|
||||
'target': 'new',
|
||||
'context': context,
|
||||
}
|
||||
|
||||
def _sync_response(self, cr, uid, ids=False, context=None):
|
||||
"""Scheduler will be call to get response from gengo and all term will get
|
||||
by scheduler which terms are in approved state"""
|
||||
meta = self.pool.get('jobs.meta')
|
||||
translation_pool = self.pool.get('ir.translation')
|
||||
gengo = meta.gengo_authentication(cr, uid, ids, context)
|
||||
translation_id = translation_pool.search(cr, uid, [('job_id', '!=', False), ('state', '=', 'inprogress'), ('gengo_translation', '=', True)], limit=local.LIMIT, context=context)
|
||||
for trns in translation_pool.browse(cr, uid, translation_id, context):
|
||||
job_response = gengo.getTranslationJob(id=trns.job_id)
|
||||
if job_response['response']['job']['status'] == 'approved':
|
||||
translation_pool.write(cr, uid, translation_id, {'value': job_response['response']['job']['body_tgt'], 'state': 'translated', 'gengo_control': True})
|
||||
flag, gengo = meta.gengo_authentication(cr, uid, context)
|
||||
if not flag:
|
||||
_logger.warning("%s", gengo)
|
||||
else:
|
||||
translation_id = translation_pool.search(cr, uid, [('state', '=', 'inprogress'), ('gengo_translation', '=', True)], limit=local.LIMIT, context=context)
|
||||
for trns in translation_pool.browse(cr, uid, translation_id, context):
|
||||
if trns.job_id:
|
||||
job_response = gengo.getTranslationJob(id=trns.job_id)
|
||||
if job_response['response']['job']['status'] == 'approved':
|
||||
translation_pool.write(cr, uid, trns.id, {'value': job_response['response']['job']['body_tgt'], 'state': 'translated', 'gengo_control': True})
|
||||
return True
|
||||
|
||||
def _sync_request(self, cr, uid, ids=0, context=None):
|
||||
def _sync_request(self, cr, uid, ids=False, context=None):
|
||||
"""This scheduler will send a job request to the gengo , which terms are
|
||||
in translate state and gengo_translation is true"""
|
||||
if context is None:
|
||||
context = {}
|
||||
language_pool = self.pool.get('res.lang')
|
||||
translation_pool = self.pool.get('ir.translation')
|
||||
try:
|
||||
language_pool = self.pool.get('res.lang')
|
||||
trg_lang = self.browse(cr, uid, ids)[0]
|
||||
translation_pool = self.pool.get('ir.translation')
|
||||
lang_search_id = language_pool.search(cr, uid, [('gengo_sync', '=', True), ('code', '=', trg_lang.lang)])
|
||||
if not lang_search_id:
|
||||
term_ids = translation_pool.search(cr, uid, [('state', '=', 'inprogress'), ('gengo_translation', '=', 'True'), ('lang', '=', trg_lang.lang)], limit=local.LIMIT)
|
||||
self._send_translation_terms(cr, uid, ids, trg_lang.lang, term_ids, context)
|
||||
msg = "Your Request has been Successfully Send to Gengo"
|
||||
lang_ids = language_pool.search(cr, uid, [('gengo_sync', '=', True)])
|
||||
langs = [lang.code for lang in language_pool.browse(cr, uid, lang_ids)]
|
||||
langs = self.pool.get('jobs.meta').check_lang_support(cr, uid, langs)
|
||||
term_ids = translation_pool.search(cr, uid, [('state', '=', 'translate'), ('gengo_translation', '=', True), ('lang', 'in', langs)], limit=local.LIMIT)
|
||||
if term_ids:
|
||||
self._send_translation_terms(cr, uid, ids, term_ids, context)
|
||||
_logger.info("Translation terms %s has been posted to gengo successfully", len(term_ids))
|
||||
else:
|
||||
msg = "This language is select as Active All Translation Request will be sent by System Automatically"
|
||||
context.update({'message': msg})
|
||||
_logger.info('No Translation terms to process.')
|
||||
except Exception, e:
|
||||
_logger.warning("%s", e)
|
||||
_logger.error("%s", e)
|
||||
|
||||
_name = 'base.update.translations'
|
||||
_inherit = "base.update.translations"
|
||||
|
|
|
@ -19,10 +19,9 @@
|
|||
#
|
||||
##############################################################################
|
||||
|
||||
from osv import orm, osv
|
||||
from tools.translate import _
|
||||
from osv import orm
|
||||
import tools
|
||||
from mygengo import MyGengo
|
||||
import re
|
||||
|
||||
LIMIT = 20
|
||||
|
||||
|
@ -57,12 +56,13 @@ LANG_CODE_MAPPING = {
|
|||
'sv_SE': 'sv',
|
||||
'ko_KR': 'ko',
|
||||
'pt_PT': 'pt',
|
||||
'en': 'en',
|
||||
'en_US': 'en',
|
||||
'ja_JP': 'ja',
|
||||
'es_ES': 'es',
|
||||
'zh_CN': 'zh',
|
||||
'de_DE': 'de',
|
||||
'fr_FR': 'fr',
|
||||
'fr_BE': 'fr',
|
||||
'ru_RU': 'ru',
|
||||
'it_IT': 'it',
|
||||
'pt_BR': 'pt-br'
|
||||
|
@ -105,44 +105,63 @@ class JobsMeta(orm.AbstractModel):
|
|||
|
||||
_name = "jobs.meta"
|
||||
|
||||
def gengo_authentication(self, cr, uid, ids, context=None):
|
||||
def gengo_authentication(self, cr, uid, context=None):
|
||||
''' To Send Request and Get Response from Gengo User needs Public and Private
|
||||
key for that user need to signup to gengo and get public and private
|
||||
key which is provided by gengo to authentic user '''
|
||||
|
||||
gengo_parameter_pool = self.pool.get('res.users').browse(cr, uid, uid, context)
|
||||
user = self.pool.get('res.users').browse(cr, uid, uid, context)
|
||||
if not user.company_id.gengo_public_key or not user.company_id.gengo_private_key:
|
||||
return (False, "Invalid gengo configuration.\nEither pulic key or private key is missing.")
|
||||
try:
|
||||
gengo = MyGengo(
|
||||
public_key=gengo_parameter_pool.company_id.gengo_public_key.encode('ascii'),
|
||||
private_key=gengo_parameter_pool.company_id.gengo_private_key.encode('ascii'),
|
||||
public_key=user.company_id.gengo_public_key.encode('ascii'),
|
||||
private_key=user.company_id.gengo_private_key.encode('ascii'),
|
||||
sandbox=True,
|
||||
)
|
||||
return gengo
|
||||
)
|
||||
gengo.getAccountStats()
|
||||
return (True, gengo)
|
||||
except Exception, e:
|
||||
raise osv.except_osv(_('Warning !'), _(e))
|
||||
return (False, "Gengo Connection Error\n"+e.message)
|
||||
|
||||
def pack_jobs_request(self, cr, uid, term_ids, trg_lang, context):
|
||||
def pack_jobs_request(self, cr, uid, term_ids, context):
|
||||
jobs = {}
|
||||
auto_approve = 0
|
||||
gengo_parameter_pool = self.pool.get('res.users').browse(cr, uid, uid, context)
|
||||
translation_pool = self.pool.get('ir.translation')
|
||||
if gengo_parameter_pool.company_id.gengo_auto_approve:
|
||||
auto_approve = 1
|
||||
g_lang = LANG_CODE_MAPPING.get(trg_lang)
|
||||
if g_lang:
|
||||
for terms in translation_pool.browse(cr, uid, term_ids, context):
|
||||
#NOTE: Discard none string and only special char string
|
||||
if re.search(r"[a-z A-Z]", terms.src) and terms.src:
|
||||
job = {'type': 'text',
|
||||
'slug': 'single::English to' + LANG_MAPPING.get(g_lang),
|
||||
'tier': gengo_parameter_pool.company_id.gengo_tier,
|
||||
'body_src': terms.src,
|
||||
'lc_src': 'en',
|
||||
'lc_tgt': g_lang,
|
||||
'auto_approve': auto_approve,
|
||||
'comment': gengo_parameter_pool.company_id.gengo_comment,
|
||||
}
|
||||
jobs.update({terms.id: job})
|
||||
for term in translation_pool.browse(cr, uid, term_ids, context):
|
||||
if term.src and term.src != "":
|
||||
job = {'type': 'text',
|
||||
'slug': 'single::English to' + LANG_CODE_MAPPING[term.lang],
|
||||
'tier': tools.ustr(gengo_parameter_pool.company_id.gengo_tier),
|
||||
'body_src': term.src,
|
||||
'lc_src': 'en',
|
||||
'lc_tgt': LANG_CODE_MAPPING[term.lang],
|
||||
'auto_approve': auto_approve,
|
||||
'comment': gengo_parameter_pool.company_id.gengo_comment,
|
||||
}
|
||||
jobs.update({term.id: job})
|
||||
return {'jobs': jobs}
|
||||
|
||||
def check_lang_support(self, cr, uid, langs, context=None):
|
||||
new_langs = []
|
||||
flag, gengo = self.gengo_authentication(cr, uid, context)
|
||||
if not flag:
|
||||
return []
|
||||
else:
|
||||
user = self.pool.get('res.users').browse(cr, uid, uid, context)
|
||||
tier = user.company_id.gengo_tier
|
||||
if tier == "machine":
|
||||
tier = "nonprofit"
|
||||
|
||||
lang_pair = gengo.getServiceLanguagePairs(lc_src='en')
|
||||
if lang_pair['opstat'] == 'ok':
|
||||
for g_lang in lang_pair['response']:
|
||||
for l in langs:
|
||||
if LANG_CODE_MAPPING[l] == g_lang['lc_tgt'] and g_lang['tier'] == tier:
|
||||
new_langs.append(l)
|
||||
return list(set(new_langs))
|
||||
JobsMeta()
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
Loading…
Reference in New Issue