Simple and powerfull tools for programming in Odoo framework (f.e shell, reference to models in stand-alone applications, Mediator pattern for intermodel communications)
This package implements tools which help you to access database using Odoo models’ mechanizm. Use Odoo Gateway and you will be able to
- Run models’ methods in shell. Usefull when you need to fix some database data or test you code.
- Create stand-alone scripts which can refer to Odoo models and call their methods.
To use Odoo Gateway properly you need only Odoo library of 7 or 8 versions.
You can use pip:
$ pip install odoo_gateway
Shell can be launched with:
$ odoosh [params]
Parameters are the same as openerp server’s ones. You can list then any time, just type:
$ odoosh --help
Ok, lets do it right now!:
$ odoosh -c config_file.conf -d database1 In :
Nice! Odoo shell is launched now. Following objects are already in namespace:
cr - Odoo wrapper for psycopg’s cursor:
In : cr <openerp.sql_db.Cursor at 0x3450d50> In : cr.execute("select id, name from sale_order limit 1") In : cr.fetchall() [(600094, u'SALE-123456')]``
conn - Psycopg’s connection to database:
In : conn <connection object at 0x3485f60; dsn: 'host=localhost port=5432 user=odoo password=xxxx dbname=database1', closed: 0> In : conn.rollback()
rr - Psycopg’s cursor
uid - Id of Odoo superuser
conf - Instance of openerp.tools.config.configmanager that contains parsed Odoo parameters:
In : conf <openerp.tools.config.configmanager at 0x210bbd0> In : conf['db_name'] database1 In : conf['db_host'] localhost
models - Adapter for Odoo models’ list.:
In : models.sale_order # get reference for model sale.order sale.order() In : type(models.sale_order) openerp.api.sale.order In : """Now we are free to apply API and user defined methods of model.""" In : sale1 = models.sale_order.search([('name','=', 'sale-111111')]) In : sale1.name sale-111111 In : sale2 = models['sale.order'].search([('name','=', 'sale-111112')]) In : sale2.name sale-111112
There are two ways to refer to Odoo model.
- You can use model’s name name.of.model and get reference with models[name.of.model]. See line 15 of example above.
- Or you can replace ‘.’ with ‘_’ and type models.name_of_model. The result will be the same as in previous point. See lines 10-14.
- session - Instance of odoo_gateway.session.Session
There are problems which require using openerp API outside the openerp server, in another independent process. For example imagine that you have a Shop. Done? Ok. There is a python application APP. APP is a messenger and uses the system of queues. Customers send messages in which they describe what products they want to buy. APP gets messages from queues, parses them and create sale orders in database.
I belive that APP does not need for openerp server to be launched. No, sir!
All you need is instance of odoo_gateway.session.Session, which is initiated with odoo server’ command parameters.:
from odoo_gateway import Session session = Session(['-c', 'config.conf', '-d', 'testdb'])
As you get session object you can get anything you may dream about.:
models = session.models cr = session.cr conn = session.conn uid = session.uid rr = session.rr conf = session.conf # The power is yours now def create_sale(**params): models.sale_order.create(**params) cr.commit()