Browse Source

[RFR] use odoorpc

10.0
Holger Brunn 7 years ago
committed by OCA-git-bot
parent
commit
96b8c9ad8a
  1. 2
      base_import_odoo/__openerp__.py
  2. 26
      base_import_odoo/models/import_odoo_database.py
  3. 34
      base_import_odoo/tests/test_base_import_odoo.py

2
base_import_odoo/__openerp__.py

@ -25,6 +25,6 @@
], ],
"installable": True, "installable": True,
"external_dependencies": { "external_dependencies": {
"python": ['erppeek'],
"python": ['odoorpc'],
}, },
} }

26
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). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging import logging
try: try:
from erppeek import Client
import odoorpc
except: except:
logging.debug('Unable to import erppeek')
logging.error('Unable to import odoorpc')
import psycopg2 import psycopg2
import traceback import traceback
from urlparse import urlparse
from openerp import _, api, exceptions, fields, models, tools from openerp import _, api, exceptions, fields, models, tools
from collections import namedtuple from collections import namedtuple
@ -105,8 +106,8 @@ class ImportOdooDatabase(models.Model):
self.env.cr.commit() self.env.cr.commit()
for model_line in self.import_line_ids: for model_line in self.import_line_ids:
model = model_line.model_id 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 [] tools.safe_eval(model_line.domain) if model_line.domain else []
) )
remote_counts[model.model] = len(remote_ids[model.model]) remote_counts[model.model] = len(remote_ids[model.model])
@ -159,8 +160,8 @@ class ImportOdooDatabase(models.Model):
"""Import records of a configured model""" """Import records of a configured model"""
model = self.env[context.model_line.model_id.model] model = self.env[context.model_line.model_id.model]
fields = self._run_import_model_get_fields(context) 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) self._run_import_get_record(context, model, data)
if (model._name, data['id']) in context.idmap: if (model._name, data['id']) in context.idmap:
@ -449,7 +450,18 @@ class ImportOdooDatabase(models.Model):
def _get_connection(self): def _get_connection(self):
self.ensure_one() 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.constrains('url', 'database', 'user', 'password')
@api.multi @api.multi

34
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): class TestBaseImportOdoo(TransactionCase):
@at_install(False) @at_install(False)
@post_install(True) @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 mocked functions simply search/read in the current database
# the effect then should be that the models in question are duplicated, # 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 # we just need to try not to be confused by the fact that local and
# remote ids are the same # 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) group_count = self.env['res.groups'].search([], count=True)
user_count = self.env['res.users'].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({ self.env.ref('base_import_odoo.demodb').write({
'password': 'admin', '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 # here the actual test begins - check that we created new
# objects, check xmlids, check values, check if dummies are # objects, check xmlids, check values, check if dummies are
# cleaned up/replaced # cleaned up/replaced

Loading…
Cancel
Save