diff --git a/addons/base_gengo/__init__.py b/addons/base_gengo/__init__.py index 6825dc22f47..c434c6b1843 100644 --- a/addons/base_gengo/__init__.py +++ b/addons/base_gengo/__init__.py @@ -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: diff --git a/addons/base_gengo/__openerp__.py b/addons/base_gengo/__openerp__.py index 43a6e258418..1173d513493 100644 --- a/addons/base_gengo/__openerp__.py +++ b/addons/base_gengo/__openerp__.py @@ -18,7 +18,6 @@ # along with this program. If not, see . # ############################################################################## - { '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, diff --git a/addons/base_gengo/gengo_sync_schedular_data.xml b/addons/base_gengo/gengo_sync_schedular_data.xml new file mode 100644 index 00000000000..ab78b59e0b8 --- /dev/null +++ b/addons/base_gengo/gengo_sync_schedular_data.xml @@ -0,0 +1,26 @@ + + + + + + Gengo Sync Translation (Response) + + 30 + minutes + -1 + + + + + + + Gengo Sync Translation (Request) + + 30 + minutes + -1 + + + + + diff --git a/addons/base_gengo/ir_translation.py b/addons/base_gengo/ir_translation.py new file mode 100644 index 00000000000..7bda5023592 --- /dev/null +++ b/addons/base_gengo/ir_translation.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (C) 2004-2012 OpenERP S.A. (). +# +# 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 . +# +############################################################################## + +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, + } diff --git a/addons/base_gengo/ir_translation.xml b/addons/base_gengo/ir_translation.xml new file mode 100644 index 00000000000..753c12c7e7d --- /dev/null +++ b/addons/base_gengo/ir_translation.xml @@ -0,0 +1,36 @@ + + + + + Translations + ir.translation + + search + + + + + + + + + + + ir.translation.form.inherit + + ir.translation + form + + + + + + + + + + + + + + diff --git a/addons/base_gengo/res_company.py b/addons/base_gengo/res_company.py new file mode 100644 index 00000000000..888e675e26b --- /dev/null +++ b/addons/base_gengo/res_company.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (C) 2004-2012 OpenERP S.A. (). +# +# 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 . +# +############################################################################## + +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", + } diff --git a/addons/base_gengo/res_company_view.xml b/addons/base_gengo/res_company_view.xml new file mode 100644 index 00000000000..bfa0b1ddf95 --- /dev/null +++ b/addons/base_gengo/res_company_view.xml @@ -0,0 +1,37 @@ + + + + + + res.company.form.inherit + + res.company + form + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/addons/base_gengo/res_lang.py b/addons/base_gengo/res_lang.py new file mode 100644 index 00000000000..e9c88c9fd29 --- /dev/null +++ b/addons/base_gengo/res_lang.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (C) 2004-2012 OpenERP S.A. (). +# +# 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 . +# +############################################################################## + +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') + } diff --git a/addons/base_gengo/res_lang_view.xml b/addons/base_gengo/res_lang_view.xml new file mode 100644 index 00000000000..e0c8edd11aa --- /dev/null +++ b/addons/base_gengo/res_lang_view.xml @@ -0,0 +1,17 @@ + + + + + + res.lang.form.inherit + + res.lang + form + + + + + + + + diff --git a/addons/base_gengo/wizard/__init__.py b/addons/base_gengo/wizard/__init__.py new file mode 100644 index 00000000000..7bde4753da6 --- /dev/null +++ b/addons/base_gengo/wizard/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2010 Tiny SPRL (). +# +# 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 . +# +############################################################################## + +import gengo_update_translation +import abstract_wrapper +import gengo_update_message +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/base_gengo/wizard/alert_message_gengo.xml b/addons/base_gengo/wizard/alert_message_gengo.xml new file mode 100644 index 00000000000..84bfc7bb484 --- /dev/null +++ b/addons/base_gengo/wizard/alert_message_gengo.xml @@ -0,0 +1,18 @@ + + + + + + gengo.update.message.form + gengo.update.message + form + +
+ + + +
+
+
+
+
diff --git a/addons/base_gengo/wizard/gengo_update_message.py b/addons/base_gengo/wizard/gengo_update_message.py new file mode 100644 index 00000000000..214b9978a19 --- /dev/null +++ b/addons/base_gengo/wizard/gengo_update_message.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2010 Tiny SPRL (). +# +# 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 . +# +############################################################################## + +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: diff --git a/addons/base_gengo/wizard/gengo_update_translation.py b/addons/base_gengo/wizard/gengo_update_translation.py new file mode 100644 index 00000000000..9cb5f0696f5 --- /dev/null +++ b/addons/base_gengo/wizard/gengo_update_translation.py @@ -0,0 +1,160 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Business Applications +# Copyright (C) 2004-2012 OpenERP S.A. (). +# +# 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 . +# +############################################################################## + +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: