exportoo/remote.py

540 lines
14 KiB
Python
Raw Normal View History

2022-01-14 14:27:23 +01:00
#!/usr/bin/env python
2022-01-18 00:37:13 +01:00
## Usage:
## ./remote.py <MODEL> <METHOD> [<param> [<param> [<param>]]] [+<opt>] [+<opt=value>]
## Where <opt> are:
## json Renders json data.
## tsv Renders tsv data.
## csv Renders csv data.
## xml Soon WIP Renders xml data.
## recurse Execute the <METHOD> recursively.
## recurse=L Execute the <METHOD> recursively, limiting
## to model names in L being a list with comma and no spaces.
2022-01-14 14:27:23 +01:00
from __future__ import print_function
import os
import json
import odoorpc # pip install odoorpc
2022-01-17 14:39:27 +01:00
#from typing import Iterable # > py38
2022-01-17 19:09:09 +01:00
# from collections import Iterable # < py38
2022-01-14 14:27:23 +01:00
2022-01-18 00:37:13 +01:00
from config import *
2022-01-17 19:04:33 +01:00
from utils import *
from renderers import render, tsv
2022-01-14 14:27:23 +01:00
2022-01-17 14:39:27 +01:00
2022-01-17 19:04:33 +01:00
# ODOO
2022-01-17 14:39:27 +01:00
2022-01-17 19:04:33 +01:00
# Prepare the connection to the server
# odoo = odoorpc.ODOO('localhost', port=8069)
2022-01-17 14:39:27 +01:00
2022-01-17 19:04:33 +01:00
# Check available databases
# eprint( 'Databases: ', odoo.db.list() )
#jlog( odoo.db.list() )
2022-01-14 14:27:23 +01:00
odoo = None
def autolog():
# Then, use the odoorpc.ODOO.load() class method:
2022-01-17 23:12:58 +01:00
global odoo
2022-01-14 14:27:23 +01:00
odoo = odoorpc.ODOO.load('session')
# Or, if you have saved your configuration in another file:
# odoo = odoorpc.ODOO.load('tutorial', '~/my_own_odoorpcrc')
# You can check available sessions with odoorpc.ODOO.list(), and remove them with odoorpc.ODOO.remove():
# odoorpc.ODOO.list()
# ['session']
# odoorpc.ODOO.remove('tutorial')
# 'tutorial' not in odoorpc.ODOO.list()
# True
# Current user
user = odoo.env.user
eprint( 'User: ', user.name ) # name of the user connected
eprint( 'Company: ', user.company_id.name ) # the name of its company
eprint( '_________________________' )
2022-01-17 23:12:58 +01:00
# return odoo
2022-01-14 14:27:23 +01:00
# Simple 'raw' query
#user_data = odoo.execute('res.users', 'read', [user.id])
#print(user_data)
# Use all methods of a model
#if 'sale.order' in odoo.env:
# Order = odoo.env['sale.order']
# order_ids = Order.search([])
# for order in Order.browse(order_ids):
# print(order.name)
# products = [line.product_id.name for line in order.order_line]
# print(products)
# Update data through a record
#user.name = "Brian Jones"
def get_schema( name ):
eprint( 'get_schema' )
Model = odoo.env['ir.model']
ids = Model.search([( 'model', '=', name )])
#print( ids )
for model in Model.browse( ids ):
obj = model.read()[0]
fields = [ field.read()[0] for field in model.field_id ]
#jlog( fields )
obj['field_id'] = fields
jlog( transform_model(obj) )
eprint( "%s/schemas/%s.json" % ( DATADIR, model.model ) )
2022-01-14 14:27:23 +01:00
def transform_model( obj ):
return { "name": obj['model'], "modules": obj['modules'], 'fields': [ {} for field in obj['field_id'] ] }
def rel_fields( model ):
Model = odoo.env['ir.model.fields']
ids = Model.search([( 'model_id.model', '=', model ), ('relation','!=','false')])
return Model.browse( ids )
# COMMANDS
def login( server = None, port = None, db = None, user = None, passwd = None ):
2022-01-17 23:12:58 +01:00
global odoo
2022-01-14 14:27:23 +01:00
if server:
tsv( 'Server:', server )
else:
server = raw_input('Server address: ')
if port:
tsv( 'Port:', port )
else:
port = raw_input('Port: ')
odoo = odoorpc.ODOO( server, port=port )
print( 'Connected to ' + server + ':' + port )
print( 'Available databases:' )
for _db in odoo.db.list():
print( _db )
if db:
tsv( 'Database:', db )
else:
db = raw_input('Choose database: ')
if user:
tsv( 'User:', user )
else:
user = raw_input('User: ')
if passwd:
tsv( 'Password:', '****************' )
else:
passwd = raw_input('Password: ')
eprint( server, port, user, passwd )
user = odoo.login( db, user, passwd )
odoo.save('session')
# # By default, these informations are stored in the ~/.odoorpcrc file. You can however use another file:
# # odoo.save('tutorial', '~/my_own_odoorpcrc')
def search( model, domains ):
Model = odoo.env[model]
ids = Model.search( domains )
2022-01-16 13:49:08 +01:00
return ids
2022-01-14 19:49:59 +01:00
# render_tsv( model, Model.browse(ids) )
2022-01-14 14:27:23 +01:00
# print( 'id', 'name', sep='\t' )
# for inst in Model.browse( ids ):
# render_tsv( model, inst )
def fields( model, domains ):
Model = odoo.env['ir.model.fields']
2022-01-17 12:28:35 +01:00
domains = [( 'model_id.model', '=', model )] + domains
ids = Model.search( domains )
2022-01-17 12:32:02 +01:00
return Model.browse(ids)
2022-01-14 14:27:23 +01:00
2022-01-14 19:49:59 +01:00
stack = []
2022-01-14 14:27:23 +01:00
current_exports = []
2022-01-16 13:59:19 +01:00
2022-01-17 12:57:28 +01:00
def lookup_file( muid, format ):
filename = file_path( muid, format )
# print( field.ttype, field.relation, id, _filename )
if not os.path.exists( filename ):
return { 'file': filename, 'exists': False }
else:
return { 'file': filename, 'exists': True }
2022-01-16 13:44:50 +01:00
2022-01-17 13:15:03 +01:00
_visited = []
2022-01-17 23:44:30 +01:00
def lookup_model( muid, recurse = False ):
2022-01-16 13:44:50 +01:00
eprint( "> Lookup model: %s" % (muid) )
2022-01-17 23:44:30 +01:00
# recursive = False
# if ( 'recursive' in OPTS and OPTS['recursive'] ): # or ( 'format' in OPTS and 'json' in OPTS['format'] ):
# recursive = OPTS['recursive']
if type(recurse) == str:
recurse = recurse.split(',')
eprint( "> Recursive: %s %s" % (recurse, type(recurse)) )
2022-01-17 12:57:28 +01:00
2022-01-16 15:40:40 +01:00
_tree = {}
2022-01-16 16:03:50 +01:00
# _ctree = _tree
2022-01-16 13:59:19 +01:00
model, id = muid.split( '/' )
2022-01-16 14:01:04 +01:00
id = int(id)
2022-01-16 13:52:05 +01:00
eprint( model, id )
2022-01-16 15:40:40 +01:00
_tree[muid] = {}
2022-01-17 13:15:03 +01:00
if muid in _visited:
_tree[muid]['error'] = 'Cyclic %s already visited' % (muid)
return _tree
2022-01-16 16:03:50 +01:00
# _ctree = _tree[muid]
2022-01-16 13:52:05 +01:00
if model in odoo.env:
2022-01-16 13:44:50 +01:00
rfields = rel_fields( model )
eprint( rfields )
2022-01-16 16:03:50 +01:00
try:
inst = odoo.env[model].browse( id )
except:
_tree[muid]['error'] = 'Instance %s not found' % (muid)
2022-01-16 16:23:48 +01:00
return _tree
2022-01-17 13:15:03 +01:00
_visited.append( muid )
2022-01-16 13:44:50 +01:00
# data = inst.read()[0]
# size = len( json.dumps(data, indent=4) )
# filename = json_path( model, inst.id )
2022-01-16 16:03:50 +01:00
# _tree[muid]['size'] = size
2022-01-16 13:44:50 +01:00
for field in rfields:
# if data[field.name]:
2022-01-16 16:03:50 +01:00
# _tree[muid][field.name] = {}
2022-01-16 14:08:33 +01:00
_value = getattr( inst, field.name )
2022-01-16 14:21:28 +01:00
print( field.name )
2022-01-16 14:08:33 +01:00
print( _value )
2022-01-16 15:14:13 +01:00
if _value:
print( field.name, field.ttype )
2022-01-16 13:44:50 +01:00
if field.ttype == 'many2one':
2022-01-16 14:08:33 +01:00
id = _value.id
2022-01-16 15:40:40 +01:00
# _stack.append( Muid(field.relation, id) )
2022-01-16 16:03:50 +01:00
_tree[muid][field.name] = [ Muid(field.relation, id) ]
2022-01-16 13:44:50 +01:00
if field.ttype == 'one2many':
2022-01-16 16:03:50 +01:00
_tree[muid][field.name] = [ Muid(field.relation, id) for id in _value.ids ]
2022-01-16 15:40:40 +01:00
# for id in _value.ids:
# _stack.append( Muid(field.relation, id) )
2022-01-16 14:08:33 +01:00
# if field.ttype == 'many2many':
# if field.ttype == 'reference':
2022-01-17 13:15:03 +01:00
2022-01-17 23:46:50 +01:00
if recurse == True or type(recurse) == list and field.relation in recurse:
2022-01-17 13:17:26 +01:00
_tree[muid][field.name] = [ lookup_model( id ) for id in _tree[muid][field.name] ]
2022-01-17 13:15:03 +01:00
2022-01-16 15:40:40 +01:00
else:
2022-01-16 16:03:50 +01:00
_tree[muid]['error'] = 'Model do not exists'
2022-01-16 15:40:40 +01:00
2022-01-16 16:23:48 +01:00
return _tree
2022-01-17 23:44:30 +01:00
def lookup( model, domains, recurse = False ):
2022-01-17 12:57:28 +01:00
eprint( "> Lookup many: %s %s" % (model,domains) )
2022-02-06 18:15:59 +01:00
# if model in MODEL_IGNORE:
# eprint( 'IGNORED' )
# return
2022-01-16 16:23:48 +01:00
Model = odoo.env[model]
ids = Model.search( domains )
# rfields = rel_fields( model )
# eprint( rfields )
for inst in Model.browse( ids ):
2022-01-17 23:44:30 +01:00
tree = lookup_model( Muid(model, inst.id), recurse=recurse )
2022-01-16 16:23:48 +01:00
filename = json_path( model, inst.id )
if filename in [item['file'] for item in stack]:
continue
tree['file'] = filename
# stack.append({ 'model': model, 'id': inst.id, 'file': filename, 'data': size })
stack.append( tree )
2022-01-16 16:28:44 +01:00
2022-01-17 13:47:36 +01:00
jlog( stack )
2022-01-17 16:11:01 +01:00
return stack
# return flatten_stack( stack )
2022-01-17 13:47:36 +01:00
def flatten_stack( data ):
flat = []
2022-01-17 13:55:08 +01:00
2022-01-17 15:20:25 +01:00
def flatten( obj ):
if type(obj) == dict:
2022-01-17 15:53:38 +01:00
# eprint( 'is a dict')
2022-01-17 15:27:45 +01:00
key = obj.keys()[0] if obj.keys()[0] != 'file' else obj.keys()[1]
2022-01-17 15:25:34 +01:00
flat.append( key )
2022-01-17 15:20:25 +01:00
for field in obj[key].keys():
2022-01-17 15:53:38 +01:00
# eprint( 'go flat key', field )
2022-01-17 15:20:25 +01:00
flatten( obj[key][field] )
if type(obj) == list:
2022-01-17 15:53:38 +01:00
# eprint( 'is a list')
2022-01-17 15:20:25 +01:00
for item in obj:
2022-01-17 16:01:51 +01:00
# eprint( 'item: ', item, type(item))
2022-01-17 16:11:01 +01:00
# if type(item) in (str,unicode):
2022-01-17 15:53:38 +01:00
# eprint( 'sub item is a str')
2022-01-17 16:11:01 +01:00
# flat.append( item )
2022-01-17 15:20:25 +01:00
if type(item) == dict:
2022-01-17 15:53:38 +01:00
# eprint( 'sub item is a dict')
2022-01-17 15:20:25 +01:00
flatten( item )
2022-01-17 15:32:14 +01:00
if type(obj) == str:
2022-01-17 15:53:38 +01:00
# eprint( 'is a str')
2022-01-17 15:32:14 +01:00
flat.append( item )
2022-01-17 13:55:08 +01:00
2022-01-17 15:20:25 +01:00
for obj in data:
flatten( obj )
return flat
2022-01-17 13:47:36 +01:00
2022-01-14 19:49:59 +01:00
2022-01-17 13:38:31 +01:00
def lookup_old( model, domains ):
2022-01-16 13:44:50 +01:00
eprint( "> Lookup: %s %s" % (model,domains) )
2022-01-14 19:49:59 +01:00
if model in MODEL_IGNORE:
print( 'IGNORED' )
return
Model = odoo.env[model]
ids = Model.search( domains )
rfields = rel_fields( model )
eprint( rfields )
for inst in Model.browse( ids ):
data = inst.read()[0]
size = len( json.dumps(data, indent=4) )
filename = json_path( model, inst.id )
2022-01-14 19:58:10 +01:00
if filename in [item['file'] for item in stack]:
2022-01-14 19:49:59 +01:00
continue
# print( 'Preparing data for %s' % (filename) )
stack.append({ 'model': model, 'id': inst.id, 'file': filename, 'data': size })
for field in rfields:
#data[field.name] = inst[field.name].read()[0]
#eprint( data[field.name] )
if field.relation in MODEL_IGNORE:
continue
if data[field.name]:
tsv( 'Field: ', field.ttype, field.name, field.relation, data[field.name] )
if field.ttype == 'many2one':
id = data[field.name][0]
_filename = json_path( field.relation, id )
print( field.ttype, field.relation, id, _filename )
if not os.path.exists( _filename ):
lookup( field.relation, [('id','=',id)] )
else:
print('Already exists %s' % (_filename) )
if field.ttype == 'one2many':
for id in data[field.name]:
_filename = json_path( field.relation, id )
print( field.ttype, field.relation, id, _filename )
if not os.path.exists( _filename ):
lookup( field.relation, [('id','=',id)] )
else:
print('Already exists %s' % (_filename) )
print( stack )
2022-01-14 14:27:23 +01:00
def export_json( model, domains ):
2022-01-14 19:49:59 +01:00
print( "> Export to json: %s %s" % (model,domains) )
2022-01-14 14:27:23 +01:00
if model in MODEL_IGNORE:
print( 'IGNORED' )
return
Model = odoo.env[model]
ids = Model.search( domains )
ensure_dir( "%s/%s" % (DATADIR, model) )
2022-01-14 14:27:23 +01:00
rfields = rel_fields( model )
eprint( rfields )
for inst in Model.browse( ids ):
data = inst.read()[0]
filename = "%s/%s/%s.json" % ( DATADIR, model, inst.id )
2022-01-14 14:27:23 +01:00
if filename in current_exports:
continue
print( 'Preparing data for %s' % (filename) )
current_exports.append( filename )
# Write pretty print JSON data to file
with open( filename, "w") as write_file:
json.dump(data, write_file, indent=4)
print( "%s written" % (filename) )
for field in rfields:
#data[field.name] = inst[field.name].read()[0]
#eprint( data[field.name] )
if field.relation in MODEL_IGNORE:
continue
if data[field.name]:
tsv( 'Field: ', field.ttype, field.name, field.relation, data[field.name] )
if field.ttype == 'many2one':
id = data[field.name][0]
_filename = "%s/%s/%s.json" % ( DATADIR, field.relation, id )
2022-01-14 14:27:23 +01:00
print( field.ttype, field.relation, id, _filename )
if not os.path.exists( _filename ):
export_json( field.relation, [('id','=',id)] )
else:
print('Already exists %s' % (_filename) )
if field.ttype == 'one2many':
for id in data[field.name]:
_filename = "%s/%s/%s.json" % ( DATADIR, field.relation, id )
2022-01-14 14:27:23 +01:00
print( field.ttype, field.relation, id, _filename )
if not os.path.exists( _filename ):
export_json( field.relation, [('id','=',id)] )
else:
print('Already exists %s' % (_filename) )
2022-01-18 00:44:05 +01:00
def save( model, domains, recurse = False, force = False ):
# force = OPTS['force'] if 'force' in OPTS else False
2022-01-18 00:37:13 +01:00
models = flatten_stack( lookup( model, domains, recurse=recurse ) )
2022-01-17 18:36:09 +01:00
unique_models = uniq( models )
2022-01-17 16:11:01 +01:00
jlog( unique_models )
2022-01-17 18:36:09 +01:00
to_browse = groupby_model( unique_models )
2022-01-17 16:11:01 +01:00
2022-02-06 02:26:16 +01:00
for mod in to_browse.keys():
2022-02-06 02:44:39 +01:00
print('loading %s %s' % (len(to_browse[mod]), mod) )
2022-02-06 02:26:16 +01:00
if mod in odoo.env:
try:
2022-02-06 02:54:01 +01:00
list = odoo.env[mod].browse( to_browse[mod] )
except:
eprint('Model %s not found' % (mod) )
continue
if list:
for inst in list:
2022-02-06 02:44:39 +01:00
filename = json_path( mod, inst.id )
print( inst.id, inst.name, ' > ', filename )
# Write pretty print JSON data to file
with open( filename, "w") as write_file:
2022-02-06 02:54:01 +01:00
json.dump(inst.read()[0], write_file, indent=4)
2022-02-06 02:44:39 +01:00
print( "%s written" % (filename) )
2022-02-06 02:54:01 +01:00
2022-01-17 16:11:01 +01:00
return to_browse
2022-01-14 14:27:23 +01:00
# AUTO EXEC
2022-01-17 19:04:33 +01:00
if __name__ == "__main__":
import sys
# PARAMS = sys.argv[1:]
PARAMS = []
OPTS = {}
for param in sys.argv[1:]:
# if param[0] == '-':
# kv = param[1:].split( '=' )
# if len(kv) == 1:
# OPTS[kv[0]] = False
# else:
# OPTS[kv[0]] = kv[1]
# elif
if param[0] == '+':
kv = param[1:].split( '=' )
if len(kv) == 1:
OPTS[kv[0]] = True
else:
OPTS[kv[0]] = kv[1]
else:
PARAMS.append( param )
eprint( len(sys.argv), 'argument(s):', PARAMS, OPTS )
eprint( MODEL_IGNORE )
2022-01-14 14:27:23 +01:00
2022-01-17 19:04:33 +01:00
MODEL = PARAMS[0]
if len(PARAMS) > 1:
METHOD = PARAMS[1]
2022-01-14 14:27:23 +01:00
2022-01-17 19:04:33 +01:00
if PARAMS[0] == 'login':
login( *PARAMS[1:] )
quit()
else:
2022-01-18 00:51:58 +01:00
autolog()
2022-01-14 14:27:23 +01:00
2022-01-17 19:04:33 +01:00
if MODEL in odoo.env:
Model = odoo.env[MODEL]
2022-01-17 15:53:38 +01:00
2022-01-17 19:04:33 +01:00
if METHOD == 'search':
# render_tsv( odoo.env[MODEL].browse( odoo.env[MODEL].search( args2domains(PARAMS[2:]) ) ) )
ids = search( MODEL, args2domains(PARAMS[2:]) )
render( OPTS, MODEL, Model.browse(ids) )
#ids = Model.search( args2domains(PARAMS[2:]) )
#print( 'id', 'name', sep='\t' )
#for inst in Model.browse( ids ):
# render_tsv( MODEL, inst )
2022-01-14 19:49:59 +01:00
2022-01-17 19:04:33 +01:00
if METHOD == 'browse':
ids = PARAMS[2:]
ids = [ int(s) for s in ids ]
#print( ids )
for inst in Model.browse( ids ):
jlog( inst.read()[0] )
2022-01-16 13:44:50 +01:00
2022-01-17 19:04:33 +01:00
if METHOD == 'fields_get':
jlog( Model.fields_get() )
2022-01-16 16:23:48 +01:00
2022-01-17 19:04:33 +01:00
if METHOD == 'fields':
res = fields( MODEL, args2domains(PARAMS[2:]) )
render( OPTS, 'ir.model.fields', res )
# Model = odoo.env['ir.model.fields']
# ids = Model.search([( 'model_id.model', '=', MODEL )])
# render( OPTS, 'ir.model.fields', Model.browse(ids) )
#for inst in Field.browse( ids ):
# render_tsv( 'ir.model.fields', inst )
2022-01-14 14:27:23 +01:00
2022-01-18 00:37:13 +01:00
if 'lookup' in METHOD or 'save' in METHOD:
2022-01-17 23:44:30 +01:00
recurse = False
if ( 'recurse' in OPTS and OPTS['recurse'] ):
recurse = OPTS['recurse']
2022-01-17 19:04:33 +01:00
if METHOD == 'lookup':
2022-01-17 23:44:30 +01:00
jlog( lookup( MODEL, args2domains(PARAMS[2:]), recurse=recurse ) )
2022-01-14 14:27:23 +01:00
2022-01-17 19:04:33 +01:00
if METHOD == 'lookup-model':
2022-01-17 23:44:30 +01:00
jlog( lookup_model( Muid(MODEL,PARAMS[2]), recurse=recurse ) )
2022-01-14 14:27:23 +01:00
2022-01-17 19:04:33 +01:00
# if METHOD == 'lookup-recs':
# jlog( lookup_recs( MODEL, args2domains(PARAMS[2:]) ) )
if METHOD == 'export-json':
export_json( MODEL, args2domains(PARAMS[2:]) )
if METHOD == 'save':
2022-02-06 02:04:04 +01:00
jlog( save( MODEL, args2domains(PARAMS[2:]), recurse=recurse ) )
2022-01-17 19:04:33 +01:00
elif MODEL == 'db':
if METHOD == 'list':
jlog( odoo.db.list() )
elif MODEL == 'schema':
get_schema( METHOD )
2022-01-14 14:27:23 +01:00