diff --git a/base_import_odoo/README.rst b/base_import_odoo/README.rst index 3ff3d68b6..af6385e2e 100644 --- a/base_import_odoo/README.rst +++ b/base_import_odoo/README.rst @@ -12,7 +12,7 @@ Use cases ========= - merging databases -- one way sync (needs a bit polishing) +- one way sync - aggregating management data from distributed systems @@ -57,6 +57,7 @@ Known issues / Roadmap * 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 =========== diff --git a/base_import_odoo/models/import_odoo_database.py b/base_import_odoo/models/import_odoo_database.py index 3965dd49f..6d78f80ca 100644 --- a/base_import_odoo/models/import_odoo_database.py +++ b/base_import_odoo/models/import_odoo_database.py @@ -62,6 +62,13 @@ class ImportOdooDatabase(models.Model): 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): @@ -177,8 +184,9 @@ class ImportOdooDatabase(models.Model): context, model, data, create_dummy=False, ) if (model._name, data['id']) in context.idmap: - # there's a mapping for this record, nothing to do - continue + 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) @@ -193,8 +201,14 @@ class ImportOdooDatabase(models.Model): xmlid = '%d-%s-%d' % ( self.id, model._name.replace('.', '_'), _id or 0, ) - if self.env.ref('base_import_odoo.%s' % xmlid, False): - new = self.env.ref('base_import_odoo.%s' % xmlid) + 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) @@ -321,10 +335,18 @@ class ImportOdooDatabase(models.Model): continue if isinstance(record, list): record = record[0] - records = model.search([ + domain = [ (field.name, '=', record.get(field.name)) for field in mapping.field_ids - ], limit=1) + 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 diff --git a/base_import_odoo/views/import_odoo_database.xml b/base_import_odoo/views/import_odoo_database.xml index ab8333a8d..a67e8c823 100644 --- a/base_import_odoo/views/import_odoo_database.xml +++ b/base_import_odoo/views/import_odoo_database.xml @@ -26,6 +26,7 @@ + @@ -64,8 +65,9 @@ }); } -

Import progress

-
+

Import progress

+

Import results

+