Skip to content

Commit

Permalink
Merge PR #51 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by simahawk
  • Loading branch information
OCA-git-bot committed Jul 8, 2024
2 parents e752f64 + 4a3d4ec commit 295c5fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions endpoint/models/endpoint_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def _default_code_snippet_docs(self):
* status_code
which are all optional.
Use ``log`` function to log messages into ir.logging table.
"""

def _get_code_snippet_eval_context(self, request):
Expand All @@ -106,8 +108,31 @@ def _get_code_snippet_eval_context(self, request):
"exceptions": safe_eval.wrap_module(
exceptions, ["UserError", "ValidationError"]
),
"log": self._code_snippet_log_func,
}

def _code_snippet_log_func(self, message, level="info"):
# Almost barely copied from ir.actions.server
with self.pool.cursor() as cr:
cr.execute(
"""
INSERT INTO ir_logging
(create_date, create_uid, type, dbname, name, level, message, path, line, func)
VALUES (NOW() at time zone 'UTC', %s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(
self.env.uid,
"server",
self._cr.dbname,
__name__,
level,
message,
"endpoint",
self.id,
self.name,
),
)

def _handle_exec__code(self, request):
if not self._code_snippet_valued():
return {}
Expand Down
18 changes: 18 additions & 0 deletions endpoint/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ def test_endpoint_code_eval_free_vals(self):
payload = result["payload"]
self.assertEqual(json.loads(payload), {"a": 1, "b": 2})

def test_endpoint_log(self):
self.endpoint.write(
{
"code_snippet": textwrap.dedent(
"""
log("ciao")
result = {"ok": True}
"""
)
}
)
with self._get_mocked_request() as req:
# just test that logging does not break
# as it creates a record directly via sql
# and we cannot easily check the result
self.endpoint._handle_request(req)
self.env.cr.execute("DELETE FROM ir_logging")

@mute_logger("endpoint.endpoint", "odoo.modules.registry")
def test_endpoint_validate_request(self):
endpoint = self.endpoint.copy(
Expand Down

0 comments on commit 295c5fc

Please sign in to comment.