# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-from openerp import fields, models
+from openerp import api, fields, models
class ImportOdooDatabaseField(models.Model):
@@ -22,6 +22,18 @@ class ImportOdooDatabaseField(models.Model):
'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 '
@@ -36,7 +48,21 @@ class ImportOdooDatabaseField(models.Model):
[
('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]
diff --git a/base_import_odoo/tests/test_base_import_odoo.py b/base_import_odoo/tests/test_base_import_odoo.py
index 76739fe56..7951840c5 100644
--- a/base_import_odoo/tests/test_base_import_odoo.py
+++ b/base_import_odoo/tests/test_base_import_odoo.py
@@ -20,7 +20,7 @@ class TestBaseImportOdoo(TransactionCase):
# remote ids are the same
def _mock_execute(model, method, *args):
if method == 'read':
- return self.env[model].browse(args[0]).read(fields=args[1])
+ return self.env[model].browse(args[0]).read(args[1])
if method == 'search':
return self.env[model].search(args[0]).ids
@@ -37,23 +37,19 @@ class TestBaseImportOdoo(TransactionCase):
# 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'),
- )
+ 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']
- (
- self.env.ref(self._get_xmlid('base.user_demo')) +
- self.env.ref('base.user_demo')
- ).read(fields)
+ (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(
- self.env.ref(self._get_xmlid('base.user_demo')).partner_id,
+ imported_user.partner_id,
self.env.ref(self._get_xmlid('base.partner_demo'))
)
# no new groups because they should be mapped by name
@@ -65,6 +61,15 @@ class TestBaseImportOdoo(TransactionCase):
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)
# TODO: test much more
run += 1
demodb = self.env.ref('base_import_odoo.demodb')
diff --git a/base_import_odoo/views/import_odoo_database_field.xml b/base_import_odoo/views/import_odoo_database_field.xml
index 061b63e50..8d44bd57c 100644
--- a/base_import_odoo/views/import_odoo_database_field.xml
+++ b/base_import_odoo/views/import_odoo_database_field.xml
@@ -16,13 +16,18 @@
Select fields which must be equal to treat a pair of remote and local records of this model as being equal.
+
+ 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 fields for which to generate unique values during import. You'll need this for res.users#login for example.
-
+
+
+