Holger Brunn
7 years ago
committed by
OCA-git-bot
18 changed files with 911 additions and 0 deletions
-
94base_import_odoo/README.rst
-
4base_import_odoo/__init__.py
-
29base_import_odoo/__openerp__.py
-
11base_import_odoo/demo/import_odoo_database.xml
-
23base_import_odoo/demo/import_odoo_database_field.xml
-
17base_import_odoo/demo/import_odoo_database_model.xml
-
9base_import_odoo/demo/res_users.xml
-
7base_import_odoo/models/__init__.py
-
472base_import_odoo/models/import_odoo_database.py
-
35base_import_odoo/models/import_odoo_database_field.py
-
20base_import_odoo/models/import_odoo_database_model.py
-
13base_import_odoo/models/ir_model_data.py
-
4base_import_odoo/security/ir.model.access.csv
-
BINbase_import_odoo/static/description/icon.png
-
4base_import_odoo/tests/__init__.py
-
68base_import_odoo/tests/test_base_import_odoo.py
-
89base_import_odoo/views/import_odoo_database.xml
-
12base_import_odoo/views/menu.xml
@ -0,0 +1,94 @@ |
|||
.. 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 (needs a bit polishing) |
|||
- 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`), set the flag `unique`, then the import will generate a unique value for this field. |
|||
|
|||
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 |
|||
* Probably it's safer and faster to disable recomputation during import, and recompute all fields afterwards |
|||
|
|||
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. |
@ -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 |
@ -0,0 +1,29 @@ |
|||
# -*- 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": [ |
|||
'base', |
|||
], |
|||
"demo": [ |
|||
"demo/res_users.xml", |
|||
"demo/import_odoo_database.xml", |
|||
"demo/import_odoo_database_field.xml", |
|||
"demo/import_odoo_database_model.xml", |
|||
], |
|||
"data": [ |
|||
"security/ir.model.access.csv", |
|||
"views/import_odoo_database.xml", |
|||
"views/menu.xml", |
|||
], |
|||
"installable": True, |
|||
"external_dependencies": { |
|||
"python": ['erppeek'], |
|||
}, |
|||
} |
@ -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> |
@ -0,0 +1,23 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<openerp> |
|||
<data noupdate="1"> |
|||
<record id="mapping_user_id" model="import.odoo.database.field"> |
|||
<field name="database_id" ref="demodb" /> |
|||
<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_company_id" model="import.odoo.database.field"> |
|||
<field name="database_id" ref="demodb" /> |
|||
<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="model_id" ref="base.model_res_users" /> |
|||
<field name="fields_id" ref="base.field_res_users_login" /> |
|||
<field name="unique" eval="True" /> |
|||
</record> |
|||
</data> |
|||
</openerp> |
@ -0,0 +1,17 @@ |
|||
<?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_users" model="import.odoo.database.model"> |
|||
<field name="sequence">2</field> |
|||
<field name="model_id" ref="base.model_res_users" /> |
|||
<field name="database_id" ref="demodb" /> |
|||
<field name="domain">[(1, '=', 1)]</field> |
|||
</record> |
|||
</data> |
|||
</openerp> |
@ -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> |
@ -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 |
@ -0,0 +1,472 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# © 2017 Therp BV <http://therp.nl> |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
try: |
|||
from erppeek import Client |
|||
except: |
|||
pass |
|||
import psycopg2 |
|||
import traceback |
|||
from openerp import _, api, exceptions, fields, models, tools |
|||
from collections import namedtuple |
|||
|
|||
|
|||
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, |
|||
) |
|||
|
|||
@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.search( |
|||
model.model, |
|||
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, |
|||
'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 |
|||
|
|||
for start_index in range( |
|||
len(remote_ids[model._name]) / commit_threshold + 1 |
|||
): |
|||
index = start_index * commit_threshold |
|||
ids = remote_ids[model._name][index:index + commit_threshold] |
|||
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() |
|||
|
|||
@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.read( |
|||
model._name, context.ids, fields.keys() |
|||
): |
|||
self._run_import_get_record(context, model, data) |
|||
if (model._name, data['id']) in context.idmap: |
|||
# 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, |
|||
) |
|||
if self.env.ref('base_import_odoo.%s' % xmlid, False): |
|||
new = self.env.ref('base_import_odoo.%s' % xmlid) |
|||
new.with_context( |
|||
**self._create_record_context(model, record) |
|||
).write(record) |
|||
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, |
|||
}) |
|||
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'])) |
|||
if not _id: |
|||
_id = context.dummies.get((model._name, record['id'])) |
|||
if _id: |
|||
context.dummy_instances.append( |
|||
dummy_instance(*(context.field_context + (_id,))) |
|||
) |
|||
if not _id: |
|||
mapping = self.import_field_mappings.filtered( |
|||
lambda x: x.model_id.model == model._name and |
|||
( |
|||
not x.fields_id or |
|||
x.fields_id.name == context.field_context.field_name and |
|||
x.fields_id.model_id.model == |
|||
context.field_context.record_model |
|||
) and |
|||
x.local_id and |
|||
(x.remote_id == record['id'] or not x.remote_id) |
|||
)[:1] |
|||
if mapping: |
|||
if mapping.local_id: |
|||
_id = mapping.local_id |
|||
context.idmap[(model._name, record['id'])] = _id |
|||
else: |
|||
_id = self._run_import_create_dummy( |
|||
context, model, record, forcecreate=True, |
|||
) |
|||
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 |
|||
if not _id and create_dummy: |
|||
_id = self._run_import_create_dummy(context, model, record) |
|||
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.iteritems() |
|||
if model_name == model._name |
|||
] |
|||
), |
|||
], 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,))) |
|||
) |
|||
return dummy.id |
|||
required = [ |
|||
name |
|||
for name, field in model._fields.iteritems() |
|||
if field.required |
|||
] |
|||
defaults = model.default_get(required) |
|||
values = {'id': record['id']} |
|||
for name, field in model._fields.iteritems(): |
|||
if name not in required or name in defaults: |
|||
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']: |
|||
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] |
|||
# TODO: support more types, refactor to one function per type |
|||
values[name] = value |
|||
dummy = self._create_record(context, model, values) |
|||
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,))) |
|||
) |
|||
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'] |
|||
) |
|||
data[field_name] = [ |
|||
self._run_import_get_record( |
|||
new_context, |
|||
self.env[model._fields[field_name].comodel_name], |
|||
{'id': _id}, |
|||
create_dummy=model._fields[field_name].required, |
|||
) |
|||
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 or not mapping.fields_id: |
|||
continue |
|||
if mapping.unique: |
|||
value = data.get(mapping.fields_id.name, '') |
|||
counter = 1 |
|||
while model.with_context(active_test=False).search([ |
|||
( |
|||
mapping.fields_id.name, '=', |
|||
data.get(mapping.fields_id.name, value) |
|||
), |
|||
]): |
|||
data[mapping.fields_id.name] = '%s (%d)' % (value, counter) |
|||
counter += 1 |
|||
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.iteritems() |
|||
if not field.compute or field.related |
|||
} |
|||
|
|||
@api.multi |
|||
def _run_import_model_cleanup_dummies( |
|||
self, context, model, remote_id, local_id |
|||
): |
|||
for instance in context.dummy_instances: |
|||
if ( |
|||
instance.model_name != model._name or |
|||
instance.remote_id != remote_id |
|||
): |
|||
continue |
|||
if not context.idmap.get(instance.remote_id): |
|||
continue |
|||
model = self.env[instance.model_name] |
|||
record = model.browse(context.idmap[instance.remote_id]) |
|||
field_name = instance.field_id.name |
|||
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, context.idmap[remote_id]), |
|||
(4, local_id), |
|||
]}) |
|||
else: |
|||
raise exceptions.UserError( |
|||
_('Unhandled field type %s') % |
|||
record._fields[field_name].type |
|||
) |
|||
context.dummy_instances.remove(instance) |
|||
dummy_id = context.dummies[(record._model, remote_id)] |
|||
if dummy_id in context.to_delete: |
|||
model.browse(dummy_id).unlink() |
|||
del context.dummies[(record._model, remote_id)] |
|||
|
|||
def _get_connection(self): |
|||
self.ensure_one() |
|||
return Client(self.url, self.database, self.user, self.password) |
|||
|
|||
@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 |
|||
] |
@ -0,0 +1,35 @@ |
|||
# -*- 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 ImportOdooDatabaseField(models.Model): |
|||
_name = 'import.odoo.database.field' |
|||
_description = 'A field mapping for records in the remote database' |
|||
|
|||
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', |
|||
) |
|||
fields_id = fields.Many2one( |
|||
'ir.model.fields', string='Field', help='If set, the mapping is only ' |
|||
'effective when setting said field', ondelete='cascade', |
|||
) |
|||
unique = fields.Boolean( |
|||
'Unique', help='If set on a char field, a number is appended until ' |
|||
'the value is unique. Set this for fields with uniqueness constraints', |
|||
) |
|||
# 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' |
|||
) |
@ -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') |
@ -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') |
@ -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 |
After Width: 128 | Height: 128 | Size: 9.2 KiB |
@ -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 |
@ -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 mock import patch |
|||
from openerp.tests.common import TransactionCase, post_install, at_install |
|||
|
|||
|
|||
class TestBaseImportOdoo(TransactionCase): |
|||
@at_install(False) |
|||
@post_install(True) |
|||
@patch('erppeek.Client.__init__', side_effect=lambda *args: None) |
|||
def test_base_import_odoo(self, mock_client_init): |
|||
# the mocked functions simply search/read in the current database |
|||
# the effect then should be that the models in question are duplicated, |
|||
# we just need to try not to be confused by the fact that local and |
|||
# remote ids are the same |
|||
def _mock_search( |
|||
model, domain, offset=0, limit=None, order=None, context=None, |
|||
): |
|||
return self.env[model].with_context( |
|||
**(context or self.env.context) |
|||
).search( |
|||
domain, offset=offset, limit=limit, order=order, |
|||
).ids |
|||
|
|||
def _mock_read( |
|||
model, domain_or_ids, fields=None, offset=0, limit=None, |
|||
order=None, context=None, |
|||
): |
|||
return self.env[model].with_context( |
|||
**(context or self.env.context) |
|||
).browse(domain_or_ids).read(fields=fields) |
|||
|
|||
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('erppeek.Client.search', side_effect=_mock_search): |
|||
with patch('erppeek.Client.read', side_effect=_mock_read): |
|||
self.env.ref('base_import_odoo.demodb')._run_import() |
|||
# here the actual test begins - check that we created new |
|||
# objects, check xmlids, check values, check if dummies are |
|||
# cleaned up/replaced |
|||
self.assertNotEqual( |
|||
self.env.ref(self._get_xmlid('base.user_demo')), |
|||
self.env.ref('base.user_demo'), |
|||
) |
|||
self.assertEqual( |
|||
dict(self.env.ref(self._get_xmlid('base.user_demo'))._cache), |
|||
dict(self.env.ref('base.user_demo')._cache), |
|||
) |
|||
# TODO: test much more |
|||
demodb = self.env.ref('base_import_odoo.demodb') |
|||
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) |
|||
|
|||
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, |
|||
) |
@ -0,0 +1,89 @@ |
|||
<?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> |
|||
<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="cronjob_id" attrs="{'invisible': [('cronjob_id', '=', False)]}" /> |
|||
<field name="status_html" attrs="{'invisible': [('status_html', '=', False)]}" string="Progress" /> |
|||
</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)]}"> |
|||
<tree> |
|||
<field name="model_id" /> |
|||
<field name="fields_id" /> |
|||
<field name="local_id" /> |
|||
<field name="remote_id" /> |
|||
<field name="unique" /> |
|||
</tree> |
|||
</field> |
|||
</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> |
|||
<dl> |
|||
<t t-foreach="object.status_data.get('counts', [])" t-as="model_name"> |
|||
<t t-set="model_display_name" t-value="request.env['ir.model'].search([('model', '=', model_name)]).name" /> |
|||
<dt t-esc="model_display_name" /> |
|||
<dd> |
|||
<a t-att-href="'javascript:base_import_database_open("%s", "%s", %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['counts'][model_name]" /> done |
|||
</a> |
|||
</dd> |
|||
</t> |
|||
</dl> |
|||
<t t-if="object.status_data.get('error')"><pre t-esc="object.status_data['error']" /></t> |
|||
</template> |
|||
</data> |
|||
</openerp> |
@ -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> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue