From 19c3d1100bc797d271dd32d2fb506d47e2f86ab5 Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Fri, 20 Oct 2017 14:17:13 -0700 Subject: [PATCH] [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 --- base_external_system/__manifest__.py | 2 +- .../models/external_system.py | 9 +++---- .../models/external_system_adapter.py | 10 ++++++++ .../tests/test_external_system.py | 24 ++++++++++++++++++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/base_external_system/__manifest__.py b/base_external_system/__manifest__.py index 13486c28..2f70b562 100644 --- a/base_external_system/__manifest__.py +++ b/base_external_system/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Base External System", "summary": "Data models allowing for connection to external systems.", - "version": "10.0.1.0.0", + "version": "10.0.1.0.1", "category": "Base", "website": "https://laslabs.com/", "author": "LasLabs, Odoo Community Association (OCA)", diff --git a/base_external_system/models/external_system.py b/base_external_system/models/external_system.py index 57a17142..75918c22 100644 --- a/base_external_system/models/external_system.py +++ b/base_external_system/models/external_system.py @@ -112,10 +112,11 @@ class ExternalSystem(models.Model): def create(self, vals): """Create the interface for the record and assign to ``interface``.""" record = super(ExternalSystem, self).create(vals) - interface = self.env[vals['system_type']].create({ - 'system_id': record.id, - }) - record.interface = interface + if not self.env.context.get('no_create_interface'): + interface = self.env[vals['system_type']].create({ + 'system_id': record.id, + }) + record.interface = interface return record @api.multi diff --git a/base_external_system/models/external_system_adapter.py b/base_external_system/models/external_system_adapter.py index b44c3d4c..605eabd6 100644 --- a/base_external_system/models/external_system_adapter.py +++ b/base_external_system/models/external_system_adapter.py @@ -69,3 +69,13 @@ class ExternalSystemAdapter(models.AbstractModel): odoo.exceptions.UserError: In the event of a good connection. """ 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 diff --git a/base_external_system/tests/test_external_system.py b/base_external_system/tests/test_external_system.py index 6d786270..e996b302 100644 --- a/base_external_system/tests/test_external_system.py +++ b/base_external_system/tests/test_external_system.py @@ -44,11 +44,33 @@ class TestExternalSystem(Common): def test_create_creates_and_assigns_interface(self): """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.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): """It should proxy to the interface connection tester.""" with self.assertRaises(UserError): 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())