Browse Source

Merge pull request #988 from hbrunn/8.0-base_import_odoo

[ADD] base_import_odoo
pull/837/merge
Daniel Reis 7 years ago
committed by GitHub
parent
commit
81cdd4da4d
  1. 98
      base_import_odoo/README.rst
  2. 4
      base_import_odoo/__init__.py
  3. 32
      base_import_odoo/__openerp__.py
  4. 11
      base_import_odoo/demo/import_odoo_database.xml
  5. 72
      base_import_odoo/demo/import_odoo_database_field.xml
  6. 35
      base_import_odoo/demo/import_odoo_database_model.xml
  7. 11
      base_import_odoo/demo/ir_attachment.xml
  8. 8
      base_import_odoo/demo/res_partner.xml
  9. 9
      base_import_odoo/demo/res_users.xml
  10. 7
      base_import_odoo/models/__init__.py
  11. 637
      base_import_odoo/models/import_odoo_database.py
  12. 68
      base_import_odoo/models/import_odoo_database_field.py
  13. 20
      base_import_odoo/models/import_odoo_database_model.py
  14. 13
      base_import_odoo/models/ir_model_data.py
  15. 4
      base_import_odoo/security/ir.model.access.csv
  16. BIN
      base_import_odoo/static/description/icon.png
  17. 4
      base_import_odoo/tests/__init__.py
  18. 129
      base_import_odoo/tests/test_base_import_odoo.py
  19. 93
      base_import_odoo/views/import_odoo_database.xml
  20. 47
      base_import_odoo/views/import_odoo_database_field.xml
  21. 12
      base_import_odoo/views/menu.xml
  22. 1
      requirements.txt

98
base_import_odoo/README.rst

@ -0,0 +1,98 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
================================
Import from remote Odoo database
================================
This module was written to import data from another Odoo database. The idea is that you define which models to import from the other database, and add eventual mappings for records you don't want to import.
Use cases
=========
- merging databases
- one way sync
- aggregating management data from distributed systems
Configuration
=============
Go to Settings / Remote Odoo import / Import configurations and create a configuration.
After filling in your credentials, select models you want to import from the remote database. If you only want to import a subset of the records, add an appropriate domain.
The import will copy records of all models listed, and handle links to records of models which are not imported depending on the existing field mappings. Field mappings to local records also are a stopping condition. Without those, the import will have to create some record for all required x2x fields, which you probably don't want.
Probably you'll want to map records of model `res.company`, and at least the admin user.
The module doesn't import one2many fields, if you want to have those, add the model the field in question points to to the list of imported models, possibly with a domain.
If you don't fill in a remote ID, the addon will use the configured local ID for every record of the model (this way, you can for example map all users in the remote system to some import user in the current system).
For fields that have a uniqueness constraint (like `res.users#login`), create a field mapping if type `unique`, then the import will generate a unique value for this field.
For models using references with two fields (like `ir.attachment`), create a field mapping of type `by reference` and select the two fields involved. The import will transform known ids (=ids of models you import) to the respective local id, and clean out the model/id fields for unknown models/ids.
Usage
=====
To use this module, you need to:
#. go to an import configuration and hit the button ``Run import``
#. be patient, this creates a cronjob which will start up to a minutes afterwards
#. reload the form, as soon as the cronjob runs you'll see a field ``Progress`` that lets you inspect what was imported already
#. note that the cronjob also resets the password as soon as it has read it. So for a subsequent import, you'll have to fill it in again
#. running an import a second time won't duplicate data, it should recognize records imported earlier and just update them
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/149/8.0
Known issues / Roadmap
======================
* Yes of course this duplicates a lot of connector functionality. Rewrite this with the connector framework, probably collaborate with https://github.com/OCA/connector-odoo2odoo
* Do something with workflows
* Support reference fields, while being at it refactor _run_import_map_values to call a function per field type
* Probably it's safer and faster to disable recomputation during import, and recompute all fields afterwards
* Add duplicate handling strategy 'Overwrite older'
Bug Tracker
===========
Bugs are tracked on `GitHub Issues
<https://github.com/OCA/server-tools/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.
Credits
=======
Images
------
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
Contributors
------------
* Holger Brunn <hbrunn@therp.nl>
Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list <mailto:community@mail.odoo.com>`_ or the `appropriate specialized mailinglist <https://odoo-community.org/groups>`_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues.
Maintainer
----------
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org
This module is maintained by the OCA.
OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.
To contribute to this module, please visit https://odoo-community.org.

4
base_import_odoo/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import models

32
base_import_odoo/__openerp__.py

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Import from Odoo",
"version": "8.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Tools",
"summary": "Import records from another Odoo instance",
"depends": [
'mail',
],
"demo": [
"demo/res_partner.xml",
"demo/res_users.xml",
"demo/ir_attachment.xml",
"demo/import_odoo_database.xml",
"demo/import_odoo_database_field.xml",
"demo/import_odoo_database_model.xml",
],
"data": [
"views/import_odoo_database_field.xml",
"security/ir.model.access.csv",
"views/import_odoo_database.xml",
"views/menu.xml",
],
"installable": True,
"external_dependencies": {
"python": ['odoorpc'],
},
}

11
base_import_odoo/demo/import_odoo_database.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data noupdate="1">
<record id="demodb" model="import.odoo.database">
<field name="url">http://localhost:8069</field>
<field name="database">demodb</field>
<field name="user">admin</field>
<field name="password" eval="False" />
</record>
</data>
</openerp>

72
base_import_odoo/demo/import_odoo_database_field.xml

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data noupdate="1">
<record id="mapping_partner_id_root" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">fixed</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="local_id" ref="base.partner_root" />
<field name="remote_id" ref="base.partner_root" />
</record>
<record id="mapping_partner_id_company" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">fixed</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="local_id" ref="base.main_partner" />
<field name="remote_id" ref="base.main_partner" />
</record>
<record id="mapping_partner_id_public" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">fixed</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="local_id" ref="base.public_partner" />
<field name="remote_id" ref="base.public_partner" />
</record>
<record id="mapping_user_root" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">fixed</field>
<field name="model_id" ref="base.model_res_users" />
<field name="local_id" ref="mapped_admin" />
<field name="remote_id" ref="base.user_root" />
</record>
<record id="mapping_user_public" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">fixed</field>
<field name="model_id" ref="base.model_res_users" />
<field name="local_id" ref="base.public_user" />
<field name="remote_id" ref="base.public_user" />
</record>
<record id="mapping_company_id" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">fixed</field>
<field name="model_id" ref="base.model_res_company" />
<field name="local_id" ref="base.main_company" />
<field name="remote_id" ref="base.main_company" />
</record>
<record id="mapping_login" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">unique</field>
<field name="model_id" ref="base.model_res_users" />
<field name="field_ids" eval="[(4, ref('base.field_res_users_login'))]" />
</record>
<record id="mapping_models" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">by_field</field>
<field name="model_id" ref="base.model_ir_model" />
<field name="field_ids" eval="[(4, ref('base.field_ir_model_name'))]" />
</record>
<record id="mapping_groups" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">by_field</field>
<field name="model_id" ref="base.model_res_groups" />
<field name="field_ids" eval="[(4, ref('base.field_res_groups_name'))]" />
</record>
<record id="mapping_attachment" model="import.odoo.database.field">
<field name="database_id" ref="demodb" />
<field name="mapping_type">by_reference</field>
<field name="model_id" ref="base.model_ir_attachment" />
<field name="model_field_id" ref="base.field_ir_attachment_res_model" />
<field name="id_field_id" ref="base.field_ir_attachment_res_id" />
</record>
</data>
</openerp>

35
base_import_odoo/demo/import_odoo_database_model.xml

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data noupdate="1">
<record id="model_partner" model="import.odoo.database.model">
<field name="sequence">1</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="database_id" ref="demodb" />
<field name="domain">[(1, '=', 1)]</field>
</record>
<record id="model_alias" model="import.odoo.database.model">
<field name="sequence">2</field>
<field name="model_id" ref="mail.model_mail_alias" />
<field name="database_id" ref="demodb" />
<field name="domain">[(1, '=', 1)]</field>
</record>
<record id="model_users" model="import.odoo.database.model">
<field name="sequence">3</field>
<field name="model_id" ref="base.model_res_users" />
<field name="database_id" ref="demodb" />
<field name="domain">[(1, '=', 1)]</field>
</record>
<record id="model_groups" model="import.odoo.database.model">
<field name="sequence">4</field>
<field name="model_id" ref="base.model_res_groups" />
<field name="database_id" ref="demodb" />
<field name="domain">[(1, '=', 1)]</field>
</record>
<record id="model_attachment" model="import.odoo.database.model">
<field name="sequence">5</field>
<field name="model_id" ref="base.model_ir_attachment" />
<field name="database_id" ref="demodb" />
<field name="domain">[('res_model', 'in', ['res.users'])]</field>
</record>
</data>
</openerp>

11
base_import_odoo/demo/ir_attachment.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="attachment_demo" model="ir.attachment">
<field name="name">Demo attachment</field>
<field name="res_model">res.users</field>
<field name="res_id" ref="base.user_demo" />
<field name="datas">aGVsbG8gd29ybGQK</field>
</record>
</data>
</openerp>

8
base_import_odoo/demo/res_partner.xml

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

9
base_import_odoo/demo/res_users.xml

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

7
base_import_odoo/models/__init__.py

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import ir_model_data
from . import import_odoo_database
from . import import_odoo_database_model
from . import import_odoo_database_field

637
base_import_odoo/models/import_odoo_database.py

@ -0,0 +1,637 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging
try:
import odoorpc
except:
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
_logger = logging.getLogger('base_import_odoo')
import_context_tuple = namedtuple(
'import_context', [
'remote', 'model_line', 'ids', 'idmap', 'dummies', 'dummy_instances',
'to_delete', 'field_context',
]
)
class ImportContext(import_context_tuple):
def with_field_context(self, *args):
return ImportContext(*(self[:-1] + (field_context(*args),)))
field_context = namedtuple(
'field_context', ['record_model', 'field_name', 'record_id'],
)
mapping_key = namedtuple('mapping_key', ['model_name', 'remote_id'])
dummy_instance = namedtuple(
'dummy_instance', ['model_name', 'field_name', 'remote_id', 'dummy_id'],
)
class ImportOdooDatabase(models.Model):
_name = 'import.odoo.database'
_description = 'An Odoo database to import'
url = fields.Char(required=True)
database = fields.Char(required=True)
user = fields.Char(default='admin', required=True)
password = fields.Char(default='admin')
import_line_ids = fields.One2many(
'import.odoo.database.model', 'database_id', string='Import models',
)
import_field_mappings = fields.One2many(
'import.odoo.database.field', 'database_id', string='Field mappings',
)
cronjob_id = fields.Many2one(
'ir.cron', string='Import job', readonly=True, copy=False,
)
cronjob_running = fields.Boolean(compute='_compute_cronjob_running')
status_data = fields.Serialized('Status', readonly=True, copy=False)
status_html = fields.Html(
compute='_compute_status_html', readonly=True, sanitize=False,
)
duplicates = fields.Selection(
[
('skip', 'Skip existing'), ('overwrite', 'Overwrite existing'),
('overwrite_empty', 'Overwrite empty fields'),
],
'Duplicate handling', default='skip', required=True,
)
@api.multi
def action_import(self):
"""Create a cronjob to run the actual import"""
self.ensure_one()
if self.cronjob_id:
return self.cronjob_id.write({
'numbercall': 1,
'doall': True,
'active': True,
})
return self.write({
'cronjob_id': self._create_cronjob().id,
})
@api.multi
def _run_import(self, commit=True, commit_threshold=100):
"""Run the import as cronjob, commit often"""
self.ensure_one()
if not self.password:
return
# model name: [ids]
remote_ids = {}
# model name: count
remote_counts = {}
# model name: count
done = {}
# mapping_key: local_id
idmap = {}
# mapping_key: local_id
# this are records created or linked when we need to fill a required
# field, but the local record is not yet created
dummies = {}
# model name: [local_id]
# this happens when we create a dummy we can throw away again
to_delete = {}
# dummy_instance
dummy_instances = []
remote = self._get_connection()
self.write({'password': False})
if commit and not tools.config['test_enable']:
# pylint: disable=invalid-commit
self.env.cr.commit()
for model_line in self.import_line_ids:
model = model_line.model_id
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])
self.write({
'status_data': {
'counts': remote_counts,
'ids': remote_ids,
'error': None,
'dummies': None,
'done': {},
}
})
if commit and not tools.config['test_enable']:
# pylint: disable=invalid-commit
self.env.cr.commit()
for model_line in self.import_line_ids:
model = self.env[model_line.model_id.model]
done[model._name] = 0
chunk_len = commit and (commit_threshold or 1) or len(
remote_ids[model._name]
)
for start_index in range(
len(remote_ids[model._name]) / chunk_len + 1
):
index = start_index * chunk_len
ids = remote_ids[model._name][index:index + chunk_len]
context = ImportContext(
remote, model_line, ids, idmap, dummies, dummy_instances,
to_delete, field_context(None, None, None),
)
try:
self._run_import_model(context)
except:
error = traceback.format_exc()
self.env.cr.rollback()
self.write({
'status_data': dict(self.status_data, error=error),
})
# pylint: disable=invalid-commit
self.env.cr.commit()
raise
done[model._name] += len(ids)
self.write({'status_data': dict(self.status_data, done=done)})
if commit and not tools.config['test_enable']:
# pylint: disable=invalid-commit
self.env.cr.commit()
missing = {}
for dummy_model, remote_id in dummies.keys():
if remote_id:
missing.setdefault(dummy_model, []).append(remote_id)
self.write({
'status_data': dict(self.status_data, dummies=dict(missing)),
})
@api.multi
def _run_import_model(self, context):
"""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.execute(
model._name, 'read', context.ids, fields.keys()
):
self._run_import_get_record(
context, model, data, create_dummy=False,
)
if (model._name, data['id']) in context.idmap:
if self.duplicates == 'skip':
# there's a mapping for this record, nothing to do
continue
data = self._run_import_map_values(context, data)
_id = data['id']
record = self._create_record(context, model, data)
self._run_import_model_cleanup_dummies(
context, model, _id, record.id,
)
@api.multi
def _create_record(self, context, model, record):
"""Create a record, add an xmlid"""
_id = record.pop('id')
xmlid = '%d-%s-%d' % (
self.id, model._name.replace('.', '_'), _id or 0,
)
new = self.env.ref('base_import_odoo.%s' % xmlid, False)
if new and new.exists():
if self.duplicates == 'overwrite_empty':
record = {
key: value
for key, value in record.items()
if not new[key]
}
new.with_context(
**self._create_record_context(model, record)
).write(record)
_logger.debug('Updated record %s', xmlid)
else:
new = model.with_context(
**self._create_record_context(model, record)
).create(record)
self.env['ir.model.data'].create({
'name': xmlid,
'model': model._name,
'module': 'base_import_odoo',
'res_id': new.id,
'noupdate': True,
'import_database_id': self.id,
'import_database_record_id': _id,
})
_logger.debug('Created record %s', xmlid)
context.idmap[mapping_key(model._name, _id)] = new.id
return new
def _create_record_context(self, model, record):
"""Return a context that is used when creating a record"""
context = {
'tracking_disable': True,
}
if model._name == 'res.users':
context['no_reset_password'] = True
return context
@api.multi
def _run_import_get_record(
self, context, model, record, create_dummy=True,
):
"""Find the local id of some remote record. Create a dummy if not
available"""
_id = context.idmap.get((model._name, record['id']))
logged = False
if not _id:
_id = context.dummies.get((model._name, record['id']))
if _id:
context.dummy_instances.append(
dummy_instance(*(context.field_context + (_id,)))
)
else:
logged = True
_logger.debug(
'Got %s(%d[%d]) from idmap', model._model, _id,
record['id'] or 0,
)
if not _id:
_id = self._run_import_get_record_mapping(
context, model, record, create_dummy=create_dummy,
)
elif not logged:
logged = True
_logger.debug(
'Got %s(%d[%d]) from dummies', model._model, _id, record['id'],
)
if not _id:
xmlid = self.env['ir.model.data'].search([
('import_database_id', '=', self.id),
('import_database_record_id', '=', record['id']),
('model', '=', model._name),
], limit=1)
if xmlid:
_id = xmlid.res_id
context.idmap[(model._name, record['id'])] = _id
elif not logged:
logged = True
_logger.debug(
'Got %s(%d[%d]) from mappings',
model._model, _id, record['id'],
)
if not _id and create_dummy:
_id = self._run_import_create_dummy(
context, model, record,
forcecreate=record['id'] not in
self.status_data['ids'].get(model._name, [])
)
elif _id and not logged:
_logger.debug(
'Got %s(%d[%d]) from xmlid', model._model, _id, record['id'],
)
return _id
@api.multi
def _run_import_get_record_mapping(
self, context, model, record, create_dummy=True,
):
current_field = self.env['ir.model.fields'].search([
('name', '=', context.field_context.field_name),
('model_id.model', '=', context.field_context.record_model),
])
mappings = self.import_field_mappings.filtered(
lambda x:
x.mapping_type == 'fixed' and
x.model_id.model == model._name and
(
not x.field_ids or current_field in x.field_ids
) and x.local_id and
(x.remote_id == record['id'] or not x.remote_id) or
x.mapping_type == 'by_field' and
x.model_id.model == model._name
)
_id = None
for mapping in mappings:
if mapping.mapping_type == 'fixed':
assert mapping.local_id
_id = mapping.local_id
context.idmap[(model._name, record['id'])] = _id
break
elif mapping.mapping_type == 'by_field':
assert mapping.field_ids
if len(record) == 1:
# just the id of a record we haven't seen yet.
# read the whole record from remote to check if
# this can be mapped to an existing record
record = context.remote.execute(
model._name, 'read', record['id'],
mapping.field_ids.mapped('name'),
) or None
if not record:
continue
if isinstance(record, list):
record = record[0]
domain = [
(field.name, '=', record.get(field.name))
for field in mapping.field_ids
if record.get(field.name)
]
if len(domain) < len(mapping.field_ids):
# play it save, only use mapping if we really select
# something specific
continue
records = model.with_context(active_test=False).search(
domain, limit=1,
)
if records:
_id = records.id
context.idmap[(model._name, record['id'])] = _id
break
else:
raise exceptions.UserError(_('Unknown mapping'))
return _id
@api.multi
def _run_import_create_dummy(
self, context, model, record, forcecreate=False,
):
"""Either misuse some existing record or create an empty one to satisfy
required links"""
dummy = model.search([
(
'id', 'not in',
[
v for (model_name, remote_id), v
in context.dummies.items()
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
]
),
], limit=1)
if dummy and not forcecreate:
context.dummies[mapping_key(model._name, record['id'])] = dummy.id
context.dummy_instances.append(
dummy_instance(*(context.field_context + (dummy.id,)))
)
_logger.debug(
'Using %d as dummy for %s(%d[%d]).%s[%d]',
dummy.id, context.field_context.record_model,
context.idmap.get(context.field_context.record_id, 0),
context.field_context.record_id,
context.field_context.field_name, record['id'],
)
return dummy.id
required = [
name
for name, field in model._fields.items()
if field.required
]
defaults = model.default_get(required)
values = {'id': record['id']}
for name, field in model._fields.items():
if name not in required or defaults.get(name):
continue
value = None
if field.type in ['char', 'text', 'html']:
value = '/'
elif field.type in ['boolean']:
value = False
elif field.type in ['integer', 'float']:
value = 0
elif model._fields[name].type in ['date', 'datetime']:
value = '2000-01-01'
elif field.type in ['many2one']:
if name in model._inherits.values():
continue
new_context = context.with_field_context(
model._name, name, record['id']
)
value = self._run_import_get_record(
new_context,
self.env[model._fields[name].comodel_name],
{'id': record.get(name, [None])[0]},
)
elif field.type in ['selection'] and not callable(field.selection):
value = field.selection[0][0]
elif field.type in ['selection'] and callable(field.selection):
value = field.selection(model)[0][0]
values[name] = value
dummy = self._create_record(context, model, values)
del context.idmap[mapping_key(model._name, record['id'])]
context.dummies[mapping_key(model._name, record['id'])] = dummy.id
context.to_delete.setdefault(model._name, [])
context.to_delete[model._name].append(dummy.id)
context.dummy_instances.append(
dummy_instance(*(context.field_context + (dummy.id,)))
)
_logger.debug(
'Created %d as dummy for %s(%d[%d]).%s[%d]',
dummy.id, context.field_context.record_model,
context.idmap.get(context.field_context.record_id, 0),
context.field_context.record_id or 0,
context.field_context.field_name, record['id'],
)
return dummy.id
@api.multi
def _run_import_map_values(self, context, data):
model = self.env[context.model_line.model_id.model]
for field_name in data.keys():
if not isinstance(
model._fields[field_name], fields._Relational
) or not data[field_name]:
continue
if model._fields[field_name].type == 'one2many':
# don't import one2many fields, use an own configuration
# for this
data.pop(field_name)
continue
ids = data[field_name] if (
model._fields[field_name].type != 'many2one'
) else [data[field_name][0]]
new_context = context.with_field_context(
model._name, field_name, data['id']
)
comodel = self.env[model._fields[field_name].comodel_name]
data[field_name] = [
self._run_import_get_record(
new_context, comodel, {'id': _id},
create_dummy=model._fields[field_name].required or
any(
m.model_id.model == comodel._name
for m in self.import_line_ids
),
)
for _id in ids
]
data[field_name] = filter(None, data[field_name])
if model._fields[field_name].type == 'many2one':
if data[field_name]:
data[field_name] = data[field_name] and data[field_name][0]
else:
data[field_name] = None
else:
data[field_name] = [(6, 0, data[field_name])]
for mapping in self.import_field_mappings:
if mapping.model_id.model != model._name:
continue
if mapping.mapping_type == 'unique':
for field in mapping.field_ids:
value = data.get(field.name, '')
counter = 1
while model.with_context(active_test=False).search([
(field.name, '=', data.get(field.name, value)),
]):
data[field.name] = '%s (%d)' % (value, counter)
counter += 1
elif mapping.mapping_type == 'by_reference':
res_model = data.get(mapping.model_field_id.name)
res_id = data.get(mapping.id_field_id.name)
update = {
mapping.model_field_id.name: None,
mapping.id_field_id.name: None,
}
if res_model in self.env.registry and res_id:
new_context = context.with_field_context(
model._name, res_id, data['id']
)
record_id = self._run_import_get_record(
new_context, self.env[res_model], {'id': res_id},
create_dummy=False
)
if record_id:
update.update({
mapping.model_field_id.name: res_model,
mapping.id_field_id.name: record_id,
})
data.update(update)
return data
@api.multi
def _run_import_model_get_fields(self, context):
return {
name: field
for name, field
in self.env[context.model_line.model_id.model]._fields.items()
if not field.compute or field.related
}
@api.multi
def _run_import_model_cleanup_dummies(
self, context, model, remote_id, local_id
):
if not (model._name, remote_id) in context.dummies:
return
for instance in context.dummy_instances:
key = mapping_key(instance.model_name, instance.remote_id)
if key not in context.idmap:
continue
dummy_id = context.dummies[(model._name, remote_id)]
record_model = self.env[instance.model_name]
comodel = record_model._fields[instance.field_name].comodel_name
if comodel != model._name or instance.dummy_id != dummy_id:
continue
record = record_model.browse(context.idmap[key])
field_name = instance.field_name
_logger.debug(
'Replacing dummy %d on %s(%d).%s with %d',
dummy_id, record_model._name, record.id, field_name, local_id,
)
if record._fields[field_name].type == 'many2one':
record.write({field_name: local_id})
elif record._fields[field_name].type == 'many2many':
record.write({field_name: [
(3, dummy_id),
(4, local_id),
]})
else:
raise exceptions.UserError(
_('Unhandled field type %s') %
record._fields[field_name].type
)
context.dummy_instances.remove(instance)
if dummy_id in context.to_delete:
model.browse(dummy_id).unlink()
_logger.debug('Deleting dummy %d', dummy_id)
if (model._name, remote_id) in context.dummies:
del context.dummies[(model._name, remote_id)]
def _get_connection(self):
self.ensure_one()
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
def _constrain_url(self):
for this in self:
if this == self.env.ref('base_import_odoo.demodb', False):
continue
if tools.config['test_enable']:
continue
if not this.password:
continue
this._get_connection()
@api.depends('status_data')
@api.multi
def _compute_status_html(self):
for this in self:
if not this.status_data:
continue
this.status_html = self.env.ref(
'base_import_odoo.view_import_odoo_database_qweb'
).render({'object': this})
@api.depends('cronjob_id')
@api.multi
def _compute_cronjob_running(self):
for this in self:
if not this.cronjob_id:
continue
try:
with self.env.cr.savepoint():
self.env.cr.execute(
'select id from "%s" where id=%%s for update nowait' %
self.env['ir.cron']._table,
(this.cronjob_id.id,), log_exceptions=False,
)
except psycopg2.OperationalError:
this.cronjob_running = True
@api.multi
def _create_cronjob(self):
self.ensure_one()
return self.env['ir.cron'].create({
'name': self.display_name,
'model': self._name,
'function': '_run_import',
'doall': True,
'args': str((self.ids,)),
})
@api.multi
def name_get(self):
return [
(this.id, '%s@%s, %s' % (this.user, this.url, this.database))
for this in self
]

68
base_import_odoo/models/import_odoo_database_field.py

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

20
base_import_odoo/models/import_odoo_database_model.py

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import fields, models
class ImportOdooDatabaseModel(models.Model):
_name = 'import.odoo.database.model'
_description = 'A model to import from a remote database'
_order = 'sequence'
sequence = fields.Integer()
model_id = fields.Many2one(
'ir.model', string='Model', required=True, ondelete='cascade',
)
database_id = fields.Many2one(
'import.odoo.database', string='Database', required=True,
ondelete='cascade',
)
domain = fields.Char(help='Optional filter to import only a subset')

13
base_import_odoo/models/ir_model_data.py

@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import fields, models
class IrModelData(models.Model):
_inherit = 'ir.model.data'
import_database_id = fields.Many2one(
'import.odoo.database', string='From remote database',
)
import_database_record_id = fields.Integer('Remote database ID')

4
base_import_odoo/security/ir.model.access.csv

@ -0,0 +1,4 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_import_odoo_database,access_import_odoo_database,model_import_odoo_database,base.group_system,1,1,1,1
access_import_odoo_database_model,access_import_odoo_database_model,model_import_odoo_database_model,base.group_system,1,1,1,1
access_import_odoo_database_field,access_import_odoo_database_field,model_import_odoo_database_field,base.group_system,1,1,1,1

BIN
base_import_odoo/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 9.2 KiB

4
base_import_odoo/tests/__init__.py

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import test_base_import_odoo

129
base_import_odoo/tests/test_base_import_odoo.py

@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
# © 2017 Therp BV <http://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from mock import patch
from openerp.tests.common import TransactionCase, post_install, at_install
from ..models.import_odoo_database import ImportContext, field_context
class TestBaseImportOdoo(TransactionCase):
def setUp(self):
super(TestBaseImportOdoo, self).setUp()
# 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
if 'account.account' in self.env.registry:
self.env.ref('base_import_odoo.demodb').write({
'import_field_mappings': [
(
0, False,
{
'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([])
],
})
@at_install(False)
@post_install(True)
@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_execute(model, method, *args):
if method == 'read':
return self.env[model].browse(args[0]).read(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)
run = 1
for dummy in range(2):
# we run this two times to enter the code path where xmlids exist
self.env.ref('base_import_odoo.demodb').write({
'password': 'admin',
})
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
imported_user = self.env.ref(self._get_xmlid('base.user_demo'))
user = self.env.ref('base.user_demo')
self.assertNotEqual(imported_user, user)
# check that the imported scalars are equal
fields = ['name', 'email', 'signature', 'active']
(imported_user + user).read(fields)
self.assertEqual(
self._get_cache(self._get_xmlid('base.user_demo'), fields),
self._get_cache('base.user_demo', fields),
)
# check that links are correctly mapped
self.assertEqual(
imported_user.partner_id,
self.env.ref(self._get_xmlid('base.partner_demo'))
)
# no new groups because they should be mapped by name
self.assertEqual(
group_count, self.env['res.groups'].search([], count=True)
)
# all users save for root should be duplicated for every run
self.assertEqual(
self.env['res.users'].search([], count=True),
user_count + (user_count - 1) * run,
)
# check that there's a new attachment
attachment = self.env.ref('base_import_odoo.attachment_demo')
imported_attachment = self.env['ir.attachment'].search([
('res_model', '=', 'res.users'),
('res_id', '=', imported_user.id),
])
self.assertTrue(attachment)
self.assertEqual(attachment.datas, imported_attachment.datas)
self.assertNotEqual(attachment, imported_attachment)
run += 1
demodb = self.env.ref('base_import_odoo.demodb')
for line in demodb.import_line_ids:
self.assertIn(line.model_id.model, demodb.status_html)
demodb.action_import()
self.assertTrue(demodb.cronjob_id)
demodb.cronjob_id.write({'active': False})
demodb.action_import()
self.assertTrue(demodb.cronjob_id.active)
self.assertFalse(demodb.cronjob_running)
# in our setting we won't get dummies, so we test this manually
import_context = ImportContext(
None, None, [], {}, {}, [], {}, field_context(None, None, None)
)
dummy_id = demodb._run_import_create_dummy(
import_context, self.env['res.partner'], {'id': 424242},
forcecreate=True,
)
self.assertTrue(self.env['res.partner'].browse(dummy_id).exists())
def _get_xmlid(self, remote_xmlid):
remote_obj = self.env.ref(remote_xmlid)
return 'base_import_odoo.%d-%s-%s' % (
self.env.ref('base_import_odoo.demodb').id,
remote_obj._name.replace('.', '_'),
remote_obj.id,
)
def _get_cache(self, xmlid, fields):
record = self.env.ref(xmlid)
return {
field_name: record._cache[field_name]
for field_name in record._fields
if field_name in fields
}

93
base_import_odoo/views/import_odoo_database.xml

@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_import_odoo_database_tree" model="ir.ui.view">
<field name="model">import.odoo.database</field>
<field name="arch" type="xml">
<tree>
<field name="url" />
<field name="database" />
</tree>
</field>
</record>
<record id="view_import_odoo_database_form" model="ir.ui.view">
<field name="model">import.odoo.database</field>
<field name="arch" type="xml">
<form>
<header>
<button 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" />
</header>
<sheet>
<field name="status_html" attrs="{'invisible': [('status_html', '=', False)]}" />
<group col="4" name="credentials">
<field name="url" widget="url" 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="cronjob_id" attrs="{'invisible': [('cronjob_id', '=', False)]}" />
</group>
<field name="import_line_ids" attrs="{'readonly': [('cronjob_running', '=', True)]}">
<tree editable="top">
<field name="sequence" widget="handle" />
<field name="model_id" />
<field name="domain" />
</tree>
</field>
<field name="import_field_mappings" attrs="{'readonly': [('cronjob_running', '=', True)]}"/>
</sheet>
</form>
</field>
</record>
<template id="view_import_odoo_database_qweb">
<script type="text/javascript">
function base_import_database_open(model, model_name, database_id)
{
return new openerp.web.Model('ir.model.data')
.query(['res_id'])
.filter([
['module', '=', 'base_import_odoo'],
['model', '=', model],
['import_database_id', '=', database_id],
])
.all()
.then(function(data)
{
return openerp.webclient.action_manager.do_action({
'name': model_name,
'type': 'ir.actions.act_window',
'views': [[false, 'list'], [false, 'form']],
'res_model': model,
'domain': [['id', 'in', _.map(data, function(x) {return x.res_id})]]
});
});
}
</script>
<h2 t-if="object.cronjob_running">Import progress</h2>
<h2 t-if="not object.cronjob_running">Import results</h2>
<div style="display: flex; flex-flow: row wrap">
<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_display_name" t-value="import_line.model_id.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)">
<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>
</div>
</div>
<t t-if="object.status_data.get('error')"><pre t-esc="object.status_data['error']" /></t>
<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.
<dl>
<t t-foreach="object.status_data['dummies']" t-as="model_name">
<dt t-esc="model_name" />
<dd t-esc="object.status_data['dummies'][model_name]" />
</t>
</dl>
To fix this, create mappings for the remote ids listed, or if this is not feasible, map the whole model. You might also have a too specific domain on your import model definition.
</div>
</template>
</data>
</openerp>

47
base_import_odoo/views/import_odoo_database_field.xml

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_import_odoo_database_field_form" model="ir.ui.view">
<field name="model">import.odoo.database.field</field>
<field name="arch" type="xml">
<form>
<group>
<field name="model_id" />
<field name="model" invisible="True" />
<field name="mapping_type" />
</group>
<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.
</div>
<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.
</div>
<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.
</div>
<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.
</div>
<group>
<field name="local_id" attrs="{'invisible': [('mapping_type', '!=', 'fixed')], 'required': [('mapping_type', '=', 'fixed')]}" />
<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>
</form>
</field>
</record>
<record id="view_import_odoo_database_field_tree" model="ir.ui.view">
<field name="model">import.odoo.database.field</field>
<field name="arch" type="xml">
<tree>
<field name="sequence" widget="handle" />
<field name="model_id" />
<field name="mapping_type" />
<field name="field_ids" />
</tree>
</field>
</record>
</data>
</openerp>

12
base_import_odoo/views/menu.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<menuitem 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"
name="Import configurations"
view_mode="tree,form"
/>
<menuitem id="menu_import_odoo_database" action="action_import_odoo_database" parent="menu_main" />
</data>
</openerp>

1
requirements.txt

@ -6,3 +6,4 @@
acme_tiny
IPy
python-json-logger
odoorpc
Loading…
Cancel
Save