From 96b8c9ad8a498397927bcbf78867ab96ef338ebf Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 12 Oct 2017 09:07:22 +0200 Subject: [PATCH] [RFR] use odoorpc --- base_import_odoo/__openerp__.py | 2 +- .../models/import_odoo_database.py | 26 ++++++++++---- .../tests/test_base_import_odoo.py | 34 +++++++------------ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/base_import_odoo/__openerp__.py b/base_import_odoo/__openerp__.py index cfc169e3c..5c40fc3a8 100644 --- a/base_import_odoo/__openerp__.py +++ b/base_import_odoo/__openerp__.py @@ -25,6 +25,6 @@ ], "installable": True, "external_dependencies": { - "python": ['erppeek'], + "python": ['odoorpc'], }, } diff --git a/base_import_odoo/models/import_odoo_database.py b/base_import_odoo/models/import_odoo_database.py index 50b87b982..2b99d2133 100644 --- a/base_import_odoo/models/import_odoo_database.py +++ b/base_import_odoo/models/import_odoo_database.py @@ -3,11 +3,12 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import logging try: - from erppeek import Client + import odoorpc except: - logging.debug('Unable to import erppeek') + logging.error('Unable to import odoorpc') import psycopg2 import traceback +from urlparse import urlparse from openerp import _, api, exceptions, fields, models, tools from collections import namedtuple @@ -105,8 +106,8 @@ class ImportOdooDatabase(models.Model): self.env.cr.commit() for model_line in self.import_line_ids: model = model_line.model_id - remote_ids[model.model] = remote.search( - model.model, + remote_ids[model.model] = remote.execute( + model.model, 'search', tools.safe_eval(model_line.domain) if model_line.domain else [] ) remote_counts[model.model] = len(remote_ids[model.model]) @@ -159,8 +160,8 @@ class ImportOdooDatabase(models.Model): """Import records of a configured model""" model = self.env[context.model_line.model_id.model] fields = self._run_import_model_get_fields(context) - for data in context.remote.read( - model._name, context.ids, fields.keys() + for data in context.remote.execute( + model._name, 'read', context.ids, fields.keys() ): self._run_import_get_record(context, model, data) if (model._name, data['id']) in context.idmap: @@ -449,7 +450,18 @@ class ImportOdooDatabase(models.Model): def _get_connection(self): self.ensure_one() - return Client(self.url, self.database, self.user, self.password) + url = urlparse(self.url) + hostport = url.netloc.split(':') + if len(hostport) == 1: + hostport.append('80') + host, port = hostport + remote = odoorpc.ODOO( + host, + protocol='jsonrpc+ssl' if url.scheme == 'https' else 'jsonrpc', + port=int(port), + ) + remote.login(self.database, self.user, self.password) + return remote @api.constrains('url', 'database', 'user', 'password') @api.multi diff --git a/base_import_odoo/tests/test_base_import_odoo.py b/base_import_odoo/tests/test_base_import_odoo.py index 3a9f3a186..76739fe56 100644 --- a/base_import_odoo/tests/test_base_import_odoo.py +++ b/base_import_odoo/tests/test_base_import_odoo.py @@ -8,28 +8,21 @@ from openerp.tests.common import TransactionCase, post_install, at_install class TestBaseImportOdoo(TransactionCase): @at_install(False) @post_install(True) - @patch('erppeek.Client.__init__', side_effect=lambda *args: None) - def test_base_import_odoo(self, mock_client_init): + @patch( + 'odoorpc.ODOO.__init__', + side_effect=lambda self, *args, **kwargs: None, + ) + @patch('odoorpc.ODOO.login', side_effect=lambda *args: None) + def test_base_import_odoo(self, mock_client, mock_client_login): # the mocked functions simply search/read in the current database # the effect then should be that the models in question are duplicated, # we just need to try not to be confused by the fact that local and # remote ids are the same - def _mock_search( - model, domain, offset=0, limit=None, order=None, context=None, - ): - return self.env[model].with_context( - **(context or self.env.context) - ).search( - domain, offset=offset, limit=limit, order=order, - ).ids - - def _mock_read( - model, domain_or_ids, fields=None, offset=0, limit=None, - order=None, context=None, - ): - return self.env[model].with_context( - **(context or self.env.context) - ).browse(domain_or_ids).read(fields=fields) + def _mock_execute(model, method, *args): + if method == 'read': + return self.env[model].browse(args[0]).read(fields=args[1]) + if method == 'search': + return self.env[model].search(args[0]).ids group_count = self.env['res.groups'].search([], count=True) user_count = self.env['res.users'].search([], count=True) @@ -39,9 +32,8 @@ class TestBaseImportOdoo(TransactionCase): self.env.ref('base_import_odoo.demodb').write({ 'password': 'admin', }) - with patch('erppeek.Client.search', side_effect=_mock_search): - with patch('erppeek.Client.read', side_effect=_mock_read): - self.env.ref('base_import_odoo.demodb')._run_import() + with patch('odoorpc.ODOO.execute', side_effect=_mock_execute): + self.env.ref('base_import_odoo.demodb')._run_import() # here the actual test begins - check that we created new # objects, check xmlids, check values, check if dummies are # cleaned up/replaced