From f329c8201eb72bea6b32dc1fff014ded9c21d78e Mon Sep 17 00:00:00 2001 From: Saran440 Date: Mon, 10 Nov 2025 12:17:17 +0700 Subject: [PATCH] [IMP] usability_webhooks: implement context when call function --- usability_webhooks/controllers/utils.py | 71 +++++++++++++++++-------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/usability_webhooks/controllers/utils.py b/usability_webhooks/controllers/utils.py index 0fc20d32..117c8533 100644 --- a/usability_webhooks/controllers/utils.py +++ b/usability_webhooks/controllers/utils.py @@ -621,43 +621,68 @@ def search_data(self, model, vals): @api.model def call_function(self, model, vals): """ - Call a function on a model object based on the provided input. - Parameters (search_key) are used to search for the record: - - search_key: - A dictionary containing the search criteria to find the record. - - Parameters (payload) are used to call the function: - - 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: - ================================== + Call a method on a specific model record using the provided input. + + This method allows you to dynamically call a function on a model object + with optional parameters and context. + + Parameters + ---------- + model : str + The name of the model to call the function on. + vals : dict + A dictionary containing the following keys: + - search_key : dict + Criteria used to search for the target record. + - payload : dict + - method (str): The name of the method to call on the record. + - parameter (dict, optional): Arguments to pass to the method. + - context (dict, optional): Context to use when calling the method. + + Returns + ------- + dict + A dictionary containing: + - is_success (bool): True if the method call succeeded. + - result (any): The return value of the called method. + - messages (str): Status message. + + Example + ------- { "params": { - "model": "account.move", # Model to call + "model": "account.move", "vals": { - "search_key": { - "name": "INV/2021/0001" - }, + "search_key": {"name": "INV/2021/0001"}, "payload": { "method": "action_post", - # Optional, see the function definition for required parameters "parameter": {}, + "context": {"force_create": True}, } } } } """ - _logger.info(f"[{model}].call_function(), input: {vals}") + _logger.info("[%s].call_function(), input: %s", model, vals) + data_dict = vals.get("payload", {}) + method_name = data_dict.get("method") parameter = data_dict.get("parameter", {}) + context = data_dict.get("context", {}) - rec = self._search_object(model, vals) - res = getattr(rec, data_dict["method"])(**dict(parameter) if parameter else {}) + if not method_name: + raise ValidationError(self.env._("Missing 'method' in payload")) + + rec = self._search_object(model, vals).with_context(**context) + + if not hasattr(rec, method_name): + raise AttributeError( + f"Record(s) of model {model} has no method '{method_name}'" + ) + + result = getattr(rec, method_name)(**(parameter or {})) return { "is_success": True, - "result": res, - "messages": "Function {} called successfully".format(data_dict["method"]), + "result": result, + "messages": f"Function '{method_name}' called successfully", }