Skip to content

Commit

Permalink
[BKP][ADD]endpoint
Browse files Browse the repository at this point in the history
Backport from `web-api` repo v14: https://github.com/OCA/web-api/tree/14.0/endpoint
  • Loading branch information
GuillemCForgeFlow committed Nov 7, 2024
1 parent 567c66e commit 16e9d5e
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 66 deletions.
1 change: 1 addition & 0 deletions endpoint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .hooks import post_init_hook
from . import controllers
from . import models
from . import tools
4 changes: 2 additions & 2 deletions endpoint/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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",
}
2 changes: 1 addition & 1 deletion endpoint/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 0 additions & 22 deletions endpoint/migrations/14.0.1.1.0/pre-migrate.py

This file was deleted.

23 changes: 0 additions & 23 deletions endpoint/migrations/14.0.2.0.0/post-migrate.py

This file was deleted.

21 changes: 12 additions & 9 deletions endpoint/models/endpoint_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions endpoint/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Simone Orsi <[email protected]>
* Guillem Casassas <[email protected]>
6 changes: 3 additions & 3 deletions endpoint/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 5 additions & 6 deletions endpoint/tests/test_endpoint_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions endpoint/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import misc
3 changes: 3 additions & 0 deletions endpoint/tools/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from odoo.tools.misc import wrap_module

json = wrap_module(__import__("json"), ["loads", "dumps"])

0 comments on commit 16e9d5e

Please sign in to comment.