[IMP] : black, isort, prettier

This commit is contained in:
Ronald Portier
2020-08-20 14:01:02 +02:00
committed by Pedro M. Baeza
parent 5b5b55c8c7
commit df7608d90b
10 changed files with 117 additions and 147 deletions

View File

@@ -7,17 +7,14 @@
"version": "12.0.1.0.0", "version": "12.0.1.0.0",
"category": "Base", "category": "Base",
"website": "https://github.com/OCA/server-backend", "website": "https://github.com/OCA/server-backend",
"author": "LasLabs, " "author": "LasLabs, " "Odoo Community Association (OCA)",
"Odoo Community Association (OCA)",
"license": "LGPL-3", "license": "LGPL-3",
"application": False, "application": False,
"installable": True, "installable": True,
'depends': [ "depends": ["base",],
'base', "data": [
], "demo/external_system_os_demo.xml",
'data': [ "security/ir.model.access.csv",
'demo/external_system_os_demo.xml', "views/external_system_view.xml",
'security/ir.model.access.csv',
'views/external_system_view.xml',
], ],
} }

View File

@@ -1,17 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<!-- <!--
Copyright 2017 LasLabs Inc. Copyright 2017 LasLabs Inc.
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
--> -->
<odoo> <odoo>
<record id="external_system_os" model="external.system.os"> <record id="external_system_os" model="external.system.os">
<field name="name">Example OS Connection</field> <field name="name">Example OS Connection</field>
<field name="system_type">external.system.os</field> <field name="system_type">external.system.os</field>
<field name="remote_path">/tmp</field> <field name="remote_path">/tmp</field>
<field name="company_ids" eval="[(5, 0), (4, ref('base.main_company'))]" /> <field name="company_ids" eval="[(5, 0), (4, ref('base.main_company'))]" />
</record> </record>
</odoo> </odoo>

View File

@@ -3,94 +3,89 @@
from contextlib import contextmanager from contextlib import contextmanager
from odoo import api, fields, models, _ from odoo import _, api, fields, models
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
class ExternalSystem(models.Model): class ExternalSystem(models.Model):
_name = 'external.system' _name = "external.system"
_description = 'External System' _description = "External System"
name = fields.Char( name = fields.Char(
required=True, required=True, help="This is the canonical (humanized) name for the system.",
help='This is the canonical (humanized) name for the system.',
) )
host = fields.Char( host = fields.Char(
help='This is the domain or IP address that the system can be reached ' help="This is the domain or IP address that the system can be reached " "at.",
'at.',
) )
port = fields.Integer( port = fields.Integer(
help='This is the port number that the system is listening on.', help="This is the port number that the system is listening on.",
) )
username = fields.Char( username = fields.Char(
help='This is the username that is used for authenticating to this ' help="This is the username that is used for authenticating to this "
'system, if applicable.', "system, if applicable.",
) )
password = fields.Char( password = fields.Char(
help='This is the password that is used for authenticating to this ' help="This is the password that is used for authenticating to this "
'system, if applicable.', "system, if applicable.",
) )
private_key = fields.Text( private_key = fields.Text(
help='This is the private key that is used for authenticating to ' help="This is the private key that is used for authenticating to "
'this system, if applicable.', "this system, if applicable.",
) )
private_key_password = fields.Text( private_key_password = fields.Text(
help='This is the password to unlock the private key that was ' help="This is the password to unlock the private key that was "
'provided for this sytem.', "provided for this sytem.",
) )
fingerprint = fields.Text( fingerprint = fields.Text(
help='This is the fingerprint that is advertised by this system in ' help="This is the fingerprint that is advertised by this system in "
'order to validate its identity.', "order to validate its identity.",
) )
ignore_fingerprint = fields.Boolean( ignore_fingerprint = fields.Boolean(
default=True, default=True,
help='Set this to `True` in order to ignore an invalid/unknown ' help="Set this to `True` in order to ignore an invalid/unknown "
'fingerprint from the system.', "fingerprint from the system.",
) )
remote_path = fields.Char( remote_path = fields.Char(
help='Restrict to this directory path on the remote, if applicable.', help="Restrict to this directory path on the remote, if applicable.",
) )
company_ids = fields.Many2many( company_ids = fields.Many2many(
string='Companies', string="Companies",
comodel_name='res.company', comodel_name="res.company",
required=True, required=True,
default=lambda s: [(6, 0, s.env.user.company_id.ids)], default=lambda s: [(6, 0, s.env.user.company_id.ids)],
help='Access to this system is restricted to these companies.', help="Access to this system is restricted to these companies.",
)
system_type = fields.Selection(
selection='_get_system_types',
required=True,
) )
system_type = fields.Selection(selection="_get_system_types", required=True,)
interface = fields.Reference( interface = fields.Reference(
selection='_get_system_types', selection="_get_system_types",
readonly=True, readonly=True,
help='This is the interface that this system represents. It is ' help="This is the interface that this system represents. It is "
'created automatically upon creation of the external system.', "created automatically upon creation of the external system.",
) )
_sql_constraints = [ _sql_constraints = [
('name_uniq', 'UNIQUE(name)', 'Connection name must be unique.'), ("name_uniq", "UNIQUE(name)", "Connection name must be unique."),
] ]
@api.model @api.model
def _get_system_types(self): def _get_system_types(self):
"""Return the adapter interface models that are installed.""" """Return the adapter interface models that are installed."""
adapter = self.env['external.system.adapter'] adapter = self.env["external.system.adapter"]
return [ return [(m, self.env[m]._description) for m in adapter._inherit_children]
(m, self.env[m]._description) for m in adapter._inherit_children
]
@api.multi @api.multi
@api.constrains('fingerprint', 'ignore_fingerprint') @api.constrains("fingerprint", "ignore_fingerprint")
def check_fingerprint_ignore_fingerprint(self): def check_fingerprint_ignore_fingerprint(self):
"""Do not allow a blank fingerprint if not set to ignore.""" """Do not allow a blank fingerprint if not set to ignore."""
for record in self: for record in self:
if not record.ignore_fingerprint and not record.fingerprint: if not record.ignore_fingerprint and not record.fingerprint:
raise ValidationError(_( raise ValidationError(
'Fingerprint cannot be empty when Ignore Fingerprint is ' _(
'not checked.', "Fingerprint cannot be empty when Ignore Fingerprint is "
)) "not checked.",
)
)
@api.multi @api.multi
@contextmanager @contextmanager
@@ -111,10 +106,8 @@ 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)
if not self.env.context.get('no_create_interface'): if not self.env.context.get("no_create_interface"):
interface = self.env[vals['system_type']].create({ interface = self.env[vals["system_type"]].create({"system_id": record.id,})
'system_id': record.id,
})
record.interface = interface record.interface = interface
return record return record

View File

@@ -3,7 +3,7 @@
from contextlib import contextmanager from contextlib import contextmanager
from odoo import api, fields, models, _ from odoo import _, api, fields, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
@@ -13,15 +13,15 @@ class ExternalSystemAdapter(models.AbstractModel):
Methods provided are prefixed with ``external_`` in order to keep from Methods provided are prefixed with ``external_`` in order to keep from
""" """
_name = 'external.system.adapter' _name = "external.system.adapter"
_description = 'External System Adapter' _description = "External System Adapter"
_inherits = {'external.system': 'system_id'} _inherits = {"external.system": "system_id"}
system_id = fields.Many2one( system_id = fields.Many2one(
string='System', string="System",
comodel_name='external.system', comodel_name="external.system",
required=True, required=True,
ondelete='cascade', ondelete="cascade",
) )
@api.multi @api.multi
@@ -67,14 +67,14 @@ class ExternalSystemAdapter(models.AbstractModel):
Raises: Raises:
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 @api.model
def create(self, vals): def create(self, vals):
context_self = self.with_context(no_create_interface=True) context_self = self.with_context(no_create_interface=True)
vals.update({ vals.update(
'system_type': self._name, {"system_type": self._name,}
}) )
record = super(ExternalSystemAdapter, context_self).create(vals) record = super(ExternalSystemAdapter, context_self).create(vals)
record.system_id.interface = record record.system_id.interface = record
return record return record

View File

@@ -13,9 +13,9 @@ class ExternalSystemOs(models.Model):
system interface. This is still a fully usable implementation, however. system interface. This is still a fully usable implementation, however.
""" """
_name = 'external.system.os' _name = "external.system.os"
_inherit = 'external.system.adapter' _inherit = "external.system.adapter"
_description = 'External System OS' _description = "External System OS"
previous_dir = None previous_dir = None

View File

@@ -2,13 +2,13 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from contextlib import contextmanager from contextlib import contextmanager
from mock import MagicMock from mock import MagicMock
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
class Common(TransactionCase): class Common(TransactionCase):
@contextmanager @contextmanager
def _mock_method(self, method_name, method_obj=None): def _mock_method(self, method_name, method_obj=None):
if method_obj is None: if method_obj is None:

View File

@@ -7,59 +7,46 @@ from .common import Common
class TestExternalSystem(Common): class TestExternalSystem(Common):
def setUp(self): def setUp(self):
super(TestExternalSystem, self).setUp() super(TestExternalSystem, self).setUp()
self.record = self.env.ref('base_external_system.external_system_os') self.record = self.env.ref("base_external_system.external_system_os")
def test_get_system_types(self): def test_get_system_types(self):
"""It should return at least the test record's interface.""" """It should return at least the test record's interface."""
self.assertIn( self.assertIn(
(self.record._name, self.record._description), (self.record._name, self.record._description),
self.env['external.system']._get_system_types(), self.env["external.system"]._get_system_types(),
) )
def test_check_fingerprint_blank(self): def test_check_fingerprint_blank(self):
"""It should not allow blank fingerprints when checking enabled.""" """It should not allow blank fingerprints when checking enabled."""
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.record.write({ self.record.write({"ignore_fingerprint": False, "fingerprint": False})
'ignore_fingerprint': False,
'fingerprint': False,
})
def test_check_fingerprint_allowed(self): def test_check_fingerprint_allowed(self):
"""It should not raise a validation error if there is a fingerprint.""" """It should not raise a validation error if there is a fingerprint."""
self.record.write({ self.record.write({"ignore_fingerprint": False, "fingerprint": "Data"})
'ignore_fingerprint': False,
'fingerprint': 'Data',
})
self.assertTrue(True) self.assertTrue(True)
def test_client(self): def test_client(self):
"""It should yield the open interface client.""" """It should yield the open interface client."""
with self._mock_method('client', self.record) as magic: with self._mock_method("client", self.record) as magic:
with self.record.system_id.client() as client: with self.record.system_id.client() as client:
self.assertEqual(client, magic().__enter__()) self.assertEqual(client, magic().__enter__())
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({ record = self.env["external.system"].create(
'name': 'Test', {"name": "Test", "system_type": "external.system.os"}
'system_type': 'external.system.os', )
})
self.assertEqual( self.assertEqual(
record.interface._name, 'external.system.os', record.interface._name, "external.system.os",
) )
def test_create_context_override(self): def test_create_context_override(self):
"""It should allow for interface create override with context.""" """It should allow for interface create override with context."""
model = self.env['external.system'].with_context( model = self.env["external.system"].with_context(no_create_interface=True,)
no_create_interface=True, record = model.create({"name": "Test", "system_type": "external.system.os"})
)
record = model.create({
'name': 'Test',
'system_type': 'external.system.os',
})
self.assertFalse(record.interface) self.assertFalse(record.interface)
def test_action_test_connection(self): def test_action_test_connection(self):

View File

@@ -7,23 +7,22 @@ from .common import Common
class TestExternalSystemAdapter(Common): class TestExternalSystemAdapter(Common):
def setUp(self): def setUp(self):
super(TestExternalSystemAdapter, self).setUp() super(TestExternalSystemAdapter, self).setUp()
self.system = self.env.ref('base_external_system.external_system_os') self.system = self.env.ref("base_external_system.external_system_os")
self.record = self.env['external.system.adapter'].new({ self.record = self.env["external.system.adapter"].new(
'system_id': self.system.id, {"system_id": self.system.id}
}) )
def test_client_yields_client(self): def test_client_yields_client(self):
"""It should yield the client.""" """It should yield the client."""
with self._mock_method('external_get_client') as magic: with self._mock_method("external_get_client") as magic:
with self.record.client() as client: with self.record.client() as client:
self.assertEqual(client, magic()) self.assertEqual(client, magic())
def test_client_destroys_client(self): def test_client_destroys_client(self):
"""It should destroy the client after use.""" """It should destroy the client after use."""
with self._mock_method('external_destroy_client') as magic: with self._mock_method("external_destroy_client") as magic:
with self.record.client() as client: with self.record.client() as client:
self.assertFalse(magic.call_count) self.assertFalse(magic.call_count)
magic.assert_called_once_with(client) magic.assert_called_once_with(client)
@@ -31,12 +30,12 @@ class TestExternalSystemAdapter(Common):
def test_external_get_client_ensure_one(self): def test_external_get_client_ensure_one(self):
"""It should assert singletons.""" """It should assert singletons."""
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.env['external.system.adapter'].external_get_client() self.env["external.system.adapter"].external_get_client()
def test_external_destroy_client_ensure_one(self): def test_external_destroy_client_ensure_one(self):
"""It should assert singletons.""" """It should assert singletons."""
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.env['external.system.adapter'].external_destroy_client(None) self.env["external.system.adapter"].external_destroy_client(None)
def test_external_test_connection(self): def test_external_test_connection(self):
"""It should raise a UserError.""" """It should raise a UserError."""

View File

@@ -7,7 +7,6 @@ from .common import Common
class TestExternalSystemOs(Common): class TestExternalSystemOs(Common):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
"""Remember the working dir, just in case.""" """Remember the working dir, just in case."""
@@ -22,7 +21,7 @@ class TestExternalSystemOs(Common):
def setUp(self): def setUp(self):
super(TestExternalSystemOs, self).setUp() super(TestExternalSystemOs, self).setUp()
self.record = self.env.ref('base_external_system.external_system_os') self.record = self.env.ref("base_external_system.external_system_os")
def test_external_get_client_returns_os(self): def test_external_get_client_returns_os(self):
"""It should return the Pyhton OS module.""" """It should return the Pyhton OS module."""

View File

@@ -1,21 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<!-- <!--
Copyright 2017 LasLabs Inc. Copyright 2017 LasLabs Inc.
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
--> -->
<odoo> <odoo>
<record id="external_system_view_form" model="ir.ui.view"> <record id="external_system_view_form" model="ir.ui.view">
<field name="name">external.system.view.form</field> <field name="name">external.system.view.form</field>
<field name="model">external.system</field> <field name="model">external.system</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="External System"> <form string="External System">
<header> <header>
<button name="action_test_connection" <button
name="action_test_connection"
type="object" type="object"
string="Test Connection" /> string="Test Connection"
/>
</header> </header>
<sheet> <sheet>
<group name="data"> <group name="data">
@@ -50,7 +49,6 @@
</form> </form>
</field> </field>
</record> </record>
<record id="external_system_view_tree" model="ir.ui.view"> <record id="external_system_view_tree" model="ir.ui.view">
<field name="name">external.system.view.tree</field> <field name="name">external.system.view.tree</field>
<field name="model">external.system</field> <field name="model">external.system</field>
@@ -62,38 +60,39 @@
</tree> </tree>
</field> </field>
</record> </record>
<record id="external_system_view_search" model="ir.ui.view"> <record id="external_system_view_search" model="ir.ui.view">
<field name="name">external.system.view.search</field> <field name="name">external.system.view.search</field>
<field name="model">external.system</field> <field name="model">external.system</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<search string="External Systems"> <search string="External Systems">
<field name="name" /> <field name="name" />
<field name="company_ids" /> <field name="company_ids" />
<field name="host" /> <field name="host" />
<field name="port" /> <field name="port" />
<field name="username" /> <field name="username" />
<group expand="0" string="Group By"> <group expand="0" string="Group By">
<filter string="Host" <filter
string="Host"
name="host" name="host"
domain="" domain=""
context="{'group_by': 'host'}" /> context="{'group_by': 'host'}"
<filter string="Port" />
<filter
string="Port"
name="port" name="port"
domain="" domain=""
context="{'group_by': 'port'}" /> context="{'group_by': 'port'}"
<filter string="Username" />
<filter
string="Username"
name="username" name="username"
domain="" domain=""
context="{'group_by': 'username'}" /> context="{'group_by': 'username'}"
/>
</group> </group>
</search> </search>
</field> </field>
</record> </record>
<record id="external_system_action" model="ir.actions.act_window"> <record id="external_system_action" model="ir.actions.act_window">
<field name="name">External Systems</field> <field name="name">External Systems</field>
<field name="res_model">external.system</field> <field name="res_model">external.system</field>
@@ -101,11 +100,11 @@
<field name="view_type">form</field> <field name="view_type">form</field>
<field name="view_mode">tree,form</field> <field name="view_mode">tree,form</field>
</record> </record>
<menuitem
<menuitem id="menu_external_system" id="menu_external_system"
name="External Systems" name="External Systems"
parent="base.menu_custom" parent="base.menu_custom"
action="external_system_action" action="external_system_action"
sequence="50" /> sequence="50"
/>
</odoo> </odoo>