diff --git a/base_external_system/README.rst b/base_external_system/README.rst index 74a15b938..86a73a412 100644 --- a/base_external_system/README.rst +++ b/base_external_system/README.rst @@ -11,7 +11,7 @@ Base External System !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:2a506ad3e94464097ed6d1a165abccb0f8151ad22ebfa20bf7aa521d308dff01 + !! source digest: sha256:3e86769a6d488f4c2debd3cb5f076fa41da5b207404fab56d493718f66ba2473 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -50,15 +50,23 @@ Configuration Configure external systems in Settings => Technical => External Systems +|Try me on Runbot| + +.. |Try me on Runbot| image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :target: https://runbot.odoo-community.org/runbot/149/10.0 + Usage ===== +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: @@ -77,14 +85,38 @@ 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. +- ``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 18.0.1.0.0 +================== + +Migration to 18.0 + +- All data will now be stored in the external.system model. +- Interface adapters can more easily be extended with functionality. +- Activated clients can be passed information from the main + external.system model. + +Version 16.0.1.0.0 +================== + +Migration to 16.0 + +Version 10.0.2.0.0 +------------------ + +Model base.external.system will be the only regular model, containing +all the data Bug Tracker =========== @@ -103,16 +135,21 @@ Authors ------- * LasLabs +* Therp BV Contributors ------------ -- Dave Lasley -- Ronald Portier -- `Tecnativa `__: +- Dave Lasley dave@laslabs.com + +- Therp BV https://therp.nl: + + - Ronald Portier ronald@therp.nl - - Alexandre Díaz - - César A. Sánchez +- Tecnativa https://www.tecnativa.com: + + - Alexandre Díaz + - César A. Sánchez Maintainers ----------- @@ -127,6 +164,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 9b4799c06..377445902 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": "18.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..7e9edaa43 100644 --- a/base_external_system/demo/external_system_os_demo.xml +++ b/base_external_system/demo/external_system_os_demo.xml @@ -1,12 +1,13 @@ - - 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 04ad36074..2cb4d4166 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-2024 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from contextlib import contextmanager @@ -12,42 +13,47 @@ class ExternalSystem(models.Model): _description = "External System" name = fields.Char( - required=True, - help="This is the canonical (humanized) name for the system.", + 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.", + help="This is the port number that the system is listening on." ) username = fields.Char( - help="This is the username that is used for authenticating to this " - "system, if applicable.", + help="This is the username that is used for authenticating to this" + " system, if applicable." ) password = fields.Char( - help="This is the password that is used for authenticating to this " - "system, if applicable.", + help="This is the password that is used for authenticating to this" + " system, if applicable." ) private_key = fields.Text( - help="This is the private key that is used for authenticating to " - "this system, if applicable.", + help="This is the private key that is used for authenticating to" + " this system, if applicable." ) private_key_password = fields.Text( - help="This is the password to unlock the private key that was " - "provided for this sytem.", + help="This is the password to unlock the private key that was" + " provided for this sytem." + ) + 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 " - "order to validate its identity.", + help="This is the fingerprint that is advertised by this system in" + " order to validate its identity." ) ignore_fingerprint = fields.Boolean( default=True, - help="Set this to `True` in order to ignore an invalid/unknown " - "fingerprint from the system.", + help="Set this to `True` in order to ignore an invalid/unknown" + " fingerprint from the system.", ) 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( string="Companies", @@ -57,25 +63,29 @@ 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."), + ("name_uniq", "UNIQUE(name)", "Connection name must be unique.") ] @api.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): @@ -84,8 +94,8 @@ def check_fingerprint_ignore_fingerprint(self): if not record.ignore_fingerprint and not record.fingerprint: raise ValidationError( _( - "Fingerprint cannot be empty when Ignore Fingerprint is " - "not checked.", + "Fingerprint cannot be empty when Ignore Fingerprint is" + " not checked." ) ) @@ -100,24 +110,55 @@ 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().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_name)s" + ) + % { + "exception": exc, + "system_name": 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..8ac45ed45 100644 --- a/base_external_system/models/external_system_adapter.py +++ b/base_external_system/models/external_system_adapter.py @@ -1,50 +1,30 @@ # 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 +32,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 index 8493c3f68..ec59403ad 100644 --- a/base_external_system/readme/CONFIGURE.md +++ b/base_external_system/readme/CONFIGURE.md @@ -1,2 +1,4 @@ Configure external systems in Settings =\> Technical =\> External Systems + +[![Try me on Runbot](https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas)](https://runbot.odoo-community.org/runbot/149/10.0) diff --git a/base_external_system/readme/CONTRIBUTORS.md b/base_external_system/readme/CONTRIBUTORS.md index 3bd797624..5e43c5634 100644 --- a/base_external_system/readme/CONTRIBUTORS.md +++ b/base_external_system/readme/CONTRIBUTORS.md @@ -1,5 +1,10 @@ -- Dave Lasley \ -- Ronald Portier \ -- [Tecnativa](https://www.tecnativa.com): - - Alexandre Díaz - - César A. Sánchez +* Dave Lasley + + +* Therp BV : + * Ronald Portier + + +* Tecnativa : + * Alexandre Díaz + * César A. Sánchez diff --git a/base_external_system/readme/HISTORY.md b/base_external_system/readme/HISTORY.md new file mode 100644 index 000000000..9449c1226 --- /dev/null +++ b/base_external_system/readme/HISTORY.md @@ -0,0 +1,16 @@ +# Version 18.0.1.0.0 + +Migration to 18.0 + +- All data will now be stored in the external.system model. +- Interface adapters can more easily be extended with functionality. +- Activated clients can be passed information from the main external.system model. + +# Version 16.0.1.0.0 + +Migration to 16.0 + +## Version 10.0.2.0.0 + +Model base.external.system will be the only regular model, containing +all the data diff --git a/base_external_system/readme/USAGE.md b/base_external_system/readme/USAGE.md index 41642c33d..87774bf68 100644 --- a/base_external_system/readme/USAGE.md +++ b/base_external_system/readme/USAGE.md @@ -1,8 +1,10 @@ +## 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 +### Using the Interface Given an `external.system` singleton called `external_system`, you would do the following to get the underlying system client: @@ -20,11 +22,11 @@ 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 +### 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. 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 16206f7c4..0789b09a8 100644 --- a/base_external_system/static/description/index.html +++ b/base_external_system/static/description/index.html @@ -372,7 +372,7 @@

Base External System

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:2a506ad3e94464097ed6d1a165abccb0f8151ad22ebfa20bf7aa521d308dff01 +!! source digest: sha256:3e86769a6d488f4c2debd3cb5f076fa41da5b207404fab56d493718f66ba2473 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

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 @@ -386,15 +386,24 @@

Base External System

  • Configuration
  • Usage @@ -402,14 +411,17 @@

    Base External System

    Configuration

    Configure external systems in Settings => Technical => External Systems

    +

    Try me on Runbot

    Usage

    +
    +

    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

    +

    Using the Interface

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

    @@ -424,7 +436,7 @@ 

    Using the Interface

    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:

    +
    + +
    +

    Version 18.0.1.0.0

    +

    Migration to 18.0

    +
      +
    • All data will now be stored in the external.system model.
    • +
    • Interface adapters can more easily be extended with functionality.
    • +
    • Activated clients can be passed information from the main +external.system model.
    • +
    +
    +
    +

    Version 16.0.1.0.0

    +

    Migration to 16.0

    +
    +

    Version 10.0.2.0.0

    +

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

    +
    +
    -

    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 @@ -443,19 +478,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 @@ -471,6 +510,8 @@

    Maintainers

    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 57aeded4c..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().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 676023817..d65bb1246 100644 --- a/base_external_system/tests/test_external_system_adapter.py +++ b/base_external_system/tests/test_external_system_adapter.py @@ -1,41 +1,24 @@ # Copyright 2017 LasLabs Inc. +# Copyright 2023-2024 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +"""Check wther 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().setUp() - self.system = self.env.ref("base_external_system.external_system_os") - self.record = self.env["external.system.adapter"].new() - - 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 771c436ba..dcf42ceab 100644 --- a/base_external_system/views/external_system_view.xml +++ b/base_external_system/views/external_system_view.xml @@ -1,6 +1,7 @@ @@ -34,12 +35,23 @@ - - - - + + + + + + @@ -49,8 +61,9 @@ - - external.system.view.tree + + + external.system.view.list external.system @@ -60,11 +73,12 @@ + external.system.view.search external.system - + @@ -73,19 +87,19 @@ @@ -93,17 +107,4 @@ - - External Systems - external.system - ir.actions.act_window - list,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..9493e8298 --- /dev/null +++ b/base_external_system/views/ir_ui_menu.xml @@ -0,0 +1,22 @@ + + + + + External Systems + external.system + ir.actions.act_window + list,form + + + +