[IMP] base_import_odoo: black, isort, prettier

pull/3136/head
Holger Brunn 2020-12-07 07:23:09 +01:00 committed by Tom
parent fed8758ced
commit 2a111a42c8
18 changed files with 561 additions and 441 deletions

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from . import models from . import models

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
{ {
@ -9,9 +8,7 @@
"category": "Tools", "category": "Tools",
"summary": "Import records from another Odoo instance", "summary": "Import records from another Odoo instance",
"website": "https://github.com/OCA/server-tools", "website": "https://github.com/OCA/server-tools",
"depends": [ "depends": ["mail",],
'mail',
],
"demo": [ "demo": [
"demo/res_partner.xml", "demo/res_partner.xml",
"demo/res_users.xml", "demo/res_users.xml",
@ -27,7 +24,5 @@
"views/menu.xml", "views/menu.xml",
], ],
"installable": True, "installable": True,
"external_dependencies": { "external_dependencies": {"python": ["odoorpc"],},
"python": ['odoorpc'],
},
} }

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record id="demodb" model="import.odoo.database"> <record id="demodb" model="import.odoo.database">
<field name="url">http://localhost:8069</field> <field name="url">http://localhost:8069</field>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record id="mapping_partner_id_root" model="import.odoo.database.field"> <record id="mapping_partner_id_root" model="import.odoo.database.field">
<field name="database_id" ref="demodb" /> <field name="database_id" ref="demodb" />

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record id="model_partner" model="import.odoo.database.model"> <record id="model_partner" model="import.odoo.database.model">
<field name="sequence">1</field> <field name="sequence">1</field>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<record id="attachment_demo" model="ir.attachment"> <record id="attachment_demo" model="ir.attachment">
<field name="name">Demo attachment</field> <field name="name">Demo attachment</field>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<record id="base.res_partner_1" model="res.partner"> <record id="base.res_partner_1" model="res.partner">
<field name="user_id" ref="base.user_demo" /> <field name="user_id" ref="base.user_demo" />

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1"> <odoo noupdate="1">
<record id="mapped_admin" model="res.users" context="{'no_reset_password': True}"> <record id="mapped_admin" model="res.users" context="{'no_reset_password': True}">
<field name="name">Mapped admin</field> <field name="name">Mapped admin</field>

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from . import import_odoo_database from . import import_odoo_database

View File

@ -1,27 +1,37 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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
import traceback
from collections import namedtuple
from urlparse import urlparse
import psycopg2
from odoo import _, api, exceptions, fields, models, tools
from odoo.tools.safe_eval import safe_eval
try: try:
import odoorpc import odoorpc
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
logging.info('Unable to import odoorpc, used in base_import_odoo') logging.info("Unable to import odoorpc, used in base_import_odoo")
odoorpc = False odoorpc = False
import psycopg2
import traceback _logger = logging.getLogger("base_import_odoo")
from urlparse import urlparse
from odoo import _, api, exceptions, fields, models, tools
from odoo.tools.safe_eval import safe_eval
from collections import namedtuple
_logger = logging.getLogger('base_import_odoo')
import_context_tuple = namedtuple( import_context_tuple = namedtuple(
'import_context', [ "import_context",
'remote', 'model_line', 'ids', 'idmap', 'dummies', 'dummy_instances', [
'to_delete', 'field_context', "remote",
] "model_line",
"ids",
"idmap",
"dummies",
"dummy_instances",
"to_delete",
"field_context",
],
) )
@ -31,46 +41,49 @@ class ImportContext(import_context_tuple):
field_context = namedtuple( field_context = namedtuple(
'field_context', ['record_model', 'field_name', 'record_id'], "field_context", ["record_model", "field_name", "record_id"],
) )
mapping_key = namedtuple('mapping_key', ['model_name', 'remote_id']) mapping_key = namedtuple("mapping_key", ["model_name", "remote_id"])
dummy_instance = namedtuple( dummy_instance = namedtuple(
'dummy_instance', ['model_name', 'field_name', 'remote_id', 'dummy_id'], "dummy_instance", ["model_name", "field_name", "remote_id", "dummy_id"],
) )
class ImportOdooDatabase(models.Model): class ImportOdooDatabase(models.Model):
_name = 'import.odoo.database' _name = "import.odoo.database"
_description = 'An Odoo database to import' _description = "An Odoo database to import"
url = fields.Char(required=True) url = fields.Char(required=True)
database = fields.Char(required=True) database = fields.Char(required=True)
user = fields.Char(default='admin', required=True) user = fields.Char(default="admin", required=True)
password = fields.Char(default='admin') password = fields.Char(default="admin")
import_line_ids = fields.One2many( import_line_ids = fields.One2many(
'import.odoo.database.model', 'database_id', string='Import models', "import.odoo.database.model", "database_id", string="Import models",
) )
import_field_mappings = fields.One2many( import_field_mappings = fields.One2many(
'import.odoo.database.field', 'database_id', string='Field mappings', "import.odoo.database.field", "database_id", string="Field mappings",
) )
cronjob_id = fields.Many2one( cronjob_id = fields.Many2one(
'ir.cron', string='Import job', readonly=True, copy=False, "ir.cron", string="Import job", readonly=True, copy=False,
) )
cronjob_running = fields.Boolean(compute='_compute_cronjob_running') cronjob_running = fields.Boolean(compute="_compute_cronjob_running")
status_data = fields.Serialized('Status', readonly=True, copy=False) status_data = fields.Serialized("Status", readonly=True, copy=False)
status_html = fields.Html( status_html = fields.Html(
compute='_compute_status_html', readonly=True, sanitize=False, compute="_compute_status_html", readonly=True, sanitize=False,
) )
duplicates = fields.Selection( duplicates = fields.Selection(
[ [
('skip', 'Skip existing'), ('overwrite', 'Overwrite existing'), ("skip", "Skip existing"),
('overwrite_empty', 'Overwrite empty fields'), ("overwrite", "Overwrite existing"),
("overwrite_empty", "Overwrite empty fields"),
], ],
'Duplicate handling', default='skip', required=True, "Duplicate handling",
default="skip",
required=True,
) )
@api.multi @api.multi
@ -78,14 +91,10 @@ class ImportOdooDatabase(models.Model):
"""Create a cronjob to run the actual import""" """Create a cronjob to run the actual import"""
self.ensure_one() self.ensure_one()
if self.cronjob_id: if self.cronjob_id:
return self.cronjob_id.write({ return self.cronjob_id.write(
'numbercall': 1, {"numbercall": 1, "doall": True, "active": True,}
'doall': True, )
'active': True, return self.write({"cronjob_id": self._create_cronjob().id,})
})
return self.write({
'cronjob_id': self._create_cronjob().id,
})
@api.model @api.model
def _run_import_cron(self, ids): def _run_import_cron(self, ids):
@ -96,11 +105,9 @@ class ImportOdooDatabase(models.Model):
"""Run the import as cronjob, commit often""" """Run the import as cronjob, commit often"""
self.ensure_one() self.ensure_one()
if not self.password: if not self.password:
self.write({ self.write(
'status_data': dict( {"status_data": dict(self.status_data, error="No password provided",),}
self.status_data, error='No password provided', )
),
})
return return
# model name: [ids] # model name: [ids]
remote_ids = {} remote_ids = {}
@ -120,44 +127,51 @@ class ImportOdooDatabase(models.Model):
# dummy_instance # dummy_instance
dummy_instances = [] dummy_instances = []
remote = self._get_connection() remote = self._get_connection()
self.write({'password': False}) self.write({"password": False})
if commit and not tools.config['test_enable']: if commit and not tools.config["test_enable"]:
# pylint: disable=invalid-commit # pylint: disable=invalid-commit
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.execute( remote_ids[model.model] = remote.execute(
model.model, 'search', model.model,
safe_eval(model_line.domain) if model_line.domain else [] "search",
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])
self.write({ self.write(
'status_data': { {
'counts': remote_counts, "status_data": {
'ids': remote_ids, "counts": remote_counts,
'error': None, "ids": remote_ids,
'dummies': None, "error": None,
'done': {}, "dummies": None,
"done": {},
} }
}) }
if commit and not tools.config['test_enable']: )
if commit and not tools.config["test_enable"]:
# pylint: disable=invalid-commit # pylint: disable=invalid-commit
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 = self.env[model_line.model_id.model] model = self.env[model_line.model_id.model]
done[model._name] = 0 done[model._name] = 0
chunk_len = commit and (commit_threshold or 1) or len( chunk_len = (
remote_ids[model._name] commit and (commit_threshold or 1) or len(remote_ids[model._name])
) )
for start_index in range( for start_index in range(len(remote_ids[model._name]) / chunk_len + 1):
len(remote_ids[model._name]) / chunk_len + 1
):
index = start_index * chunk_len index = start_index * chunk_len
ids = remote_ids[model._name][index:index + chunk_len] ids = remote_ids[model._name][index : index + chunk_len]
context = ImportContext( context = ImportContext(
remote, model_line, ids, idmap, dummies, dummy_instances, remote,
to_delete, field_context(None, None, None), model_line,
ids,
idmap,
dummies,
dummy_instances,
to_delete,
field_context(None, None, None),
) )
try: try:
self._run_import_model(context) self._run_import_model(context)
@ -165,25 +179,25 @@ class ImportOdooDatabase(models.Model):
# pragma: no cover # pragma: no cover
error = traceback.format_exc() error = traceback.format_exc()
self.env.cr.rollback() self.env.cr.rollback()
self.write({ self.write(
'status_data': dict(self.status_data, error=error), {"status_data": dict(self.status_data, error=error),}
}) )
# pylint: disable=invalid-commit # pylint: disable=invalid-commit
self.env.cr.commit() self.env.cr.commit()
raise raise
done[model._name] += len(ids) done[model._name] += len(ids)
self.write({'status_data': dict(self.status_data, done=done)}) self.write({"status_data": dict(self.status_data, done=done)})
if commit and not tools.config['test_enable']: if commit and not tools.config["test_enable"]:
# pylint: disable=invalid-commit # pylint: disable=invalid-commit
self.env.cr.commit() self.env.cr.commit()
missing = {} missing = {}
for dummy_model, remote_id in dummies.keys(): for dummy_model, remote_id in dummies.keys():
if remote_id: if remote_id:
missing.setdefault(dummy_model, []).append(remote_id) missing.setdefault(dummy_model, []).append(remote_id)
self.write({ self.write(
'status_data': dict(self.status_data, dummies=dict(missing)), {"status_data": dict(self.status_data, dummies=dict(missing)),}
}) )
@api.multi @api.multi
def _run_import_model(self, context): def _run_import_model(self, context):
@ -191,22 +205,22 @@ class ImportOdooDatabase(models.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.execute( for data in context.remote.execute(
model._name, 'read', context.ids, fields.keys() model._name, "read", context.ids, fields.keys()
): ):
self._run_import_get_record( self._run_import_get_record(
context, model, data, create_dummy=False, context, model, data, create_dummy=False,
) )
if (model._name, data['id']) in context.idmap: if (model._name, data["id"]) in context.idmap:
# one of our mappings hit, create an xmlid to persist # one of our mappings hit, create an xmlid to persist
# this knowledge # this knowledge
self._create_record_xmlid( self._create_record_xmlid(
model, context.idmap[(model._name, data['id'])], data['id'] model, context.idmap[(model._name, data["id"])], data["id"]
) )
if self.duplicates == 'skip': if self.duplicates == "skip":
# there's a mapping for this record, nothing to do # there's a mapping for this record, nothing to do
continue continue
data = self._run_import_map_values(context, data) data = self._run_import_map_values(context, data)
_id = data['id'] _id = data["id"]
record = self._create_record(context, model, data) record = self._create_record(context, model, data)
self._run_import_model_cleanup_dummies( self._run_import_model_cleanup_dummies(
context, model, _id, record.id, context, model, _id, record.id,
@ -215,10 +229,8 @@ class ImportOdooDatabase(models.Model):
@api.multi @api.multi
def _create_record(self, context, model, record): def _create_record(self, context, model, record):
"""Create a record, add an xmlid""" """Create a record, add an xmlid"""
_id = record.pop('id') _id = record.pop("id")
xmlid = '%d-%s-%d' % ( xmlid = "%d-%s-%d" % (self.id, model._name.replace(".", "_"), _id or 0,)
self.id, model._name.replace('.', '_'), _id or 0,
)
record = self._create_record_filter_fields(model, record) record = self._create_record_filter_fields(model, record)
model_defaults = {} model_defaults = {}
if context.model_line.defaults: if context.model_line.defaults:
@ -227,50 +239,45 @@ class ImportOdooDatabase(models.Model):
record.setdefault(key, value) record.setdefault(key, value)
if context.model_line.postprocess: if context.model_line.postprocess:
safe_eval( safe_eval(
context.model_line.postprocess, { context.model_line.postprocess,
'vals': record, {
'env': self.env, "vals": record,
'_id': _id, "env": self.env,
'remote': context.remote, "_id": _id,
"remote": context.remote,
}, },
mode='exec', mode="exec",
) )
new = self.env.ref('base_import_odoo.%s' % xmlid, False) new = self.env.ref("base_import_odoo.%s" % xmlid, False)
if new and new.exists(): if new and new.exists():
if self.duplicates == 'overwrite_empty': if self.duplicates == "overwrite_empty":
record = { record = {key: value for key, value in record.items() if not new[key]}
key: value new.with_context(**self._create_record_context(model, record)).write(record)
for key, value in record.items() _logger.debug("Updated record %s", xmlid)
if not new[key]
}
new.with_context(
**self._create_record_context(model, record)
).write(record)
_logger.debug('Updated record %s', xmlid)
else: else:
new = model.with_context( new = model.with_context(
**self._create_record_context(model, record) **self._create_record_context(model, record)
).create(record) ).create(record)
self._create_record_xmlid(model, new.id, _id) self._create_record_xmlid(model, new.id, _id)
_logger.debug('Created record %s', xmlid) _logger.debug("Created record %s", xmlid)
context.idmap[mapping_key(model._name, _id)] = new.id context.idmap[mapping_key(model._name, _id)] = new.id
return new return new
def _create_record_xmlid(self, model, local_id, remote_id): def _create_record_xmlid(self, model, local_id, remote_id):
xmlid = '%d-%s-%d' % ( xmlid = "%d-%s-%d" % (self.id, model._name.replace(".", "_"), remote_id or 0,)
self.id, model._name.replace('.', '_'), remote_id or 0, if self.env.ref("base_import_odoo.%s" % xmlid, False):
)
if self.env.ref('base_import_odoo.%s' % xmlid, False):
return return
return self.env['ir.model.data'].create({ return self.env["ir.model.data"].create(
'name': xmlid, {
'model': model._name, "name": xmlid,
'module': 'base_import_odoo', "model": model._name,
'res_id': local_id, "module": "base_import_odoo",
'noupdate': True, "res_id": local_id,
'import_database_id': self.id, "noupdate": True,
'import_database_record_id': remote_id, "import_database_id": self.id,
}) "import_database_record_id": remote_id,
}
)
def _create_record_filter_fields(self, model, record): def _create_record_filter_fields(self, model, record):
"""Return a version of record with unknown fields for model removed """Return a version of record with unknown fields for model removed
@ -278,7 +285,8 @@ class ImportOdooDatabase(models.Model):
defaults = model.default_get(record.keys()) defaults = model.default_get(record.keys())
return { return {
key: value key: value
if value or not model._fields[key].required else defaults.get(key) if value or not model._fields[key].required
else defaults.get(key)
for key, value in record.items() for key, value in record.items()
if key in model._fields if key in model._fields
} }
@ -286,10 +294,10 @@ class ImportOdooDatabase(models.Model):
def _create_record_context(self, model, record): def _create_record_context(self, model, record):
"""Return a context that is used when creating a record""" """Return a context that is used when creating a record"""
context = { context = {
'tracking_disable': True, "tracking_disable": True,
} }
if model._name == 'res.users': if model._name == "res.users":
context['no_reset_password'] = True context["no_reset_password"] = True
return context return context
@api.multi @api.multi
@ -298,10 +306,10 @@ class ImportOdooDatabase(models.Model):
): ):
"""Find the local id of some remote record. Create a dummy if not """Find the local id of some remote record. Create a dummy if not
available""" available"""
_id = context.idmap.get((model._name, record['id'])) _id = context.idmap.get((model._name, record["id"]))
logged = False logged = False
if not _id: if not _id:
_id = context.dummies.get((model._name, record['id'])) _id = context.dummies.get((model._name, record["id"]))
if _id: if _id:
context.dummy_instances.append( context.dummy_instances.append(
dummy_instance(*(context.field_context + (_id,))) dummy_instance(*(context.field_context + (_id,)))
@ -309,8 +317,7 @@ class ImportOdooDatabase(models.Model):
else: else:
logged = True logged = True
_logger.debug( _logger.debug(
'Got %s(%d[%d]) from idmap', model._name, _id, "Got %s(%d[%d]) from idmap", model._name, _id, record["id"] or 0,
record['id'] or 0,
) )
if not _id: if not _id:
_id = self._run_import_get_record_mapping( _id = self._run_import_get_record_mapping(
@ -319,32 +326,36 @@ class ImportOdooDatabase(models.Model):
elif not logged: elif not logged:
logged = True logged = True
_logger.debug( _logger.debug(
'Got %s(%d[%d]) from dummies', model._name, _id, record['id'], "Got %s(%d[%d]) from dummies", model._name, _id, record["id"],
) )
if not _id: if not _id:
xmlid = self.env['ir.model.data'].search([ xmlid = self.env["ir.model.data"].search(
('import_database_id', '=', self.id), [
('import_database_record_id', '=', record['id']), ("import_database_id", "=", self.id),
('model', '=', model._name), ("import_database_record_id", "=", record["id"]),
], limit=1) ("model", "=", model._name),
],
limit=1,
)
if xmlid: if xmlid:
_id = xmlid.res_id _id = xmlid.res_id
context.idmap[(model._name, record['id'])] = _id context.idmap[(model._name, record["id"])] = _id
elif not logged: elif not logged:
logged = True logged = True
_logger.debug( _logger.debug(
'Got %s(%d[%d]) from mappings', "Got %s(%d[%d]) from mappings", model._name, _id, record["id"],
model._name, _id, record['id'],
) )
if not _id and create_dummy: if not _id and create_dummy:
_id = self._run_import_create_dummy( _id = self._run_import_create_dummy(
context, model, record, context,
forcecreate=record['id'] and record['id'] not in model,
self.status_data['ids'].get(model._name, []) record,
forcecreate=record["id"]
and record["id"] not in self.status_data["ids"].get(model._name, []),
) )
elif _id and not logged: elif _id and not logged:
_logger.debug( _logger.debug(
'Got %s(%d[%d]) from xmlid', model._name, _id, record['id'], "Got %s(%d[%d]) from xmlid", model._name, _id, record["id"],
) )
return _id return _id
@ -352,44 +363,49 @@ class ImportOdooDatabase(models.Model):
def _run_import_get_record_mapping( def _run_import_get_record_mapping(
self, context, model, record, create_dummy=True, self, context, model, record, create_dummy=True,
): ):
current_field = self.env['ir.model.fields'].search([ current_field = self.env["ir.model.fields"].search(
('name', '=', context.field_context.field_name), [
('model_id.model', '=', context.field_context.record_model), ("name", "=", context.field_context.field_name),
]) ("model_id.model", "=", context.field_context.record_model),
]
)
mappings = self.import_field_mappings.filtered( mappings = self.import_field_mappings.filtered(
lambda x: lambda x: x.mapping_type == "fixed"
x.mapping_type == 'fixed' and and x.model_id.model == model._name
x.model_id.model == model._name and and (not x.field_ids or current_field in x.field_ids)
( and x.local_id
not x.field_ids or current_field in x.field_ids and (x.remote_id == record["id"] or not x.remote_id)
) and x.local_id and or x.mapping_type == "by_field"
(x.remote_id == record['id'] or not x.remote_id) or and x.model_id.model == model._name
x.mapping_type == 'by_field' and
x.model_id.model == model._name
) )
_id = None _id = None
for mapping in mappings: for mapping in mappings:
if mapping.mapping_type == 'fixed': if mapping.mapping_type == "fixed":
assert mapping.local_id assert mapping.local_id
_id = mapping.local_id _id = mapping.local_id
context.idmap[(model._name, record['id'])] = _id context.idmap[(model._name, record["id"])] = _id
break break
elif mapping.mapping_type == 'by_field': elif mapping.mapping_type == "by_field":
assert mapping.field_ids assert mapping.field_ids
if len(record) == 1: if len(record) == 1:
# just the id of a record we haven't seen yet. # just the id of a record we haven't seen yet.
# read the whole record from remote to check if # read the whole record from remote to check if
# this can be mapped to an existing record # this can be mapped to an existing record
record = context.remote.execute( record = (
model._name, 'read', record['id'], context.remote.execute(
mapping.field_ids.mapped('name'), model._name,
) or None "read",
record["id"],
mapping.field_ids.mapped("name"),
)
or None
)
if not record: if not record:
continue continue
if isinstance(record, list): if isinstance(record, list):
record = record[0] record = record[0]
domain = [ domain = [
(field.name, '=', record.get(field.name)) (field.name, "=", record.get(field.name))
for field in mapping.field_ids for field in mapping.field_ids
if record.get(field.name) if record.get(field.name)
] ]
@ -397,15 +413,13 @@ class ImportOdooDatabase(models.Model):
# play it save, only use mapping if we really select # play it save, only use mapping if we really select
# something specific # something specific
continue continue
records = model.with_context(active_test=False).search( records = model.with_context(active_test=False).search(domain, limit=1,)
domain, limit=1,
)
if records: if records:
_id = records.id _id = records.id
context.idmap[(model._name, record['id'])] = _id context.idmap[(model._name, record["id"])] = _id
break break
else: else:
raise exceptions.UserError(_('Unknown mapping')) raise exceptions.UserError(_("Unknown mapping"))
return _id return _id
@api.multi @api.multi
@ -414,84 +428,87 @@ class ImportOdooDatabase(models.Model):
): ):
"""Either misuse some existing record or create an empty one to satisfy """Either misuse some existing record or create an empty one to satisfy
required links""" required links"""
dummy = model.search([ dummy = model.search(
[
( (
'id', 'not in', "id",
"not in",
[ [
v for (model_name, remote_id), v v
in context.dummies.items() for (model_name, remote_id), v in context.dummies.items()
if model_name == model._name if model_name == model._name
] +
[
mapping.local_id for mapping
in self.import_field_mappings
if mapping.model_id.model == model._name and
mapping.local_id
] ]
+ [
mapping.local_id
for mapping in self.import_field_mappings
if mapping.model_id.model == model._name and mapping.local_id
],
), ),
], limit=1) ],
limit=1,
)
if dummy and not forcecreate: if dummy and not forcecreate:
context.dummies[mapping_key(model._name, record['id'])] = dummy.id context.dummies[mapping_key(model._name, record["id"])] = dummy.id
context.dummy_instances.append( context.dummy_instances.append(
dummy_instance(*(context.field_context + (dummy.id,))) dummy_instance(*(context.field_context + (dummy.id,)))
) )
_logger.debug( _logger.debug(
'Using %d as dummy for %s(%d[%d]).%s[%d]', "Using %d as dummy for %s(%d[%d]).%s[%d]",
dummy.id, context.field_context.record_model, dummy.id,
context.field_context.record_model,
context.idmap.get(context.field_context.record_id, 0), context.idmap.get(context.field_context.record_id, 0),
context.field_context.record_id, context.field_context.record_id,
context.field_context.field_name, record['id'], context.field_context.field_name,
record["id"],
) )
return dummy.id return dummy.id
required = [ required = [name for name, field in model._fields.items() if field.required]
name
for name, field in model._fields.items()
if field.required
]
defaults = model.default_get(required) defaults = model.default_get(required)
values = {'id': record['id']} values = {"id": record["id"]}
for name, field in model._fields.items(): for name, field in model._fields.items():
if name not in required or defaults.get(name): if name not in required or defaults.get(name):
continue continue
value = None value = None
if field.type in ['char', 'text', 'html']: if field.type in ["char", "text", "html"]:
value = '/' value = "/"
elif field.type in ['boolean']: elif field.type in ["boolean"]:
value = False value = False
elif field.type in ['integer', 'float']: elif field.type in ["integer", "float"]:
value = 0 value = 0
elif model._fields[name].type in ['date', 'datetime']: elif model._fields[name].type in ["date", "datetime"]:
value = '2000-01-01' value = "2000-01-01"
elif field.type in ['many2one']: elif field.type in ["many2one"]:
if name in model._inherits.values(): if name in model._inherits.values():
continue continue
new_context = context.with_field_context( new_context = context.with_field_context(
model._name, name, record['id'] model._name, name, record["id"]
) )
value = self._run_import_get_record( value = self._run_import_get_record(
new_context, new_context,
self.env[model._fields[name].comodel_name], self.env[model._fields[name].comodel_name],
{'id': record.get(name, [None])[0]}, {"id": record.get(name, [None])[0]},
) )
elif field.type in ['selection'] and not callable(field.selection): elif field.type in ["selection"] and not callable(field.selection):
value = field.selection[0][0] value = field.selection[0][0]
elif field.type in ['selection'] and callable(field.selection): elif field.type in ["selection"] and callable(field.selection):
value = field.selection(model)[0][0] value = field.selection(model)[0][0]
values[name] = value values[name] = value
dummy = self._create_record(context, model, values) dummy = self._create_record(context, model, values)
del context.idmap[mapping_key(model._name, record['id'])] del context.idmap[mapping_key(model._name, record["id"])]
context.dummies[mapping_key(model._name, record['id'])] = dummy.id context.dummies[mapping_key(model._name, record["id"])] = dummy.id
context.to_delete.setdefault(model._name, []) context.to_delete.setdefault(model._name, [])
context.to_delete[model._name].append(dummy.id) context.to_delete[model._name].append(dummy.id)
context.dummy_instances.append( context.dummy_instances.append(
dummy_instance(*(context.field_context + (dummy.id,))) dummy_instance(*(context.field_context + (dummy.id,)))
) )
_logger.debug( _logger.debug(
'Created %d as dummy for %s(%d[%d]).%s[%d]', "Created %d as dummy for %s(%d[%d]).%s[%d]",
dummy.id, context.field_context.record_model, dummy.id,
context.field_context.record_model,
context.idmap.get(context.field_context.record_id, 0), context.idmap.get(context.field_context.record_id, 0),
context.field_context.record_id or 0, context.field_context.record_id or 0,
context.field_context.field_name, record['id'], context.field_context.field_name,
record["id"],
) )
return dummy.id return dummy.id
@ -499,35 +516,39 @@ class ImportOdooDatabase(models.Model):
def _run_import_map_values(self, context, data): def _run_import_map_values(self, context, data):
model = self.env[context.model_line.model_id.model] model = self.env[context.model_line.model_id.model]
for field_name in data.keys(): for field_name in data.keys():
if not isinstance( if (
model._fields[field_name], fields._Relational not isinstance(model._fields[field_name], fields._Relational)
) or not data[field_name]: or not data[field_name]
):
continue continue
if model._fields[field_name].type == 'one2many': if model._fields[field_name].type == "one2many":
# don't import one2many fields, use an own configuration # don't import one2many fields, use an own configuration
# for this # for this
data.pop(field_name) data.pop(field_name)
continue continue
ids = data[field_name] if ( ids = (
model._fields[field_name].type != 'many2one' data[field_name]
) else [data[field_name][0]] if (model._fields[field_name].type != "many2one")
else [data[field_name][0]]
)
new_context = context.with_field_context( new_context = context.with_field_context(
model._name, field_name, data['id'] model._name, field_name, data["id"]
) )
comodel = self.env[model._fields[field_name].comodel_name] comodel = self.env[model._fields[field_name].comodel_name]
data[field_name] = [ data[field_name] = [
self._run_import_get_record( self._run_import_get_record(
new_context, comodel, {'id': _id}, new_context,
create_dummy=model._fields[field_name].required or comodel,
any( {"id": _id},
m.model_id.model == comodel._name create_dummy=model._fields[field_name].required
for m in self.import_line_ids or any(
m.model_id.model == comodel._name for m in self.import_line_ids
), ),
) )
for _id in ids for _id in ids
] ]
data[field_name] = filter(None, data[field_name]) data[field_name] = filter(None, data[field_name])
if model._fields[field_name].type == 'many2one': if model._fields[field_name].type == "many2one":
if data[field_name]: if data[field_name]:
data[field_name] = data[field_name] and data[field_name][0] data[field_name] = data[field_name] and data[field_name][0]
else: else:
@ -537,16 +558,16 @@ class ImportOdooDatabase(models.Model):
for mapping in self.import_field_mappings: for mapping in self.import_field_mappings:
if mapping.model_id.model != model._name: if mapping.model_id.model != model._name:
continue continue
if mapping.mapping_type == 'unique': if mapping.mapping_type == "unique":
for field in mapping.field_ids: for field in mapping.field_ids:
value = data.get(field.name, '') value = data.get(field.name, "")
counter = 1 counter = 1
while model.with_context(active_test=False).search([ while model.with_context(active_test=False).search(
(field.name, '=', data.get(field.name, value)), [(field.name, "=", data.get(field.name, value)),]
]): ):
data[field.name] = '%s (%d)' % (value, counter) data[field.name] = "%s (%d)" % (value, counter)
counter += 1 counter += 1
elif mapping.mapping_type == 'by_reference': elif mapping.mapping_type == "by_reference":
res_model = data.get(mapping.model_field_id.name) res_model = data.get(mapping.model_field_id.name)
res_id = data.get(mapping.id_field_id.name) res_id = data.get(mapping.id_field_id.name)
update = { update = {
@ -555,17 +576,21 @@ class ImportOdooDatabase(models.Model):
} }
if res_model in self.env.registry and res_id: if res_model in self.env.registry and res_id:
new_context = context.with_field_context( new_context = context.with_field_context(
model._name, res_id, data['id'] model._name, res_id, data["id"]
) )
record_id = self._run_import_get_record( record_id = self._run_import_get_record(
new_context, self.env[res_model], {'id': res_id}, new_context,
create_dummy=False self.env[res_model],
{"id": res_id},
create_dummy=False,
) )
if record_id: if record_id:
update.update({ update.update(
{
mapping.model_field_id.name: res_model, mapping.model_field_id.name: res_model,
mapping.id_field_id.name: record_id, mapping.id_field_id.name: record_id,
}) }
)
data.update(update) data.update(update)
return data return data
@ -573,15 +598,14 @@ class ImportOdooDatabase(models.Model):
def _run_import_model_get_fields(self, context): def _run_import_model_get_fields(self, context):
return { return {
name: field name: field
for name, field for name, field in self.env[
in self.env[context.model_line.model_id.model]._fields.items() context.model_line.model_id.model
]._fields.items()
if not field.compute or field.inverse if not field.compute or field.inverse
} }
@api.multi @api.multi
def _run_import_model_cleanup_dummies( def _run_import_model_cleanup_dummies(self, context, model, remote_id, local_id):
self, context, model, remote_id, local_id
):
if not (model._name, remote_id) in context.dummies: if not (model._name, remote_id) in context.dummies:
return return
for instance in context.dummy_instances: for instance in context.dummy_instances:
@ -596,69 +620,70 @@ class ImportOdooDatabase(models.Model):
record = record_model.browse(context.idmap[key]) record = record_model.browse(context.idmap[key])
field_name = instance.field_name field_name = instance.field_name
_logger.debug( _logger.debug(
'Replacing dummy %d on %s(%d).%s with %d', "Replacing dummy %d on %s(%d).%s with %d",
dummy_id, record_model._name, record.id, field_name, local_id, dummy_id,
record_model._name,
record.id,
field_name,
local_id,
) )
if record._fields[field_name].type == 'many2one': if record._fields[field_name].type == "many2one":
record.write({field_name: local_id}) record.write({field_name: local_id})
elif record._fields[field_name].type == 'many2many': elif record._fields[field_name].type == "many2many":
record.write({field_name: [ record.write({field_name: [(3, dummy_id), (4, local_id),]})
(3, dummy_id),
(4, local_id),
]})
else: else:
raise exceptions.UserError( raise exceptions.UserError(
_('Unhandled field type %s') % _("Unhandled field type %s") % record._fields[field_name].type
record._fields[field_name].type
) )
context.dummy_instances.remove(instance) context.dummy_instances.remove(instance)
if dummy_id in context.to_delete: if dummy_id in context.to_delete:
model.browse(dummy_id).unlink() model.browse(dummy_id).unlink()
_logger.debug('Deleting dummy %d', dummy_id) _logger.debug("Deleting dummy %d", dummy_id)
if (model._name, remote_id) in context.dummies: if (model._name, remote_id) in context.dummies:
del context.dummies[(model._name, remote_id)] del context.dummies[(model._name, remote_id)]
def _get_connection(self): def _get_connection(self):
self.ensure_one() self.ensure_one()
url = urlparse(self.url) url = urlparse(self.url)
hostport = url.netloc.split(':') hostport = url.netloc.split(":")
if len(hostport) == 1: if len(hostport) == 1:
hostport.append('80') hostport.append("80")
host, port = hostport host, port = hostport
if not odoorpc: # pragma: no cover if not odoorpc: # pragma: no cover
raise exceptions.UserError( raise exceptions.UserError(
_('Please install the "odoorpc" libary in your environment')) _('Please install the "odoorpc" libary in your environment')
)
remote = odoorpc.ODOO( remote = odoorpc.ODOO(
host, host,
protocol='jsonrpc+ssl' if url.scheme == 'https' else 'jsonrpc', protocol="jsonrpc+ssl" if url.scheme == "https" else "jsonrpc",
port=int(port), port=int(port),
) )
remote.login(self.database, self.user, self.password) remote.login(self.database, self.user, self.password)
return remote return remote
@api.constrains('url', 'database', 'user', 'password') @api.constrains("url", "database", "user", "password")
@api.multi @api.multi
def _constrain_url(self): def _constrain_url(self):
for this in self: for this in self:
if this == self.env.ref('base_import_odoo.demodb', False): if this == self.env.ref("base_import_odoo.demodb", False):
continue continue
if tools.config['test_enable']: if tools.config["test_enable"]:
continue continue
if not this.password: if not this.password:
continue continue
this._get_connection() this._get_connection()
@api.depends('status_data') @api.depends("status_data")
@api.multi @api.multi
def _compute_status_html(self): def _compute_status_html(self):
for this in self: for this in self:
if not this.status_data: if not this.status_data:
continue continue
this.status_html = self.env.ref( this.status_html = self.env.ref(
'base_import_odoo.view_import_odoo_database_qweb' "base_import_odoo.view_import_odoo_database_qweb"
).render({'object': this}) ).render({"object": this})
@api.depends('cronjob_id') @api.depends("cronjob_id")
@api.multi @api.multi
def _compute_cronjob_running(self): def _compute_cronjob_running(self):
for this in self: for this in self:
@ -667,9 +692,10 @@ class ImportOdooDatabase(models.Model):
try: try:
with self.env.cr.savepoint(): with self.env.cr.savepoint():
self.env.cr.execute( self.env.cr.execute(
'select id from "%s" where id=%%s for update nowait' % 'select id from "%s" where id=%%s for update nowait'
self.env['ir.cron']._table, % self.env["ir.cron"]._table,
(this.cronjob_id.id,), log_exceptions=False, (this.cronjob_id.id,),
log_exceptions=False,
) )
except psycopg2.OperationalError: except psycopg2.OperationalError:
this.cronjob_running = True this.cronjob_running = True
@ -677,17 +703,19 @@ class ImportOdooDatabase(models.Model):
@api.multi @api.multi
def _create_cronjob(self): def _create_cronjob(self):
self.ensure_one() self.ensure_one()
return self.env['ir.cron'].create({ return self.env["ir.cron"].create(
'name': self.display_name, {
'model': self._name, "name": self.display_name,
'function': '_run_import_cron', "model": self._name,
'doall': True, "function": "_run_import_cron",
'args': str((self.ids,)), "doall": True,
}) "args": str((self.ids,)),
}
)
@api.multi @api.multi
def name_get(self): def name_get(self):
return [ return [
(this.id, '%s@%s, %s' % (this.user, this.url, this.database)) (this.id, "{}@{}, {}".format(this.user, this.url, this.database))
for this in self for this in self
] ]

View File

@ -1,68 +1,70 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from odoo import api, fields, models from odoo import api, fields, models
class ImportOdooDatabaseField(models.Model): class ImportOdooDatabaseField(models.Model):
_name = 'import.odoo.database.field' _name = "import.odoo.database.field"
_description = 'A field mapping for records in the remote database' _description = "A field mapping for records in the remote database"
_order = 'database_id, sequence' _order = "database_id, sequence"
sequence = fields.Integer() sequence = fields.Integer()
database_id = fields.Many2one( database_id = fields.Many2one(
'import.odoo.database', string='Database', required=True, "import.odoo.database", string="Database", required=True, ondelete="cascade",
ondelete='cascade',
) )
model_id = fields.Many2one( model_id = fields.Many2one(
'ir.model', string='Model', required=True, ondelete='cascade', "ir.model", string="Model", required=True, ondelete="cascade",
) )
model = fields.Char(related=['model_id', 'model']) model = fields.Char(related=["model_id", "model"])
field_ids = fields.Many2many( field_ids = fields.Many2many(
'ir.model.fields', string='Field', help='If set, the mapping is only ' "ir.model.fields",
'effective when setting said field', ondelete='cascade', string="Field",
help="If set, the mapping is only " "effective when setting said field",
ondelete="cascade",
) )
model_field_id = fields.Many2one( model_field_id = fields.Many2one(
'ir.model.fields', string='Model field', compute=lambda self: "ir.model.fields",
self._compute_reference_field('model_field_id', 'char'), string="Model field",
inverse=lambda self: compute=lambda self: self._compute_reference_field("model_field_id", "char"),
self._inverse_reference_field('model_field_id', 'char'), inverse=lambda self: self._inverse_reference_field("model_field_id", "char"),
) )
id_field_id = fields.Many2one( id_field_id = fields.Many2one(
'ir.model.fields', string='ID field', compute=lambda self: "ir.model.fields",
self._compute_reference_field('id_field_id', 'integer'), string="ID field",
inverse=lambda self: compute=lambda self: self._compute_reference_field("id_field_id", "integer"),
self._inverse_reference_field('id_field_id', 'integer'), inverse=lambda self: self._inverse_reference_field("id_field_id", "integer"),
) )
# TODO: create a reference function field to set this conveniently # TODO: create a reference function field to set this conveniently
local_id = fields.Integer( local_id = fields.Integer(
'Local ID', help='If you leave this empty, a new record will be ' "Local ID",
'created in the local database when this field is set on the remote ' help="If you leave this empty, a new record will be "
'database' "created in the local database when this field is set on the remote "
"database",
) )
remote_id = fields.Integer( remote_id = fields.Integer(
'Remote ID', help='If you leave this empty, every (set) field value ' "Remote ID",
'will be mapped to the local ID' help="If you leave this empty, every (set) field value "
"will be mapped to the local ID",
) )
mapping_type = fields.Selection( mapping_type = fields.Selection(
[ [
('fixed', 'Fixed'), ("fixed", "Fixed"),
('by_field', 'Based on equal fields'), ("by_field", "Based on equal fields"),
('by_reference', 'By reference'), ("by_reference", "By reference"),
('unique', 'Unique'), ("unique", "Unique"),
], ],
string='Type', required=True, default='fixed', string="Type",
required=True,
default="fixed",
) )
@api.multi @api.multi
def _compute_reference_field(self, field_name, ttype): def _compute_reference_field(self, field_name, ttype):
for this in self: for this in self:
this[field_name] = this.field_ids.filtered( this[field_name] = this.field_ids.filtered(lambda x: x.ttype == ttype)
lambda x: x.ttype == ttype
)
@api.multi @api.multi
def _inverse_reference_field(self, field_name, ttype): def _inverse_reference_field(self, field_name, ttype):
self.field_ids = self.field_ids.filtered( self.field_ids = (
lambda x: x.ttype != ttype self.field_ids.filtered(lambda x: x.ttype != ttype) + self[field_name]
) + self[field_name] )

View File

@ -1,25 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from odoo import fields, models from odoo import fields, models
class ImportOdooDatabaseModel(models.Model): class ImportOdooDatabaseModel(models.Model):
_name = 'import.odoo.database.model' _name = "import.odoo.database.model"
_description = 'A model to import from a remote database' _description = "A model to import from a remote database"
_order = 'sequence' _order = "sequence"
sequence = fields.Integer() sequence = fields.Integer()
model_id = fields.Many2one( model_id = fields.Many2one(
'ir.model', string='Model', required=True, ondelete='cascade', "ir.model", string="Model", required=True, ondelete="cascade",
) )
database_id = fields.Many2one( database_id = fields.Many2one(
'import.odoo.database', string='Database', required=True, "import.odoo.database", string="Database", required=True, ondelete="cascade",
ondelete='cascade',
) )
domain = fields.Char(help='Optional filter to import only a subset') domain = fields.Char(help="Optional filter to import only a subset")
defaults = fields.Char(help='Optional defaults dict to avoid empty values') defaults = fields.Char(help="Optional defaults dict to avoid empty values")
postprocess = fields.Text( postprocess = fields.Text(
help='Optional python code for postprocessing. Your code has access ' help="Optional python code for postprocessing. Your code has access "
'to `vals` which is the dictionary passed to create/write, and `env`.', "to `vals` which is the dictionary passed to create/write, and `env`.",
) )

View File

@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from odoo import fields, models from odoo import fields, models
class IrModelData(models.Model): class IrModelData(models.Model):
_inherit = 'ir.model.data' _inherit = "ir.model.data"
import_database_id = fields.Many2one( import_database_id = fields.Many2one(
'import.odoo.database', string='From remote database', "import.odoo.database", string="From remote database",
) )
import_database_record_id = fields.Integer('Remote database ID') import_database_record_id = fields.Integer("Remote database ID")

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from . import test_base_import_odoo from . import test_base_import_odoo

View File

@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
# Copyright 2017-2018 Therp BV <http://therp.nl> # Copyright 2017-2018 Therp BV <http://therp.nl>
# 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).
from mock import patch from mock import patch
from odoo.tests.common import TransactionCase, post_install, at_install
from odoo.tests.common import TransactionCase, at_install, post_install
from ..models.import_odoo_database import ImportContext, field_context from ..models.import_odoo_database import ImportContext, field_context
@ -11,113 +12,118 @@ class TestBaseImportOdoo(TransactionCase):
super(TestBaseImportOdoo, self).setUp() super(TestBaseImportOdoo, self).setUp()
# if our tests run with an accounting scheme, it will fail on accounts # if our tests run with an accounting scheme, it will fail on accounts
# to fix this, if the account model exists, we create mappings for it # to fix this, if the account model exists, we create mappings for it
if 'account.account' in self.env.registry: if "account.account" in self.env.registry:
self.env.ref('base_import_odoo.demodb').write({ self.env.ref("base_import_odoo.demodb").write(
'import_field_mappings': [
(
0, False,
{ {
'mapping_type': 'fixed', "import_field_mappings": [
'model_id': (
self.env.ref('account.model_account_account').id, 0,
'local_id': account.id, False,
'remote_id': account.id, {
"mapping_type": "fixed",
"model_id": self.env.ref(
"account.model_account_account"
).id,
"local_id": account.id,
"remote_id": account.id,
}, },
) )
for account in self.env['account.account'].search([]) for account in self.env["account.account"].search([])
], ],
}) }
)
@at_install(False) @at_install(False)
@post_install(True) @post_install(True)
@patch( @patch(
'odoorpc.ODOO.__init__', "odoorpc.ODOO.__init__", side_effect=lambda self, *args, **kwargs: None,
side_effect=lambda self, *args, **kwargs: None,
) )
@patch('odoorpc.ODOO.login', side_effect=lambda *args: None) @patch("odoorpc.ODOO.login", side_effect=lambda *args: None)
def test_base_import_odoo(self, mock_client, mock_client_login): 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_execute(model, method, *args): def _mock_execute(model, method, *args):
if method == 'read': if method == "read":
return self.env[model].browse(args[0]).read(args[1]) return self.env[model].browse(args[0]).read(args[1])
if method == 'search': if method == "search":
return self.env[model].search(args[0]).ids 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)
run = 1 run = 1
for dummy in range(2): for dummy in range(2):
# we run this two times to enter the code path where xmlids exist # we run this two times to enter the code path where xmlids exist
self.env.ref('base_import_odoo.demodb').write({ self.env.ref("base_import_odoo.demodb").write(
'password': 'admin', {"password": "admin",}
}) )
with patch('odoorpc.ODOO.execute', side_effect=_mock_execute): with patch("odoorpc.ODOO.execute", side_effect=_mock_execute):
self.env.ref('base_import_odoo.demodb')._run_import() 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
imported_user = self.env.ref(self._get_xmlid('base.user_demo')) imported_user = self.env.ref(self._get_xmlid("base.user_demo"))
user = self.env.ref('base.user_demo') user = self.env.ref("base.user_demo")
self.assertNotEqual(imported_user, user) self.assertNotEqual(imported_user, user)
# check that the imported scalars are equal # check that the imported scalars are equal
fields = ['name', 'email', 'signature', 'active'] fields = ["name", "email", "signature", "active"]
(imported_user + user).read(fields) (imported_user + user).read(fields)
self.assertEqual( self.assertEqual(
self._get_cache(self._get_xmlid('base.user_demo'), fields), self._get_cache(self._get_xmlid("base.user_demo"), fields),
self._get_cache('base.user_demo', fields), self._get_cache("base.user_demo", fields),
) )
# check that links are correctly mapped # check that links are correctly mapped
self.assertEqual( self.assertEqual(
imported_user.partner_id, imported_user.partner_id,
self.env.ref(self._get_xmlid('base.partner_demo')) self.env.ref(self._get_xmlid("base.partner_demo")),
) )
# no new groups because they should be mapped by name # no new groups because they should be mapped by name
self.assertEqual( self.assertEqual(group_count, self.env["res.groups"].search([], count=True))
group_count, self.env['res.groups'].search([], count=True)
)
# all users save for root should be duplicated for every run # all users save for root should be duplicated for every run
self.assertEqual( self.assertEqual(
self.env['res.users'].search([], count=True), self.env["res.users"].search([], count=True),
user_count + (user_count - 1) * run, user_count + (user_count - 1) * run,
) )
# check that there's a new attachment # check that there's a new attachment
attachment = self.env.ref('base_import_odoo.attachment_demo') attachment = self.env.ref("base_import_odoo.attachment_demo")
imported_attachment = self.env['ir.attachment'].search([ imported_attachment = self.env["ir.attachment"].search(
('res_model', '=', 'res.users'), [("res_model", "=", "res.users"), ("res_id", "=", imported_user.id),]
('res_id', '=', imported_user.id), )
])
self.assertTrue(attachment) self.assertTrue(attachment)
self.assertEqual(attachment.datas, imported_attachment.datas) self.assertEqual(attachment.datas, imported_attachment.datas)
self.assertNotEqual(attachment, imported_attachment) self.assertNotEqual(attachment, imported_attachment)
run += 1 run += 1
demodb = self.env.ref('base_import_odoo.demodb') demodb = self.env.ref("base_import_odoo.demodb")
for line in demodb.import_line_ids: for line in demodb.import_line_ids:
self.assertIn(line.model_id.model, demodb.status_html) self.assertIn(line.model_id.model, demodb.status_html)
demodb.action_import() demodb.action_import()
self.assertTrue(demodb.cronjob_id) self.assertTrue(demodb.cronjob_id)
demodb.cronjob_id.write({'active': False}) demodb.cronjob_id.write({"active": False})
demodb.action_import() demodb.action_import()
self.assertTrue(demodb.cronjob_id.active) self.assertTrue(demodb.cronjob_id.active)
self.assertFalse(demodb.cronjob_running) self.assertFalse(demodb.cronjob_running)
# in our setting we won't get dummies, so we test this manually # in our setting we won't get dummies, so we test this manually
import_context = ImportContext( import_context = ImportContext(
None, self.env.ref('base_import_odoo.model_partner'), None,
[], {}, {}, [], {}, field_context(None, None, None) self.env.ref("base_import_odoo.model_partner"),
[],
{},
{},
[],
{},
field_context(None, None, None),
) )
dummy_id = demodb._run_import_create_dummy( dummy_id = demodb._run_import_create_dummy(
import_context, self.env['res.partner'], {'id': 424242}, import_context, self.env["res.partner"], {"id": 424242}, forcecreate=True,
forcecreate=True,
) )
self.assertTrue(self.env['res.partner'].browse(dummy_id).exists()) self.assertTrue(self.env["res.partner"].browse(dummy_id).exists())
def _get_xmlid(self, remote_xmlid): def _get_xmlid(self, remote_xmlid):
remote_obj = self.env.ref(remote_xmlid) remote_obj = self.env.ref(remote_xmlid)
return 'base_import_odoo.%d-%s-%s' % ( return "base_import_odoo.%d-%s-%s" % (
self.env.ref('base_import_odoo.demodb').id, self.env.ref("base_import_odoo.demodb").id,
remote_obj._name.replace('.', '_'), remote_obj._name.replace(".", "_"),
remote_obj.id, remote_obj.id,
) )

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<record id="view_import_odoo_database_tree" model="ir.ui.view"> <record id="view_import_odoo_database_tree" model="ir.ui.view">
<field name="model">import.odoo.database</field> <field name="model">import.odoo.database</field>
@ -14,32 +14,73 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<form> <form>
<header> <header>
<button type="object" name="action_import" string="Run import" class="oe_highlight" attrs="{'invisible': [('cronjob_running', '=', True)]}" /> <button
<button type="object" name="exists" string="Refresh" class="oe_highlight" attrs="{'invisible': [('cronjob_running', '=', False)]}" /> type="object"
name="action_import"
string="Run import"
class="oe_highlight"
attrs="{'invisible': [('cronjob_running', '=', True)]}"
/>
<button
type="object"
name="exists"
string="Refresh"
class="oe_highlight"
attrs="{'invisible': [('cronjob_running', '=', False)]}"
/>
<field name="cronjob_running" invisible="1" /> <field name="cronjob_running" invisible="1" />
</header> </header>
<sheet> <sheet>
<field name="status_html" attrs="{'invisible': [('status_html', '=', False)]}" /> <field
name="status_html"
attrs="{'invisible': [('status_html', '=', False)]}"
/>
<group col="4" name="credentials"> <group col="4" name="credentials">
<field name="url" widget="url" attrs="{'readonly': [('cronjob_running', '=', True)]}" /> <field
<field name="database" attrs="{'readonly': [('cronjob_running', '=', True)]}" /> name="url"
<field name="user" attrs="{'readonly': [('cronjob_running', '=', True)]}" /> widget="url"
<field name="password" password="True" attrs="{'readonly': [('cronjob_running', '=', True)], 'required': [('cronjob_running', '=', False)]}" /> attrs="{'readonly': [('cronjob_running', '=', True)]}"
/>
<field
name="database"
attrs="{'readonly': [('cronjob_running', '=', True)]}"
/>
<field
name="user"
attrs="{'readonly': [('cronjob_running', '=', True)]}"
/>
<field
name="password"
password="True"
attrs="{'readonly': [('cronjob_running', '=', True)], 'required': [('cronjob_running', '=', False)]}"
/>
<field name="duplicates" /> <field name="duplicates" />
<field name="cronjob_id" attrs="{'invisible': [('cronjob_id', '=', False)]}" /> <field
name="cronjob_id"
attrs="{'invisible': [('cronjob_id', '=', False)]}"
/>
</group> </group>
<label for="import_line_ids" /> <label for="import_line_ids" />
<field name="import_line_ids" attrs="{'readonly': [('cronjob_running', '=', True)]}"> <field
name="import_line_ids"
attrs="{'readonly': [('cronjob_running', '=', True)]}"
>
<tree editable="top"> <tree editable="top">
<field name="sequence" widget="handle" /> <field name="sequence" widget="handle" />
<field name="model_id" /> <field name="model_id" />
<field name="domain" /> <field name="domain" />
<field name="defaults" /> <field name="defaults" />
<field name="postprocess" placeholder="# `vals` is the dictionary to be passed to create/write, and you have access to `env`, `_id`, `remote`" /> <field
name="postprocess"
placeholder="# `vals` is the dictionary to be passed to create/write, and you have access to `env`, `_id`, `remote`"
/>
</tree> </tree>
</field> </field>
<label for="import_field_mappings" /> <label for="import_field_mappings" />
<field name="import_field_mappings" attrs="{'readonly': [('cronjob_running', '=', True)]}"/> <field
name="import_field_mappings"
attrs="{'readonly': [('cronjob_running', '=', True)]}"
/>
</sheet> </sheet>
</form> </form>
</field> </field>
@ -71,16 +112,29 @@
<h2 t-if="object.cronjob_running">Import progress</h2> <h2 t-if="object.cronjob_running">Import progress</h2>
<h2 t-if="not object.cronjob_running">Import results</h2> <h2 t-if="not object.cronjob_running">Import results</h2>
<div style="display: flex; flex-flow: row wrap"> <div style="display: flex; flex-flow: row wrap">
<div style="margin-right: .5em" t-foreach="object.import_line_ids" t-as="import_line"> <div
style="margin-right: .5em"
t-foreach="object.import_line_ids"
t-as="import_line"
>
<t t-set="model_name" t-value="import_line.model_id.model" /> <t t-set="model_name" t-value="import_line.model_id.model" />
<t t-set="model_display_name" t-value="import_line.model_id.name" /> <t t-set="model_display_name" t-value="import_line.model_id.name" />
<h3 t-esc="model_display_name" /> <h3 t-esc="model_display_name" />
<a href="#" t-att-onclick="'base_import_database_open(&quot;%s&quot;, &quot;%s&quot;, %s)' % (model_name, model_display_name, object.id)"> <a
<span t-esc="object.status_data.get('done', {}).get(model_name, 0)" />/<span t-esc="object.status_data.get('counts', {}).get(model_name, 0)" /> done href="#"
t-att-onclick="'base_import_database_open(&quot;%s&quot;, &quot;%s&quot;, %s)' % (model_name, model_display_name, object.id)"
>
<span
t-esc="object.status_data.get('done', {}).get(model_name, 0)"
/>/<span
t-esc="object.status_data.get('counts', {}).get(model_name, 0)"
/> done
</a> </a>
</div> </div>
</div> </div>
<t t-if="object.status_data.get('error')"><pre t-esc="object.status_data['error']" /></t> <t t-if="object.status_data.get('error')">
<pre t-esc="object.status_data['error']" />
</t>
<div t-if="object.status_data.get('dummies')"> <div t-if="object.status_data.get('dummies')">
The following remote ids don't have a mapping but have to be imported anyways due to not null constraints. The following remote ids don't have a mapping but have to be imported anyways due to not null constraints.
<dl> <dl>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<record id="view_import_odoo_database_field_form" model="ir.ui.view"> <record id="view_import_odoo_database_field_form" model="ir.ui.view">
<field name="model">import.odoo.database.field</field> <field name="model">import.odoo.database.field</field>
@ -9,24 +9,55 @@
<field name="model" invisible="True" /> <field name="model" invisible="True" />
<field name="mapping_type" /> <field name="mapping_type" />
</group> </group>
<div attrs="{'invisible': [('mapping_type', '!=', 'fixed')]}" class="oe_edit_only"> <div
attrs="{'invisible': [('mapping_type', '!=', 'fixed')]}"
class="oe_edit_only"
>
When a record of this model is imported, it will be replaced with the record you select as local ID. If you select a field and/or a remote ID, this replacement is only effective when setting the specified field and/or when the remote value is the specified record. When a record of this model is imported, it will be replaced with the record you select as local ID. If you select a field and/or a remote ID, this replacement is only effective when setting the specified field and/or when the remote value is the specified record.
</div> </div>
<div attrs="{'invisible': [('mapping_type', '!=', 'by_field')]}" class="oe_edit_only"> <div
attrs="{'invisible': [('mapping_type', '!=', 'by_field')]}"
class="oe_edit_only"
>
Select fields which must be equal to treat a pair of remote and local records of this model as being equal. Select fields which must be equal to treat a pair of remote and local records of this model as being equal.
</div> </div>
<div attrs="{'invisible': [('mapping_type', '!=', 'by_reference')]}" class="oe_edit_only"> <div
attrs="{'invisible': [('mapping_type', '!=', 'by_reference')]}"
class="oe_edit_only"
>
Select the field that stores the model name and the one that stores the linked ID. The IDs for previously imported records will be mapped to the local ID, for unknown models or IDs, the fields are set to NULL. Select the field that stores the model name and the one that stores the linked ID. The IDs for previously imported records will be mapped to the local ID, for unknown models or IDs, the fields are set to NULL.
</div> </div>
<div attrs="{'invisible': [('mapping_type', '!=', 'unique')]}" class="oe_edit_only"> <div
attrs="{'invisible': [('mapping_type', '!=', 'unique')]}"
class="oe_edit_only"
>
Select fields for which to generate unique values during import. You'll need this for res.users#login for example. Select fields for which to generate unique values during import. You'll need this for res.users#login for example.
</div> </div>
<group> <group>
<field name="local_id" attrs="{'invisible': [('mapping_type', '!=', 'fixed')], 'required': [('mapping_type', '=', 'fixed')]}" /> <field
<field name="remote_id" attrs="{'invisible': [('mapping_type', '!=', 'fixed')]}" /> name="local_id"
<field name="field_ids" attrs="{'invisible': [('mapping_type', 'not in', ['fixed', 'by_field', 'unique'])], 'required': [('mapping_type', 'in', ['by_field', 'unique'])]}" widget="many2many_tags" domain="[mapping_type == 'fixed' and ('relation', '=', model) or ('model_id', '=', model_id)]"/> attrs="{'invisible': [('mapping_type', '!=', 'fixed')], 'required': [('mapping_type', '=', 'fixed')]}"
<field name="model_field_id" attrs="{'invisible': [('mapping_type', '!=', 'by_reference')], 'required': [('mapping_type', '=', 'by_reference')]}" domain="[('ttype', '=', 'char'), ('model_id.model', '=', model)]" /> />
<field name="id_field_id" attrs="{'invisible': [('mapping_type', '!=', 'by_reference')], 'required': [('mapping_type', '=', 'by_reference')]}" domain="[('ttype', '=', 'integer'), ('model_id.model', '=', model)]" /> <field
name="remote_id"
attrs="{'invisible': [('mapping_type', '!=', 'fixed')]}"
/>
<field
name="field_ids"
attrs="{'invisible': [('mapping_type', 'not in', ['fixed', 'by_field', 'unique'])], 'required': [('mapping_type', 'in', ['by_field', 'unique'])]}"
widget="many2many_tags"
domain="[mapping_type == 'fixed' and ('relation', '=', model) or ('model_id', '=', model_id)]"
/>
<field
name="model_field_id"
attrs="{'invisible': [('mapping_type', '!=', 'by_reference')], 'required': [('mapping_type', '=', 'by_reference')]}"
domain="[('ttype', '=', 'char'), ('model_id.model', '=', model)]"
/>
<field
name="id_field_id"
attrs="{'invisible': [('mapping_type', '!=', 'by_reference')], 'required': [('mapping_type', '=', 'by_reference')]}"
domain="[('ttype', '=', 'integer'), ('model_id.model', '=', model)]"
/>
</group> </group>
</form> </form>
</field> </field>

View File

@ -1,10 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<menuitem id="menu_main" parent="base.menu_administration" name="Remote Odoo import" sequence="5" /> <menuitem
<act_window id="action_import_odoo_database" id="menu_main"
parent="base.menu_administration"
name="Remote Odoo import"
sequence="5"
/>
<act_window
id="action_import_odoo_database"
res_model="import.odoo.database" res_model="import.odoo.database"
name="Import configurations" name="Import configurations"
view_mode="tree,form" view_mode="tree,form"
/> />
<menuitem id="menu_import_odoo_database" action="action_import_odoo_database" parent="menu_main" /> <menuitem
id="menu_import_odoo_database"
action="action_import_odoo_database"
parent="menu_main"
/>
</odoo> </odoo>