Browse Source

[ADD] create an xmlid for mapped records too (#1059)

* [ADD] create an xmlid for mapped records too
* [IMP] filter out unknown fields
* [FIX] warning about exception handling
pull/1213/head
Holger Brunn 7 years ago
committed by Stefan Rijnhart (Opener)
parent
commit
5b2463d277
  1. 43
      base_import_odoo/models/import_odoo_database.py

43
base_import_odoo/models/import_odoo_database.py

@ -151,7 +151,8 @@ class ImportOdooDatabase(models.Model):
)
try:
self._run_import_model(context)
except:
except: # noqa: E722
# pragma: no cover
error = traceback.format_exc()
self.env.cr.rollback()
self.write({
@ -186,6 +187,11 @@ class ImportOdooDatabase(models.Model):
context, model, data, create_dummy=False,
)
if (model._name, data['id']) in context.idmap:
# one of our mappings hit, create an xmlid to persist
# this knowledge
self._create_record_xmlid(
model, context.idmap[(model._name, data['id'])], data['id']
)
if self.duplicates == 'skip':
# there's a mapping for this record, nothing to do
continue
@ -203,6 +209,7 @@ class ImportOdooDatabase(models.Model):
xmlid = '%d-%s-%d' % (
self.id, model._name.replace('.', '_'), _id or 0,
)
record = self._create_record_filter_fields(model, record)
new = self.env.ref('base_import_odoo.%s' % xmlid, False)
if new and new.exists():
if self.duplicates == 'overwrite_empty':
@ -219,19 +226,35 @@ class ImportOdooDatabase(models.Model):
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,
})
self._create_record_xmlid(model, new.id, _id)
_logger.debug('Created record %s', xmlid)
context.idmap[mapping_key(model._name, _id)] = new.id
return new
def _create_record_xmlid(self, model, local_id, remote_id):
xmlid = '%d-%s-%d' % (
self.id, model._name.replace('.', '_'), remote_id or 0,
)
if self.env.ref('base_import_odoo.%s' % xmlid, False):
return
return self.env['ir.model.data'].create({
'name': xmlid,
'model': model._name,
'module': 'base_import_odoo',
'res_id': local_id,
'noupdate': True,
'import_database_id': self.id,
'import_database_record_id': remote_id,
})
def _create_record_filter_fields(self, model, record):
"""Return a version of record with unknown fields for model removed"""
return {
key: value
for key, value in record.items()
if key in model._fields
}
def _create_record_context(self, model, record):
"""Return a context that is used when creating a record"""
context = {

Loading…
Cancel
Save