[IMP] base_external_system: Add create bypass

* In cases of deep inheritance, it may be required to create an adapter directly. Add an override in the create via the env context to support this.
* Add default system type if creating from an interface
* Fix interface assignment during creation in adapter
This commit is contained in:
Dave Lasley
2017-10-20 14:17:13 -07:00
parent 368380e9e8
commit 4db2a3ea1f
4 changed files with 39 additions and 6 deletions

View File

@@ -5,7 +5,7 @@
{ {
"name": "Base External System", "name": "Base External System",
"summary": "Data models allowing for connection to external systems.", "summary": "Data models allowing for connection to external systems.",
"version": "10.0.1.0.0", "version": "10.0.1.0.1",
"category": "Base", "category": "Base",
"website": "https://laslabs.com/", "website": "https://laslabs.com/",
"author": "LasLabs, Odoo Community Association (OCA)", "author": "LasLabs, Odoo Community Association (OCA)",

View File

@@ -112,10 +112,11 @@ class ExternalSystem(models.Model):
def create(self, vals): def create(self, vals):
"""Create the interface for the record and assign to ``interface``.""" """Create the interface for the record and assign to ``interface``."""
record = super(ExternalSystem, self).create(vals) record = super(ExternalSystem, self).create(vals)
interface = self.env[vals['system_type']].create({ if not self.env.context.get('no_create_interface'):
'system_id': record.id, interface = self.env[vals['system_type']].create({
}) 'system_id': record.id,
record.interface = interface })
record.interface = interface
return record return record
@api.multi @api.multi

View File

@@ -69,3 +69,13 @@ class ExternalSystemAdapter(models.AbstractModel):
odoo.exceptions.UserError: In the event of a good connection. odoo.exceptions.UserError: In the event of a good connection.
""" """
raise UserError(_('The connection was a success.')) raise UserError(_('The connection was a success.'))
@api.model
def create(self, vals):
context_self = self.with_context(no_create_interface=True)
vals.update({
'system_type': self._name,
})
record = super(ExternalSystemAdapter, context_self).create(vals)
record.system_id.interface = record
return record

View File

@@ -44,11 +44,33 @@ class TestExternalSystem(Common):
def test_create_creates_and_assigns_interface(self): def test_create_creates_and_assigns_interface(self):
"""It should create and assign the interface on record create.""" """It should create and assign the interface on record create."""
record = self.env['external.system'].create({
'name': 'Test',
'system_type': 'external.system.os',
})
self.assertEqual( self.assertEqual(
self.record.interface._name, 'external.system.os', record.interface._name, 'external.system.os',
) )
def test_create_context_override(self):
"""It should allow for interface create override with context."""
model = self.env['external.system'].with_context(
no_create_interface=True,
)
record = model.create({
'name': 'Test',
'system_type': 'external.system.os',
})
self.assertFalse(record.interface)
def test_action_test_connection(self): def test_action_test_connection(self):
"""It should proxy to the interface connection tester.""" """It should proxy to the interface connection tester."""
with self.assertRaises(UserError): with self.assertRaises(UserError):
self.record.system_id.action_test_connection() self.record.system_id.action_test_connection()
def test_unlink_deletes_interface(self):
"""It should delete the interface when the system is deleted."""
interface = self.record.interface
self.assertTrue(interface.exists())
self.record.unlink()
self.assertFalse(interface.exists())