76 lines
2.6 KiB
Python
Executable File
76 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import xmlrpc.client
|
|
import argparse, sys, os, ast
|
|
from urllib.parse import urlparse
|
|
|
|
example_text = """
|
|
example: odooctl --model 'res.partner' --domain "('id', '=', 66)" --field 'name' --value Test
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(
|
|
prog='odooctl',
|
|
description='Update Odoo records using the XML-RPC API.',
|
|
epilog=example_text,
|
|
)
|
|
|
|
# Define arguments
|
|
parser.add_argument('--model', help='Name of model in format "res.partner"')
|
|
parser.add_argument('--domain', help='Domain in format [("field", "operator", "value"), ...]')
|
|
parser.add_argument('--field', help='Field name of model to write or read')
|
|
parser.add_argument('--method', help='Methods are: create, write, read, search_count, search_one, unlink')
|
|
parser.add_argument('--value', help='Value to write')
|
|
|
|
# Parse arguments
|
|
args = parser.parse_args()
|
|
|
|
# Show help if no command is given
|
|
if not any(vars(args).values()):
|
|
parser.print_help()
|
|
sys.exit()
|
|
|
|
# Get environment variables
|
|
url = os.environ.get('ODOO_URL') or 'http://localhost:8069'
|
|
db = os.environ.get('ODOO_DATABASE') or 'odoo'
|
|
username = os.getenv('ODOO_USERNAME') or 'admin'
|
|
password = os.environ.get('ODOO_PASSWORD') or 'admin'
|
|
|
|
# Connect
|
|
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
|
|
uid = common.authenticate(db, username, password, {})
|
|
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
|
|
|
|
# Check permissions
|
|
models.execute_kw(db, uid, password, args.model, 'check_access_rights', ['read'], {'raise_exception': False})
|
|
|
|
# Parse domain and value
|
|
if args.domain:
|
|
domain = ast.literal_eval(args.domain) if args.domain else False
|
|
|
|
if args.value:
|
|
value = ast.literal_eval(args.value)
|
|
|
|
# Execute method
|
|
if args.method == 'create':
|
|
models.execute_kw(db, uid, password, args.model, args.method, [value])
|
|
|
|
if args.method == 'write':
|
|
ids = models.execute_kw(db, uid, password, args.model, 'search', [domain])
|
|
models.execute_kw(db, uid, password, args.model, 'write', [ids, {args.field: value}])
|
|
|
|
if args.method == 'read':
|
|
ids = models.execute_kw(db, uid, password, args.model, 'search', [domain], {'limit': 1})
|
|
res = models.execute_kw(db, uid, password, args.model, 'read', [ids])
|
|
print(res[0][args.field])
|
|
|
|
if args.method == 'search_count':
|
|
count = models.execute_kw(db, uid, password, args.model, 'search_count', [domain])
|
|
print(count)
|
|
|
|
if args.method == 'search_one':
|
|
ids = models.execute_kw(db, uid, password, args.model, 'search', [domain])
|
|
print(ids[0])
|
|
|
|
if args.method == 'unlink':
|
|
ids = models.execute_kw(db, uid, password, args.model, 'search', [domain])
|
|
models.execute_kw(db, uid, password, args.model, 'unlink', [ids]) |