Project
Function
Sample CLI
gway odoo execute-kw
References
['debug', 'error', 'exception', 'info', 'warning']
Full Code
def execute_kw(
*args, model: str, method: str,
url : str = "[ODOO_BASE_URL]",
db_name : str = "[ODOO_DB_NAME]",
username : str = "[ODOO_ADMIN_USER]",
password : str = "[ODOO_ADMIN_PASSWORD]",
**kwargs
) -> dict:
"""
A generic function to directly interface with Odoo's execute_kw method.
Parameters:
model (str): The Odoo model to interact with (e.g., 'sale.order').
method (str): The method to call on the model (e.g., 'read', 'write').
args (list): Positional arguments to pass to the method.
kwargs (dict): Keyword arguments to pass to the method.
Returns:
dict: The result of the execute_kw call.
"""
gw.info(f"Odoo Execute: {model=} {method=} @ {url=} {db_name=} {username=}")
try:
common_client = client.ServerProxy(f"{url}/xmlrpc/2/common")
except Exception as e:
gw.exception(f"Error with ServerProxy setup", e)
raise
gw.debug(f"ServerProxy client: {common_client}")
try:
uid = common_client.authenticate(db_name, username, password, {})
except Exception as e:
gw.error(f"Error with Odoo authentication: {e}")
print(f"( Did you forget to specify the correct --client? )")
raise
try:
models_client = client.ServerProxy(f"{url}/xmlrpc/2/object")
for reserved in ("db_name", "uid", "password", "model", "method"):
gw.warning(f"Removing reserved keyword: {reserved}")
kwargs.pop(reserved, None)
gw.debug(f"Model client call execute_kw {model}.{method} with {args=} {kwargs=}")
result = models_client.execute_kw(db_name, uid, password, model, method, *args, **kwargs)
return result
except Exception as e:
gw.error(f"Error executing {model}.{method}: {e}")
raise