From 1b0b97b7b7035b0cfa14f3a55f2f403cea005a29 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Tue, 27 Jan 2026 13:02:31 +0100 Subject: [PATCH] [IMP] Make base_external_system easily extendable This module will be extended with modules to connect to http systems, with additional modules to use oauth authorization. --- base_external_system/README.rst | 96 +++++++++++----- base_external_system/__init__.py | 1 - base_external_system/__manifest__.py | 5 +- .../demo/external_system_os_demo.xml | 9 +- base_external_system/models/__init__.py | 3 +- .../models/external_system.py | 93 ++++++++++----- .../models/external_system_adapter.py | 67 +++-------- .../models/external_system_adapter_os.py | 48 ++++++++ base_external_system/readme/CONFIGURE.md | 1 + base_external_system/readme/CONFIGURE.rst | 1 - base_external_system/readme/CONTRIBUTORS.md | 10 ++ base_external_system/readme/CONTRIBUTORS.rst | 6 - base_external_system/readme/DESCRIPTION.md | 7 ++ base_external_system/readme/DESCRIPTION.rst | 7 -- base_external_system/readme/HISTORY.md | 13 +++ base_external_system/readme/USAGE.md | 32 ++++++ base_external_system/readme/USAGE.rst | 32 ------ .../security/ir.model.access.csv | 1 - .../static/description/index.html | 108 ++++++++++++------ base_external_system/tests/__init__.py | 3 +- .../tests/test_external_system.py | 57 +++------ .../tests/test_external_system_adapter.py | 47 +++----- .../tests/test_external_system_adapter_os.py | 43 +++++++ .../views/external_system_view.xml | 45 ++++---- base_external_system/views/ir_ui_menu.xml | 24 ++++ 25 files changed, 463 insertions(+), 296 deletions(-) create mode 100644 base_external_system/models/external_system_adapter_os.py create mode 100644 base_external_system/readme/CONFIGURE.md delete mode 100644 base_external_system/readme/CONFIGURE.rst create mode 100644 base_external_system/readme/CONTRIBUTORS.md delete mode 100644 base_external_system/readme/CONTRIBUTORS.rst create mode 100644 base_external_system/readme/DESCRIPTION.md delete mode 100644 base_external_system/readme/DESCRIPTION.rst create mode 100644 base_external_system/readme/HISTORY.md create mode 100644 base_external_system/readme/USAGE.md delete mode 100644 base_external_system/readme/USAGE.rst create mode 100644 base_external_system/tests/test_external_system_adapter_os.py create mode 100644 base_external_system/views/ir_ui_menu.xml diff --git a/base_external_system/README.rst b/base_external_system/README.rst index ecba11168..383b7ab76 100644 --- a/base_external_system/README.rst +++ b/base_external_system/README.rst @@ -7,7 +7,7 @@ Base External System !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:538c9c995cdcc3f95d664975e426c7a54638c5a728cef8253014ef178d29ec3f + !! source digest: sha256:199072a1b536548cfc88a3f85e558966096b960920cc11d93bdcbabcefc66bf3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -28,13 +28,13 @@ Base External System |badge1| |badge2| |badge3| |badge4| |badge5| -This module provides an interface/adapter mechanism for the definition of remote -systems. +This module provides an interface/adapter mechanism for the definition +of remote systems. -Note that this module stores everything in plain text. In the interest of security, -it is recommended you use another module (such as `keychain` or `red_october` to -encrypt things like the password and private key). This is not done here in order -to not force a specific security method. +Note that this module stores everything in plain text. In the interest +of security, it is recommended you use another module (such as keychain +or red_october to encrypt things like the password and private key). +This is not done here in order to not force a specific security method. **Table of contents** @@ -49,28 +49,31 @@ Configure external systems in Settings => Technical => External Systems Usage ===== -The credentials for systems are stored in the ``external.system`` model, and are to -be configured by the user. This model is the unified interface for the underlying -adapters. +Implementation +-------------- + +The credentials for systems are stored in the ``external.system`` model, +and are to be configured by the user. This model is the unified +interface for the underlying adapters. Using the Interface ~~~~~~~~~~~~~~~~~~~ -Given an ``external.system`` singleton called ``external_system``, you would do the -following to get the underlying system client: +Given an ``external.system`` singleton called ``external_system``, you +would do the following to get the underlying system client: -.. code-block:: python +.. code:: python with external_system.client() as client: client.do_something() -The client will be destroyed once the context has completed. Destruction takes place -in the adapter's ``external_destroy_client`` method. +The client will be destroyed once the context has completed. Destruction +takes place in the adapter's ``external_destroy_client`` method. -The only unified aspect of this interface is the client connection itself. Other more -opinionated interface/adapter mechanisms can be implemented in other modules, such as -the file system interface in `OCA/server-tools/external_file_location -`_. +The only unified aspect of this interface is the client connection +itself. Other more opinionated interface/adapter mechanisms can be +implemented in other modules, such as the file system interface in +`OCA/server-tools/external_file_location `__. Creating an Adapter ~~~~~~~~~~~~~~~~~~~ @@ -78,9 +81,29 @@ Creating an Adapter Modules looking to add an external system adapter should inherit the ``external.system.adapter`` model and override the following methods: -* ``external_get_client``: Returns a usable client for the system -* ``external_destroy_client``: Destroy the connection, if applicable. Does not need - to be defined if the connection destroys itself. +- ``external_get_client``: Returns a usable client for the system +- ``external_destroy_client``: Destroy the connection, if applicable. + Does not need to be defined if the connection destroys itself. + +Changelog +========= + +Version 16.0.1.0.0 +------------------ + +Migration to 16.0 + +Version 16.0.2.0.0 +------------------ + +Model base.external.system will be the only regular model, containing +all the data. + +If people have used this module "as is" to store connection data, no +changes will be needed. In case anybody has implemented a specific +interface, this would need to be changed to an abstract class that +inherits from external.system.adapter, using the example of +``external.system.adapter.os``. Bug Tracker =========== @@ -96,22 +119,27 @@ Credits ======= Authors -~~~~~~~ +------- * LasLabs +* Therp BV Contributors -~~~~~~~~~~~~ +------------ -* Dave Lasley -* Ronald Portier -* `Tecnativa `__: +- Dave Lasley dave@laslabs.com - * Alexandre Díaz - * César A. Sánchez +- Therp BV https://therp.nl: + + - Ronald Portier ronald@therp.nl + +- Tecnativa https://www.tecnativa.com: + + - Alexandre Díaz + - César A. Sánchez Maintainers -~~~~~~~~~~~ +----------- This module is maintained by the OCA. @@ -123,6 +151,14 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. +.. |maintainer-NL66278| image:: https://github.com/NL66278.png?size=40px + :target: https://github.com/NL66278 + :alt: NL66278 + +Current `maintainer `__: + +|maintainer-NL66278| + This module is part of the `OCA/server-backend `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_external_system/__init__.py b/base_external_system/__init__.py index 0639de1b6..75d792451 100644 --- a/base_external_system/__init__.py +++ b/base_external_system/__init__.py @@ -1,4 +1,3 @@ -# Copyright 2017 LasLabs Inc. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from . import models diff --git a/base_external_system/__manifest__.py b/base_external_system/__manifest__.py index b51d6e7c2..62383093d 100644 --- a/base_external_system/__manifest__.py +++ b/base_external_system/__manifest__.py @@ -1,4 +1,5 @@ # Copyright 2017 LasLabs Inc. +# Copyright 2024 Therp BV . # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). { @@ -7,7 +8,8 @@ "version": "16.0.1.0.0", "category": "Base", "website": "https://github.com/OCA/server-backend", - "author": "LasLabs, " "Odoo Community Association (OCA)", + "author": "LasLabs, Therp BV, Odoo Community Association (OCA)", + "maintainers": ["NL66278"], "license": "LGPL-3", "application": False, "installable": True, @@ -16,5 +18,6 @@ "demo/external_system_os_demo.xml", "security/ir.model.access.csv", "views/external_system_view.xml", + "views/ir_ui_menu.xml", ], } diff --git a/base_external_system/demo/external_system_os_demo.xml b/base_external_system/demo/external_system_os_demo.xml index ff6b10fa1..0338f7da2 100644 --- a/base_external_system/demo/external_system_os_demo.xml +++ b/base_external_system/demo/external_system_os_demo.xml @@ -1,13 +1,16 @@ - - Example OS Connection - external.system.os + + + Example OS Connection for Testing + external.system.adapter.os /tmp + diff --git a/base_external_system/models/__init__.py b/base_external_system/models/__init__.py index 84d6fa273..ae725cf3a 100644 --- a/base_external_system/models/__init__.py +++ b/base_external_system/models/__init__.py @@ -1,3 +1,4 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from . import external_system from . import external_system_adapter -from . import external_system_os +from . import external_system_adapter_os diff --git a/base_external_system/models/external_system.py b/base_external_system/models/external_system.py index e2d79435a..abf4fc1a0 100644 --- a/base_external_system/models/external_system.py +++ b/base_external_system/models/external_system.py @@ -1,4 +1,5 @@ # Copyright 2017 LasLabs Inc. +# Copyright 2023-2026 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from contextlib import contextmanager @@ -16,8 +17,11 @@ class ExternalSystem(models.Model): required=True, help="This is the canonical (humanized) name for the system.", ) + scheme = fields.Char( + help="This is the protocol used to communicate with the system.", + ) host = fields.Char( - help="This is the domain or IP address that the system can be reached " "at.", + help="This is the domain or IP address that the system can be reached at.", ) port = fields.Integer( help="This is the port number that the system is listening on.", @@ -36,7 +40,10 @@ class ExternalSystem(models.Model): ) private_key_password = fields.Text( help="This is the password to unlock the private key that was " - "provided for this sytem.", + "provided for this system.", + ) + private_key_thumbprint = fields.Text( + help="The thumbprint generated by AAD when you upload your public cert", ) fingerprint = fields.Text( help="This is the fingerprint that is advertised by this system in " @@ -58,15 +65,10 @@ class ExternalSystem(models.Model): help="Access to this system is restricted to these companies.", ) system_type = fields.Selection( - selection="_get_system_types", + # Use lambda selection, otherwise subclasses loaded later will not be found. + selection=lambda self: self._get_system_types(), required=True, ) - interface = fields.Reference( - selection="_get_system_types", - readonly=True, - help="This is the interface that this system represents. It is " - "created automatically upon creation of the external system.", - ) _sql_constraints = [ ("name_uniq", "UNIQUE(name)", "Connection name must be unique."), @@ -76,7 +78,16 @@ class ExternalSystem(models.Model): def _get_system_types(self): """Return the adapter interface models that are installed.""" adapter = self.env["external.system.adapter"] - return [(m, self.env[m]._description) for m in adapter._inherit_children] + subclasses = set() + work = [adapter] + while work: + parent = work.pop() + for child in parent._inherit_children: + subclass = self.env[child] + if subclass not in subclasses: + subclasses.add(subclass) + work.append(subclass) + return [(m._name, m._description) for m in subclasses] @api.constrains("fingerprint", "ignore_fingerprint") def check_fingerprint_ignore_fingerprint(self): @@ -101,24 +112,52 @@ def client(self): mixed: An object representing the client connection to the remote system. """ - with self.interface.client() as client: + self.ensure_one() + adapter = None + client = None + try: + adapter = self._get_adapter() + client = adapter.external_get_client() yield client - - @api.model_create_multi - def create(self, vals_list): - """Create the interface for the record and assign to ``interface``.""" - records = self.browse([]) - for vals in vals_list: - record = super(ExternalSystem, self).create(vals) - if not self.env.context.get("no_create_interface"): - interface = self.env[vals["system_type"]].create( - {"system_id": record.id} - ) - record.interface = interface - records += record - return records + finally: + if client: + adapter.external_destroy_client(client) def action_test_connection(self): - """Test the connection to the external system.""" + """Test the connection to the external system. + + Any unexpected exception will be transformed into a + ValidationError. A ValidationError will also be raised + if no client is returned. + """ self.ensure_one() - self.interface.external_test_connection() + try: + with self.client() as client: + if client is None: + raise ValidationError( + _("Client connection failed for system %s") % self.name + ) + return True + except Exception as exc: + raise ValidationError( + _( + "Unexpected error %(exception)s when connecting to %(system)s", + exception=exc, + system=self.name, + ) + ) from None + + def _get_adapter(self): + """Trivial method to get adapter from system type. + + Adding the system to the context allows the AbstractModel for the adapter, + that can not be instantiated, to still hold information, through a class + property, that can be accessed by all methods in derived classes. + + An alternative would be to use standard python classes, but that would take + away the possibility to extend them in the Odoo way. + + To even further extend the possibility of the adapter to have its own + runtime memory, we add an (at first) empty dictionary to the context. + """ + return self.with_context(system=self, adapter_memory={}).env[self.system_type] diff --git a/base_external_system/models/external_system_adapter.py b/base_external_system/models/external_system_adapter.py index 8bf09958a..d890b34b6 100644 --- a/base_external_system/models/external_system_adapter.py +++ b/base_external_system/models/external_system_adapter.py @@ -1,50 +1,31 @@ # Copyright 2017 LasLabs Inc. +# Copyright 2023-2024 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +"""Define the interface for external system adapters.""" -from contextlib import contextmanager -from odoo import _, api, fields, models +from odoo import _, api, models from odoo.exceptions import UserError class ExternalSystemAdapter(models.AbstractModel): - """This is the model that should be inherited for new external systems. - - Methods provided are prefixed with ``external_`` in order to keep from - """ + """This is the model that should be inherited for new external systems.""" _name = "external.system.adapter" _description = "External System Adapter" - _inherits = {"external.system": "system_id"} - - system_id = fields.Many2one( - string="System", - comodel_name="external.system", - required=True, - ondelete="cascade", - ) - @contextmanager - def client(self): - """Client object usable as a context manager to include destruction. + def get_system(self): + """Get system from environment.""" + return self.env.context.get("system", None) - Yields the result from ``external_get_client``, then calls - ``external_destroy_client`` to cleanup the client. - - Yields: - mixed: An object representing the client connection to the remote - system. - """ - client = self.external_get_client() - try: - yield client - finally: - self.external_destroy_client(client) + system_id = property(get_system) + @api.model def external_get_client(self): """Return a usable client representing the remote system.""" - self.ensure_one() + self._raise_not_implemented() + @api.model def external_destroy_client(self, client): """Perform any logic necessary to destroy the client connection. @@ -52,26 +33,8 @@ def external_destroy_client(self, client): client (mixed): The client that was returned by ``external_get_client``. """ - self.ensure_one() - - def external_test_connection(self): - """Adapters should override this method, then call super if valid. - - If the connection is invalid, adapters should raise an instance of - ``odoo.ValidationError``. - - Raises: - odoo.exceptions.UserError: In the event of a good connection. - """ - raise UserError(_("The connection was a success.")) + self._raise_not_implemented() - @api.model_create_multi - def create(self, vals_list): - context_self = self.with_context(no_create_interface=True) - records = self.browse([]) - for vals in vals_list: - vals.update({"system_type": self._name}) - record = super(ExternalSystemAdapter, context_self).create(vals) - record.system_id.interface = record - records += record - return records + def _raise_not_implemented(self): + """Raise errors for methods that should be implemented in subclass.""" + raise UserError(_("Method not implemented in base adapter class.")) diff --git a/base_external_system/models/external_system_adapter_os.py b/base_external_system/models/external_system_adapter_os.py new file mode 100644 index 000000000..34dc02c04 --- /dev/null +++ b/base_external_system/models/external_system_adapter_os.py @@ -0,0 +1,48 @@ +# Copyright 2017 LasLabs Inc. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +import os + +from odoo import api, models + + +class ExternalSystemAdapterOs(models.AbstractModel): + """This is an Interface implementing the OS module. + + For the most part, this is just a sample of how to implement an external + system interface. This is still a fully usable implementation, however. + """ + + _name = "external.system.adapter.os" + _inherit = "external.system.adapter" + _description = "External System OS" + + def get_previous_dir(self): + """Get previous_dir from adapter_memory.""" + return self.env.context["adapter_memory"].get("previous_dir", None) + + def set_previous_dir(self, value): + """Store previous_dir in adapter_memor.""" + self.env.context["adapter_memory"]["previous_dir"] = value + + def del_previous_dir(self): + """Get system from environment.""" + if "previous_dir" in self.env.context["adapter_memory"]: + del self.env.context["adapter_memory"]["previous_dir"] + + previous_dir = property(get_previous_dir, set_previous_dir, del_previous_dir) + + @api.model + def external_get_client(self): + """Return a usable client representing the remote system.""" + if self.system_id.remote_path: + self.previous_dir = os.getcwd() + os.chdir(self.system_id.remote_path) + return os + + @api.model + def external_destroy_client(self, client): + """Perform any logic necessary to destroy the client connection.""" + if self.previous_dir: + os.chdir(self.previous_dir) + del self.previous_dir diff --git a/base_external_system/readme/CONFIGURE.md b/base_external_system/readme/CONFIGURE.md new file mode 100644 index 000000000..58aa37c10 --- /dev/null +++ b/base_external_system/readme/CONFIGURE.md @@ -0,0 +1 @@ +Configure external systems in Settings =\> Technical =\> External Systems diff --git a/base_external_system/readme/CONFIGURE.rst b/base_external_system/readme/CONFIGURE.rst deleted file mode 100644 index 0efcd8672..000000000 --- a/base_external_system/readme/CONFIGURE.rst +++ /dev/null @@ -1 +0,0 @@ -Configure external systems in Settings => Technical => External Systems diff --git a/base_external_system/readme/CONTRIBUTORS.md b/base_external_system/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..5e43c5634 --- /dev/null +++ b/base_external_system/readme/CONTRIBUTORS.md @@ -0,0 +1,10 @@ +* Dave Lasley + + +* Therp BV : + * Ronald Portier + + +* Tecnativa : + * Alexandre Díaz + * César A. Sánchez diff --git a/base_external_system/readme/CONTRIBUTORS.rst b/base_external_system/readme/CONTRIBUTORS.rst deleted file mode 100644 index f484d8b9c..000000000 --- a/base_external_system/readme/CONTRIBUTORS.rst +++ /dev/null @@ -1,6 +0,0 @@ -* Dave Lasley -* Ronald Portier -* `Tecnativa `__: - - * Alexandre Díaz - * César A. Sánchez diff --git a/base_external_system/readme/DESCRIPTION.md b/base_external_system/readme/DESCRIPTION.md new file mode 100644 index 000000000..bbf18523d --- /dev/null +++ b/base_external_system/readme/DESCRIPTION.md @@ -0,0 +1,7 @@ +This module provides an interface/adapter mechanism for the definition +of remote systems. + +Note that this module stores everything in plain text. In the interest +of security, it is recommended you use another module (such as keychain +or red_october to encrypt things like the password and private key). +This is not done here in order to not force a specific security method. diff --git a/base_external_system/readme/DESCRIPTION.rst b/base_external_system/readme/DESCRIPTION.rst deleted file mode 100644 index c06b08180..000000000 --- a/base_external_system/readme/DESCRIPTION.rst +++ /dev/null @@ -1,7 +0,0 @@ -This module provides an interface/adapter mechanism for the definition of remote -systems. - -Note that this module stores everything in plain text. In the interest of security, -it is recommended you use another module (such as `keychain` or `red_october` to -encrypt things like the password and private key). This is not done here in order -to not force a specific security method. diff --git a/base_external_system/readme/HISTORY.md b/base_external_system/readme/HISTORY.md new file mode 100644 index 000000000..cf757dff5 --- /dev/null +++ b/base_external_system/readme/HISTORY.md @@ -0,0 +1,13 @@ +## Version 16.0.1.0.0 + +Migration to 16.0 + +## Version 16.0.2.0.0 + +Model base.external.system will be the only regular model, containing +all the data. + +If people have used this module "as is" to store connection data, no changes +will be needed. In case anybody has implemented a specific interface, +this would need to be changed to an abstract class that inherits from +external.system.adapter, using the example of `external.system.adapter.os`. diff --git a/base_external_system/readme/USAGE.md b/base_external_system/readme/USAGE.md new file mode 100644 index 000000000..a2b96b26b --- /dev/null +++ b/base_external_system/readme/USAGE.md @@ -0,0 +1,32 @@ +## Implementation + +The credentials for systems are stored in the `external.system` model, +and are to be configured by the user. This model is the unified +interface for the underlying adapters. + +### Using the Interface + +Given an `external.system` singleton called `external_system`, you would +do the following to get the underlying system client: + +``` python +with external_system.client() as client: + client.do_something() +``` + +The client will be destroyed once the context has completed. Destruction +takes place in the adapter's `external_destroy_client` method. + +The only unified aspect of this interface is the client connection +itself. Other more opinionated interface/adapter mechanisms can be +implemented in other modules, such as the file system interface in +[OCA/server-tools/external_file_location](https://github.com/OCA/server-tools/tree/9.0/external_file_location). + +### Creating an Adapter + +Modules looking to add an external system adapter should inherit the +`external.system.adapter` model and override the following methods: + +- `external_get_client`: Returns a usable client for the system +- `external_destroy_client`: Destroy the connection, if applicable. Does + not need to be defined if the connection destroys itself. diff --git a/base_external_system/readme/USAGE.rst b/base_external_system/readme/USAGE.rst deleted file mode 100644 index 7f358b53d..000000000 --- a/base_external_system/readme/USAGE.rst +++ /dev/null @@ -1,32 +0,0 @@ -The credentials for systems are stored in the ``external.system`` model, and are to -be configured by the user. This model is the unified interface for the underlying -adapters. - -Using the Interface -~~~~~~~~~~~~~~~~~~~ - -Given an ``external.system`` singleton called ``external_system``, you would do the -following to get the underlying system client: - -.. code-block:: python - - with external_system.client() as client: - client.do_something() - -The client will be destroyed once the context has completed. Destruction takes place -in the adapter's ``external_destroy_client`` method. - -The only unified aspect of this interface is the client connection itself. Other more -opinionated interface/adapter mechanisms can be implemented in other modules, such as -the file system interface in `OCA/server-tools/external_file_location -`_. - -Creating an Adapter -~~~~~~~~~~~~~~~~~~~ - -Modules looking to add an external system adapter should inherit the -``external.system.adapter`` model and override the following methods: - -* ``external_get_client``: Returns a usable client for the system -* ``external_destroy_client``: Destroy the connection, if applicable. Does not need - to be defined if the connection destroys itself. diff --git a/base_external_system/security/ir.model.access.csv b/base_external_system/security/ir.model.access.csv index f3fafd3ef..360e6a2e8 100644 --- a/base_external_system/security/ir.model.access.csv +++ b/base_external_system/security/ir.model.access.csv @@ -1,3 +1,2 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_external_system_os_admin,access_external_system_os_admin,model_external_system_os,base.group_system,1,1,1,1 access_external_system_admin,access_external_system_admin,model_external_system,base.group_system,1,1,1,1 diff --git a/base_external_system/static/description/index.html b/base_external_system/static/description/index.html index b1a2b0df6..06ae79f05 100644 --- a/base_external_system/static/description/index.html +++ b/base_external_system/static/description/index.html @@ -1,4 +1,3 @@ - @@ -367,29 +366,37 @@

Base External System

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:538c9c995cdcc3f95d664975e426c7a54638c5a728cef8253014ef178d29ec3f +!! source digest: sha256:199072a1b536548cfc88a3f85e558966096b960920cc11d93bdcbabcefc66bf3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: LGPL-3 OCA/server-backend Translate me on Weblate Try me on Runboat

-

This module provides an interface/adapter mechanism for the definition of remote -systems.

-

Note that this module stores everything in plain text. In the interest of security, -it is recommended you use another module (such as keychain or red_october to -encrypt things like the password and private key). This is not done here in order -to not force a specific security method.

+

This module provides an interface/adapter mechanism for the definition +of remote systems.

+

Note that this module stores everything in plain text. In the interest +of security, it is recommended you use another module (such as keychain +or red_october to encrypt things like the password and private key). +This is not done here in order to not force a specific security method.

Table of contents

Usage

-

The credentials for systems are stored in the external.system model, and are to -be configured by the user. This model is the unified interface for the underlying -adapters.

+
+

Implementation

+

The credentials for systems are stored in the external.system model, +and are to be configured by the user. This model is the unified +interface for the underlying adapters.

-

Using the Interface

-

Given an external.system singleton called external_system, you would do the -following to get the underlying system client:

+

Using the Interface

+

Given an external.system singleton called external_system, you +would do the following to get the underlying system client:

 with external_system.client() as client:
     client.do_something()
 
-

The client will be destroyed once the context has completed. Destruction takes place -in the adapter’s external_destroy_client method.

-

The only unified aspect of this interface is the client connection itself. Other more -opinionated interface/adapter mechanisms can be implemented in other modules, such as -the file system interface in OCA/server-tools/external_file_location.

+

The client will be destroyed once the context has completed. Destruction +takes place in the adapter’s external_destroy_client method.

+

The only unified aspect of this interface is the client connection +itself. Other more opinionated interface/adapter mechanisms can be +implemented in other modules, such as the file system interface in +OCA/server-tools/external_file_location.

-

Creating an Adapter

+

Creating an Adapter

Modules looking to add an external system adapter should inherit the external.system.adapter model and override the following methods:

  • external_get_client: Returns a usable client for the system
  • -
  • external_destroy_client: Destroy the connection, if applicable. Does not need -to be defined if the connection destroys itself.
  • +
  • external_destroy_client: Destroy the connection, if applicable. +Does not need to be defined if the connection destroys itself.
+
+
+

Changelog

+
+

Version 16.0.1.0.0

+

Migration to 16.0

+
+
+

Version 16.0.2.0.0

+

Model base.external.system will be the only regular model, containing +all the data.

+

If people have used this module “as is” to store connection data, no +changes will be needed. In case anybody has implemented a specific +interface, this would need to be changed to an abstract class that +inherits from external.system.adapter, using the example of +external.system.adapter.os.

+
+
-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -437,19 +465,23 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • LasLabs
  • +
  • Therp BV
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

+

Current maintainer:

+

NL66278

This module is part of the OCA/server-backend project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

diff --git a/base_external_system/tests/__init__.py b/base_external_system/tests/__init__.py index c91c801ac..01353cd75 100644 --- a/base_external_system/tests/__init__.py +++ b/base_external_system/tests/__init__.py @@ -1,3 +1,4 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from . import test_external_system from . import test_external_system_adapter -from . import test_external_system_os +from . import test_external_system_adapter_os diff --git a/base_external_system/tests/test_external_system.py b/base_external_system/tests/test_external_system.py index b1f212baf..4867f6340 100644 --- a/base_external_system/tests/test_external_system.py +++ b/base_external_system/tests/test_external_system.py @@ -1,20 +1,23 @@ # Copyright 2017 LasLabs Inc. +# Copyright 2023-2024 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +import os -from odoo.exceptions import UserError, ValidationError +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase -from .common import Common - -class TestExternalSystem(Common): - def setUp(self): - super(TestExternalSystem, self).setUp() - self.record = self.env.ref("base_external_system.external_system_os") +class TestExternalSystem(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.record = cls.env.ref("base_external_system.external_system_os_demo") def test_get_system_types(self): """It should return at least the test record's interface.""" + system_type_os = self.env["external.system.adapter.os"] self.assertIn( - (self.record._name, self.record._description), + (system_type_os._name, system_type_os._description), self.env["external.system"]._get_system_types(), ) @@ -25,44 +28,14 @@ def test_check_fingerprint_blank(self): def test_check_fingerprint_allowed(self): """It should not raise a validation error if there is a fingerprint.""" - # In Odoo 13.0, due to the way inverse records (models inherited from) - # are handled, setting both fields at the same time causes an error. - self.record.write({"fingerprint": "Data"}) - self.record.write({"ignore_fingerprint": False}) + self.record.write({"ignore_fingerprint": False, "fingerprint": "Data"}) self.assertTrue(True) def test_client(self): """It should yield the open interface client.""" - with self._mock_method("client", self.record) as magic: - with self.record.system_id.client() as client: - self.assertEqual(client, magic().__enter__()) - - 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( - 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) + with self.record.client() as client: + self.assertEqual(client, os) 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()) + self.record.action_test_connection() diff --git a/base_external_system/tests/test_external_system_adapter.py b/base_external_system/tests/test_external_system_adapter.py index f5ce4c07c..386fc0dd8 100644 --- a/base_external_system/tests/test_external_system_adapter.py +++ b/base_external_system/tests/test_external_system_adapter.py @@ -1,43 +1,24 @@ # Copyright 2017 LasLabs Inc. +# Copyright 2023-2026 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +"""Check whether calling not implemented methods on abstract class raise exceptions.""" from odoo.exceptions import UserError +from odoo.tests.common import TransactionCase -from .common import Common +class TestExternalSystemAdapter(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.abstract_adapter = cls.env["external.system.adapter"] -class TestExternalSystemAdapter(Common): - def setUp(self): - super(TestExternalSystemAdapter, self).setUp() - self.system = self.env.ref("base_external_system.external_system_os") - self.record = self.env["external.system.adapter"].new( - {"system_id": self.system.id} - ) - - def test_client_yields_client(self): - """It should yield the client.""" - with self._mock_method("external_get_client") as magic: - with self.record.client() as client: - self.assertEqual(client, magic()) - - def test_client_destroys_client(self): - """It should destroy the client after use.""" - with self._mock_method("external_destroy_client") as magic: - with self.record.client() as client: - self.assertFalse(magic.call_count) - magic.assert_called_once_with(client) - - def test_external_get_client_ensure_one(self): - """It should assert singletons.""" - with self.assertRaises(ValueError): - self.env["external.system.adapter"].external_get_client() - - def test_external_destroy_client_ensure_one(self): - """It should assert singletons.""" - with self.assertRaises(ValueError): - self.env["external.system.adapter"].external_destroy_client(None) + def test_external_get_client(self): + """It should raise a UserError.""" + with self.assertRaises(UserError): + self.abstract_adapter.external_get_client() - def test_external_test_connection(self): + def test_external_destroy_client(self): """It should raise a UserError.""" with self.assertRaises(UserError): - self.record.external_test_connection() + self.abstract_adapter.external_destroy_client(None) diff --git a/base_external_system/tests/test_external_system_adapter_os.py b/base_external_system/tests/test_external_system_adapter_os.py new file mode 100644 index 000000000..3604f1a4c --- /dev/null +++ b/base_external_system/tests/test_external_system_adapter_os.py @@ -0,0 +1,43 @@ +# Copyright 2017 LasLabs Inc. +# Copyright 2023-2024 Therp BV. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +import os + +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestExternalSystemAdapterOs(TransactionCase): + @classmethod + def setUpClass(cls): + """Remember the working dir, just in case.""" + super().setUpClass() + cls.working_dir = os.getcwd() + cls.record = cls.env.ref("base_external_system.external_system_os_demo") + + @classmethod + def tearDownClass(cls): + """Set the working dir back to origin, just in case.""" + super().tearDownClass() + os.chdir(cls.working_dir) + + def test_external_system_adapter_os(self): + """Client should be os, change directory and on destroy restore directory.""" + with self.record.client() as client: + self.assertEqual(client, os) + self.assertEqual(os.getcwd(), self.record.remote_path) + self.assertEqual(os.getcwd(), self.working_dir) + + def test_external_system_adapter_os_test_connection(self): + """Test testing connections. + + Connecting to existing generally available directory should work. + + Connection to non-existing (or non authorized) directory should + result in ValidationError. + """ + self.assertEqual(True, self.record.action_test_connection()) + self.record.remote_path = "/does_not_exist_never_heard_from_666" + with self.assertRaises(ValidationError): + self.record.action_test_connection() diff --git a/base_external_system/views/external_system_view.xml b/base_external_system/views/external_system_view.xml index ec0ee2546..6d1ffc9ff 100644 --- a/base_external_system/views/external_system_view.xml +++ b/base_external_system/views/external_system_view.xml @@ -1,9 +1,11 @@ + external.system.view.form external.system @@ -34,12 +36,23 @@ - - - - + + + + + + @@ -49,6 +62,7 @@ + external.system.view.tree external.system @@ -60,11 +74,12 @@ + external.system.view.search external.system - + @@ -73,19 +88,19 @@ @@ -93,17 +108,5 @@ - - External Systems - external.system - ir.actions.act_window - tree,form - - + diff --git a/base_external_system/views/ir_ui_menu.xml b/base_external_system/views/ir_ui_menu.xml new file mode 100644 index 000000000..7114f2be7 --- /dev/null +++ b/base_external_system/views/ir_ui_menu.xml @@ -0,0 +1,24 @@ + + + + + + External Systems + external.system + ir.actions.act_window + tree,form + + + + +