merge lp:~openerp-dev/openobject-addons/trunk-base-gengo-resource-aja

bzr revid: jam@tinyerp.com-20120824132942-f0hg8rbkh2752wgr
This commit is contained in:
Jigar Amin - OpenERP
2012-08-24 18:59:42 +05:30
13 changed files with 486 additions and 4 deletions
+5 -1
View File
@@ -11,7 +11,7 @@
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
@@ -19,4 +19,8 @@
#
##############################################################################
import res_company
import ir_translation
import wizard
import res_lang
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+8 -3
View File
@@ -18,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'name': 'Automated Translations through Gengo API',
'version': '0.1',
@@ -29,8 +28,14 @@ Automated Translations through Gengo API
'author': 'OpenERP SA',
'website': 'http://www.openerp.com',
'depends': ['base'],
'init_xml': [],
'update_xml': [],
'init_xml': ['gengo_sync_schedular_data.xml'],
'update_xml': [
'ir_translation.xml',
'res_company_view.xml',
'res_lang_view.xml',
'wizard/alert_message_gengo.xml',
],
'demo_xml': [],
'test': [],
'installable': True,
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!--Scheduler sync Receive Request-->
<record id="gengo_sync_receive_request_scheduler" model="ir.cron">
<field name="name" >Gengo Sync Translation (Response)</field>
<field eval="False" name="active"/>
<field name="interval_number">30</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field eval="'base.update.translations'" name="model"></field>
<field eval="'_sync_response'" name="function"/>
</record>
<!--Schedular Sync Send Request-->
<record id="gengo_sync_send_request_scheduler" model="ir.cron">
<field name="name" >Gengo Sync Translation (Request)</field>
<field eval="False" name="active"/>
<field name="interval_number">30</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field eval="'base.update.translations'" name="model"></field>
<field eval="'_sync_request'" name="function"/>
</record>
</data>
</openerp>
+37
View File
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Business Applications
# Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
class ir_translation(osv.Model):
_name = "ir.translation"
_inherit = "ir.translation"
_columns = {
'gengo_comment': fields.text("Comments"),
'gengo_translation': fields.boolean("Translation", help='This term has to be translated by Gengo automatically'),
'gengo_control': fields.boolean('Active'),
'job_id':fields.char('Gengo Job Id',size=32),
}
_defaults = {
'gengo_control': False,
}
+36
View File
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_translation_search" model="ir.ui.view">
<field name="name">Translations</field>
<field name="model">ir.translation</field>
<field name="inherit_id" ref="base.view_translation_search"/>
<field name="type">search</field>
<field name="arch" type="xml">
<search string="Translations">
<filter icon="terp-project" string="In Progress" domain="[('state','=','inprogress')]"></filter>
<filter string="To Control" domain="[('state','=','translated')]"></filter>
</search>
</field>
</record>
<!-- ir.translation form view -->
<record model="ir.ui.view" id="view_ir_translation_inherit_base_gengo_form">
<field name="name">ir.translation.form.inherit</field>
<field name="inherit_id" ref="base.view_translation_form"/>
<field name="model">ir.translation</field>
<field name="type">form</field>
<field name="arch" type="xml">
<xpath expr="//form[@string='Translations']/sheet" position="inside">
<group string="comments" col="1">
<field name="gengo_comment" nolabel="1" placeholder="Add Your comments here for Translation Job...."/>
</group>
<group col="4">
<field name="gengo_translation" />
<field name="gengo_control" />
</group>
</xpath>
</field>
</record>
</data>
</openerp>
+42
View File
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Business Applications
# Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
class res_company(osv.Model):
_name = "res.company"
_inherit = "res.company"
_columns = {
"gengo_private_key": fields.text("Gengo private key"),
"gengo_public_key": fields.text("Gengo public key"),
"gengo_tier": fields.selection([('machine', 'Machine'),
('standard', 'Standard'),
('pro', 'Pro'),
('ultra', 'Ultra')], "Tier types", required=True),
"gengo_comment": fields.text("comments"),
"gengo_auto_approve": fields.boolean("Active", help="Jobs are Automatically Approved by Gengo."),
"fields_ids": fields.many2many('ir.model.fields', 'fields_company_rel', 'field_id', 'model_id', 'fields'),
}
_defaults = {
"gengo_tier": "machine",
}
+37
View File
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!-- res.company form view -->
<record model="ir.ui.view" id="view_company_inherit_base_gengo_form">
<field name="name">res.company.form.inherit</field>
<field name="inherit_id" ref="base.view_company_form"/>
<field name="model">res.company</field>
<field name="type">form</field>
<field name="arch" type="xml">
<xpath expr="//notebook[last()]" position="inside">
<page string="Gengo Parameters">
<group col="2">
<group col="1" string="Private Key">
<field name="gengo_private_key" nolabel="1"/>
</group>
<group col="1" string="Public Key">
<field name="gengo_public_key" nolabel="1"/>
</group>
</group>
<group col="4">
<field name="gengo_tier" />
<field name="gengo_auto_approve"/>
</group>
<group string="Comments" col="1">
<field name="gengo_comment" nolabel="1" placeholder="Add Your comments here for Translator...."/>
</group>
<group col="1">
<field name="fields_ids" nolabel="1"/>
</group>
</page>
</xpath>
</field>
</record>
</data>
</openerp>
+31
View File
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Business Applications
# Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
class res_company(osv.Model):
_name = "res.lang"
_inherit = "res.lang"
_columns = {
'gengo_sync': fields.boolean('Active', help='Synchronize Translation Periodically')
}
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!-- res.company form view -->
<record model="ir.ui.view" id="view_language_inherit_base_gengo_form">
<field name="name">res.lang.form.inherit</field>
<field name="inherit_id" ref="base.res_lang_form"/>
<field name="model">res.lang</field>
<field name="type">form</field>
<field name="arch" type="xml">
<xpath expr="//sheet/group" position="inside">
<field name="gengo_sync" />
</xpath>
</field>
</record>
</data>
</openerp>
+25
View File
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import gengo_update_translation
import abstract_wrapper
import gengo_update_message
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Update Message Form View -->
<record model="ir.ui.view" id="update_translation_wizard_view_confirm">
<field name="name">gengo.update.message.form</field>
<field name="model">gengo.update.message</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Update Gengo Data" version="7.0">
<group>
<field name='name'/>
</group>
</form>
</field>
</record>
</data>
</openerp>
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv
from osv import fields
class gengo_update_message(osv.osv):
"""Update Message"""
_name = "gengo.update.message"
_description = "Update Message"
_columns = {
'name': fields.text('Message', readonly=True),
}
def default_get(self, cr, uid, fields, context=None):
if context == None:
context = {}
res = super(gengo_update_message, self).default_get(cr, uid, fields, context=context)
res.update({'name': context.get('message')})
return res
gengo_update_message()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
@@ -0,0 +1,160 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Business Applications
# Copyright (C) 2004-2012 OpenERP S.A. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv
import abstract_wrapper as local
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': ""
}
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']:
vals = {}
for t_id, res in jobs.items():
if tier == "machine":
vals.update({'value': res['body_tgt'], 'state': 'translated'})
else:
vals.update({'job_id': res['job_id'], 'state': 'inprogress'})
translation_pool.write(cr, uid, [t_id], vals, context)
return
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)
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 = []
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)
def act_update(self, cr, uid, ids, context=None):
if context == None:
context = {}
lang_pool = self.pool.get('res.lang')
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)
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')
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=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:
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:
_logger.info('No Translation terms to process.')
except Exception, e:
_logger.error("%s", e)
_name = 'base.update.translations'
_inherit = "base.update.translations"
base_update_translation()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: