|
HowToUseTrytondAsAModule
Gives an example of how to interact with tryton server code with a simple import
Phase-Implementation IntroductionUsing Tryton code as a library can be useful for various stuff:
Of course this list is not extensive. ExampleThis example does the following things:
Notes:
Version 2.2#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import os,sys
DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
'/', 'usr', 'local', 'tryton', 'trytond', 'trytond')))
if os.path.isdir(DIR):
sys.path.insert(0, os.path.dirname(DIR))
from trytond.config import CONFIG
# Load the configuration file
CONFIG.update_etc('/usr/local/tryton/trytond/etc/trytond.conf')# Replace with your path
from trytond.pool import Pool
from trytond.model import Cache
from trytond.transaction import Transaction
# dbname contains the db you want to use
dbname = 'dbTryton'
CONTEXT={}
Pool.start()
pool = Pool(dbname)
# Clean the global cache for multi-instance
Cache.clean(dbname)
# Instantiate the pool
with Transaction().start(dbname, 0, context=CONTEXT):
pool.init()
# User 0 is root user. We use it to get the admin id:
with Transaction().start(dbname, 0, context=CONTEXT):
user_obj = pool.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
with Transaction().start(dbname, user, context=CONTEXT) as transaction:
# No password is needed, because we are working directly with the
# API, bypassing any networking stuff.
# don't forget to install the party's module before this test.
party_obj = pool.get('party.party')
new_party_id = party_obj.create({'name': 'New party'})
transaction.cursor.commit()
print new_party_id
# Reset the global cache for multi-instance
Cache.resets(dbname)Version 1.8#! /usr/bin/env python
from trytond.config import CONFIG
from trytond.modules import register_classes
from trytond.pool import Pool
from trytond.backend import Database
from trytond.model import Cache
from trytond.transaction import Transaction
# Load the configuration file
CONFIG.configfile = '/etc/trytond.conf' # Replace with your path
CONFIG.load()
# Register classes populates the pool of models:
register_classes()
# dbname contains the db you want to use
DBNAME = 'test'
# Instantiate the database and the pool
DB = Database(DBNAME).connect()
POOL = Pool(DBNAME)
POOL.init()
# Clean the global cache for multi-instance
Cache.clean(DBNAME)
# User 0 is root user. We use it to get the admin id:
with Transaction().start(DBNAME, 0, None):
user_obj = POOL.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
with Transaction().start(DBNAME, user, None) as transation:
# No password is needed, because we are working directly with the
# API, bypassing any networking stuff.
party_obj = POOL.get('party.party')
new_party_id = party_obj.create({'name': 'New party'})
transaction.cursor.commit()
print new_party_id
# Reset the global cache for multi-instance
Cache.resets(DBNAME)Version 1.6#! /usr/bin/env python
import sys, os
import logging
logging.basicConfig(level=logging.FATAL)
from trytond.config import CONFIG
CONFIG.load()
from trytond.modules import register_classes
from trytond.pool import Pool
from trytond.backend import Database
from trytond.tools import Cache
# Register classes populates the pool of models:
register_classes()
# dbname contains the db you want to use
DBNAME = 'test'
# Instantiate the database and the pool
DB = Database(DBNAME).connect()
POOL = Pool(DBNAME)
POOL.init()
# Get a cursor on the database
cursor = DB.cursor()
# Clean the global cache for multi-instance
Cache.clean(DBNAME)
# User 0 is root user. We use it to get the admin id:
user_obj = POOL.get('res.user')
user = user_obj.search(cursor, 0, [
('login', '=', 'admin'),
], limit=1)[0]
# No password is needed, because we are working directly with the
# API, bypassing any networking stuff.
party_obj = POOL.get('party.party')
new_party_id = party_obj.create(cursor, user, {
'name': 'New party'
})
print new_party_id
cursor.commit()
cursor.close()
# Reset the global cache for multi-instance
Cache.resets(DBNAME)
| |
► Sign in to add a comment
With the current version of Tryton, the following hack has to be made:
Here's a patch to trytond/config.py that will load the standard configuration file properly:
http://codereview.appspot.com/1692048/diff/1/2
This patch is only applied on trunk. So update should be done when next version will be released.
Can this example be updated for the changes made after the Contextualisation of cursor, user and context?