From dc5b2254663b70014ab9ff7e3e96a00be48aab95 Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Tue, 29 Oct 2024 09:24:50 +0100 Subject: [PATCH] [BKP][ADD]endpoint Backport from `web-api` repo v14: https://github.com/OCA/web-api/tree/14.0/endpoint --- endpoint/__init__.py | 1 + endpoint/__manifest__.py | 4 ++-- endpoint/hooks.py | 2 +- endpoint/migrations/14.0.1.1.0/pre-migrate.py | 22 ------------------ .../migrations/14.0.2.0.0/post-migrate.py | 23 ------------------- endpoint/models/endpoint_mixin.py | 21 +++++++++-------- endpoint/readme/CONTRIBUTORS.rst | 1 + endpoint/tests/test_endpoint.py | 6 ++--- endpoint/tests/test_endpoint_controller.py | 11 ++++----- endpoint/tools/__init__.py | 1 + endpoint/tools/misc.py | 3 +++ 11 files changed, 29 insertions(+), 66 deletions(-) delete mode 100644 endpoint/migrations/14.0.1.1.0/pre-migrate.py delete mode 100644 endpoint/migrations/14.0.2.0.0/post-migrate.py create mode 100644 endpoint/tools/__init__.py create mode 100644 endpoint/tools/misc.py diff --git a/endpoint/__init__.py b/endpoint/__init__.py index 817d9aa9..03f8fe54 100644 --- a/endpoint/__init__.py +++ b/endpoint/__init__.py @@ -1,3 +1,4 @@ from .hooks import post_init_hook from . import controllers from . import models +from . import tools diff --git a/endpoint/__manifest__.py b/endpoint/__manifest__.py index 629596a0..ffca8f90 100644 --- a/endpoint/__manifest__.py +++ b/endpoint/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Endpoint", "summary": """Provide custom endpoint machinery.""", - "version": "14.0.2.3.0", + "version": "13.0.1.0.0", "license": "LGPL-3", "development_status": "Beta", "author": "Camptocamp,Odoo Community Association (OCA)", @@ -16,6 +16,6 @@ "security/ir_rule.xml", "views/endpoint_view.xml", ], - "demo": ["demo/endpoint_demo.xml",], + "demo": ["demo/endpoint_demo.xml"], "post_init_hook": "post_init_hook", } diff --git a/endpoint/hooks.py b/endpoint/hooks.py index 701101a3..0a514f15 100644 --- a/endpoint/hooks.py +++ b/endpoint/hooks.py @@ -54,7 +54,7 @@ def _init_server_action(env): } if tools.sql.column_exists(env.cr, "ir_act_server", "activity_user_type"): values["activity_user_type"] = "specific" - ids = query_insert(env.cr, "ir_act_server", [values,],) + ids = query_insert(env.cr, "ir_act_server", [values]) # Finally add an xmlid module, id_ = xid.split(".", 1) diff --git a/endpoint/migrations/14.0.1.1.0/pre-migrate.py b/endpoint/migrations/14.0.1.1.0/pre-migrate.py deleted file mode 100644 index 8fb61166..00000000 --- a/endpoint/migrations/14.0.1.1.0/pre-migrate.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2022 Camptocamp SA (http://www.camptocamp.com) -# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). - -import logging - -from odoo import SUPERUSER_ID, api - -_logger = logging.getLogger(__name__) - - -def migrate(cr, version): - if not version: - return - - env = api.Environment(cr, SUPERUSER_ID, {}) - module = env["ir.module.module"].search( - [("name", "=", "rpc_helper"), ("state", "=", "uninstalled"),] - ) - if module: - _logger.info("Install module rpc_helper") - module.write({"state": "to install"}) - return diff --git a/endpoint/migrations/14.0.2.0.0/post-migrate.py b/endpoint/migrations/14.0.2.0.0/post-migrate.py deleted file mode 100644 index af2834fa..00000000 --- a/endpoint/migrations/14.0.2.0.0/post-migrate.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2022 Camptocamp SA (http://www.camptocamp.com) -# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). - -import logging - -from odoo import SUPERUSER_ID, api - -from odoo.addons.endpoint.hooks import ( # pylint: disable=odoo-addons-relative-import - _init_server_action, -) - -_logger = logging.getLogger(__name__) - - -def migrate(cr, version): - if not version: - return - - env = api.Environment(cr, SUPERUSER_ID, {}) - _init_server_action(env) - domain = [("registry_sync", "=", False)] - env["endpoint.endpoint"].search(domain).write({"registry_sync": True}) - _logger.info("Activate sync for existing endpoints") diff --git a/endpoint/models/endpoint_mixin.py b/endpoint/models/endpoint_mixin.py index 2b9a97c5..7803e089 100644 --- a/endpoint/models/endpoint_mixin.py +++ b/endpoint/models/endpoint_mixin.py @@ -7,10 +7,13 @@ import werkzeug from odoo import _, api, exceptions, fields, http, models -from odoo.tools import safe_eval +from odoo.tools import misc +from odoo.tools.safe_eval import safe_eval from odoo.addons.rpc_helper.decorator import disable_rpc +from ..tools import misc as custom_misc + @disable_rpc() # Block ALL RPC calls class EndpointMixin(models.AbstractModel): @@ -88,22 +91,22 @@ def _default_code_snippet_docs(self): def _get_code_snippet_eval_context(self, request): """Prepare the context used when evaluating python code - :returns: dict -- evaluation context given to safe_eval + :returns: dict -- evaluation context given to tools.misc """ return { "env": self.env, "user": self.env.user, "endpoint": self, "request": request, - "datetime": safe_eval.datetime, - "dateutil": safe_eval.dateutil, - "time": safe_eval.time, - "json": safe_eval.json, + "datetime": misc.datetime, + "dateutil": misc.dateutil, + "time": misc.time, + "json": custom_misc.json, "Response": http.Response, - "werkzeug": safe_eval.wrap_module( + "werkzeug": misc.wrap_module( werkzeug, {"exceptions": ["NotFound", "BadRequest", "Unauthorized"]} ), - "exceptions": safe_eval.wrap_module( + "exceptions": misc.wrap_module( exceptions, ["UserError", "ValidationError"] ), "log": self._code_snippet_log_func, @@ -136,7 +139,7 @@ def _handle_exec__code(self, request): return {} eval_ctx = self._get_code_snippet_eval_context(request) snippet = self.code_snippet - safe_eval.safe_eval(snippet, eval_ctx, mode="exec", nocopy=True) + safe_eval(snippet, eval_ctx, mode="exec", nocopy=True) result = eval_ctx.get("result") if not isinstance(result, dict): raise exceptions.UserError( diff --git a/endpoint/readme/CONTRIBUTORS.rst b/endpoint/readme/CONTRIBUTORS.rst index f1c71bce..37423d21 100644 --- a/endpoint/readme/CONTRIBUTORS.rst +++ b/endpoint/readme/CONTRIBUTORS.rst @@ -1 +1,2 @@ * Simone Orsi +* Guillem Casassas diff --git a/endpoint/tests/test_endpoint.py b/endpoint/tests/test_endpoint.py index 48db35dd..4c05ebb8 100644 --- a/endpoint/tests/test_endpoint.py +++ b/endpoint/tests/test_endpoint.py @@ -25,7 +25,7 @@ def _setup_records(cls): def test_endpoint_unique(self): with self.assertRaises(psycopg2.IntegrityError): self.env["endpoint.endpoint"].create( - {"name": "Endpoint", "route": "/demo/one", "exec_mode": "code",} + {"name": "Endpoint", "route": "/demo/one", "exec_mode": "code"} ) def test_endpoint_validation(self): @@ -229,9 +229,9 @@ def test_registry_sync(self): key = endpoint._endpoint_registry_unique_key() reg = endpoint._endpoint_registry self.assertEqual(reg._get_rule(key), None) - with mock.patch.object(type(self.env.cr.postcommit), "add") as mocked: + with mock.patch.object(type(self.env.cr), "after") as mocked: endpoint.registry_sync = True - partial_func = mocked.call_args[0][0] + partial_func = mocked.call_args[0][1] self.assertEqual(partial_func.args, ([endpoint.id],)) self.assertEqual( partial_func.func.__name__, "_handle_registry_sync_post_commit" diff --git a/endpoint/tests/test_endpoint_controller.py b/endpoint/tests/test_endpoint_controller.py index 5f15d18d..cec205d6 100644 --- a/endpoint/tests/test_endpoint_controller.py +++ b/endpoint/tests/test_endpoint_controller.py @@ -6,17 +6,16 @@ import os from unittest import skipIf -from odoo.tests.common import HttpSavepointCase +from odoo.tests.common import HttpCase from odoo.tools.misc import mute_logger @skipIf(os.getenv("SKIP_HTTP_CASE"), "EndpointHttpCase skipped") -class EndpointHttpCase(HttpSavepointCase): - @classmethod - def setUpClass(cls): - super().setUpClass() +class EndpointHttpCase(HttpCase): + def setUp(self): + super(EndpointHttpCase, self).setUp() # force sync for demo records - cls.env["endpoint.endpoint"].search([])._handle_registry_sync() + self.env["endpoint.endpoint"].search([])._handle_registry_sync() def tearDown(self): self.env["ir.http"]._clear_routing_map() diff --git a/endpoint/tools/__init__.py b/endpoint/tools/__init__.py new file mode 100644 index 00000000..ab665e1d --- /dev/null +++ b/endpoint/tools/__init__.py @@ -0,0 +1 @@ +from . import misc diff --git a/endpoint/tools/misc.py b/endpoint/tools/misc.py new file mode 100644 index 00000000..b475c2ee --- /dev/null +++ b/endpoint/tools/misc.py @@ -0,0 +1,3 @@ +from odoo.tools.misc import wrap_module + +json = wrap_module(__import__("json"), ["loads", "dumps"])