Skip to content

Commit

Permalink
Merge pull request #199 from ecosoft-odoo/15.0-upd-usability_webhooks…
Browse files Browse the repository at this point in the history
…-call_function

[15.0][UPD] usability_webhooks: add api call function
  • Loading branch information
Saran440 authored Sep 30, 2024
2 parents d9f812f + 4699eb6 commit 137d4bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions usability_webhooks/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ def search_data(self, model, vals):
self._set_create_logs("webhook.search_data_log", vals)
res = self._create_api_logs(model, vals, "search_data")
return res

@http.route("/api/call_function", type="json", auth="user")
def call_function(self, model, vals):
res = self._create_api_logs(model, vals, "call_function")
return res
37 changes: 37 additions & 0 deletions usability_webhooks/controllers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,40 @@ def search_data(self, model, vals):
}
_logger.info("[{}].search_data(), output: {}".format(model, res))
return res

@api.model
def call_function(self, model, vals):
"""
Call a function on a model object based on the provided input.
Parameters:
- name (str): The name of the model to perform the function on.
- method (str): The name of the function to call.
- parameter (dict):
A dictionary containing the arguments to pass to the function. (if any)
==================================
Example Format for Call Function:
==================================
{
"params": {
"model": "account.move", # Model to call
"vals": {
"payload": {
"name": "INV/2021/0001",
"method": "action_post",
# Optional, see the function definition for required parameters
"parameter": {},
}
}
}
}
"""
_logger.info("[{}].call_function(), input: {}".format(model, vals))
data_dict = vals.get("payload", {})
key = self._search_key(model)
obj = self.env[model].search([(key, "=", data_dict.get(key))])
res = getattr(obj, data_dict["method"])(**dict(data_dict.get("parameter")))
return {
"is_success": True,
"result": res,
"messages": "Function {} called successfully".format(data_dict["method"]),
}

0 comments on commit 137d4bf

Please sign in to comment.