Utilities to make better use of OdooRPC


Keywords
odoorpc, odoorpc_utils, Odoo Python, Odoo RPC
License
CC0-1.0
Install
pip install odoorpc-utils==3.0.1

Documentation

Overview

This module offers functions to:

  1. Instantiate common Odoo RPC libraries from a single .ini file (currently odooly and odoorpc supported);

  2. Run DEEP read and search_read queries, retrieving data from multiple models at once, in an efficient manner and using parallel execution of RPCs;

  3. The function to support deep queries can also be used as a building block to support GraphQL or GraphQL-like querying.

TO-DO:

  • Extend the utils package to support GraphQL queries;
  • Write an odoorpc_fast class to speed-up RPC queries by using HTTP sessions and gzip encoding;
  • Many, many potential functionalities... feel free to help writing and suggesting!

Example 1:

from odoorpc_utils import FastDeepQueries, OdooInstantiator

# read from file odoorpc_utils.ini, odoorpc-utils.ini or odooly.ini
# If you prefer you can skip this and instantiate odoorpc the old way.
odoo = OdooInstantiator.odoorpc_from_config_file('production')

# pass an instance of logged-in odoorpc here (odooly not supported at the moment)
fdq = FastDeepQueries(odoo)

# Let's get the id of 100 sales orders...
ids = fdq.query_fast(model_name='sale.order', limit=100)

# The fields you want to retrieve are specified in this easy-to-understand syntax.
# 'fields' can also be a string
fields = ['number, date_confirmed, order_total',
          'partner_id[name, address, zip, city, state, tax_number]',
          'sale_order_line_ids[quantity, unit_price, total_price]',
          'sale_order_line_ids.product_id[name, reference]']

# Now let's make a deep query for 100 sales order and see how fast it is 
# Nope, it will not be blazing fast, it will actually be quite slow, but MUCH faster 
# than what you usually get from odoorpc...
res = fdq.query_deep(mode_name='sale.order', fields=fields, ids=ids, n_threads=8)

# Done!

# RPC is slow... each query takes ~1 second, larger queries can take much longer. Running them in
# parallel speeds things up significantly (but it is still RPC)

print(res)

Example 2:

from odoorpc_utils import FastDeepQueries, OdooInstantiator

odoo = OdooInstantiator.odoorpc_from_config_file('production')    
fdq = FastDeepQueries(odoo)

# We can also run queries defined on a queries.ini file
res = fdq.named_query('picking list report', ids=[order_id])

Sample odoorpc_utils.ini file:

[DEFAULT]
port = 443
username = user
password = pass

# Odooly-specific
scheme = https
protocol = jsonrpc

#OdooRPC-specific
timeout = 3600
# if **protocol_odoorpc** is missing, the param protocol will be
# used with odoorpc instead. But Odooly and OdooRPC do not support the same
# syntax for the protocol string, hence why an odoorpc-specific option exists
protocol_odoorpc = jsonrpc+ssl

[production]
host = www.my.tld
database = my_production_database

[testing]
host = testing.my.tld
database = testing_database

Sample queries.ini file

[DEFAULT]
# acceptable values are all parameters of the query_deep function
# with the exception of fields_deep that MUST be provided by means of the
# fields *model name* and *fields*.
n_threads = 8

# format_for__id_fields
# 1 for [id, 'Name'] - Standard OdooRPC.
# 2 for [id] - useful to have both _id and _ids fields on list of ids, but all
#              _id fields have one id only, so the list is not necessary
#              and also the create/write calls do not accept a list with one id.
# 3 for the id as an int - default. Best if you plan to later use the create/write calls
format_for__id_fields = 3

[picking list report]
model name = sale.order
fields = number,date_confirmed,order_total
fields = partner_id[name,address,zip,city,state,tax_number]
fields = sale_order_line_ids[quantity, unit_price, total_price]
fields = sale_order_line_ids.product_id.[name,reference]