-
Notifications
You must be signed in to change notification settings - Fork 3
/
oerphelper.py
107 lines (79 loc) · 3.51 KB
/
oerphelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# OERP shortcut package
import oerplib
import locale
from ConfigParser import ConfigParser
import codecs
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
cfg = ConfigParser({})
cfg.readfp(codecs.open('config.ini', 'r', 'utf8'))
oerp = oerplib.OERP(server=cfg.get('openerp', 'server'), protocol='xmlrpc+ssl',
database=cfg.get('openerp', 'database'), port=cfg.getint('openerp', 'port'),
version='7.0')
user = oerp.login(user=cfg.get('openerp', 'user'), passwd=cfg.get('openerp', 'password'))
oerpContext={'lang': 'de_DE'}
class NotFound(Exception):
pass
def getIds(db, filter):
return oerp.search(db, filter, context=oerpContext)
def getId(db, filter):
ids=getIds(db, filter)
if not ids:
raise NotFound("cannot find {} from search {}".format(db, str(filter)))
assert len(ids)==1, "found more than one {} from search {}".format(db, str(filter))
return ids[0]
#def getIds(db, filter):
# return oerp.search(db, filter, context=oerpContext)
def read(db, id, fields=[]):
readResult=oerp.read(db, [id], fields, context=oerpContext)
if len(readResult)!=1:
raise NotFound()
return readResult[0]
def write(db, id, data):
return oerp.write(db, [id], data, context=oerpContext)
def create(db, data):
return oerp.create(db, data, context=oerpContext)
def readElements(db, filter, fields=[]):
return oerp.read(db, getIds(db, filter), fields, context=oerpContext)
def readProperty(db, id, field, firstListItem=False):
readResult=read(db, id, [field])
property=readResult[field]
if firstListItem:
return property[0]
else:
return property
def categoryIdFromName(name):
return getId('product.category', [('name', '=', name)])
def partnerIdFromName(name):
return getId('res.partner', [('name', '=', name)])
def productIdFromName(name):
return getId('product.product', [('name', '=', name), ('active', '=', True)])
def warehouseIdFromName(name):
return getId('stock.warehouse', [('name', '=', name)])
def customerIdFromName(name):
return getId('res.partner', [('name', '=', name), ('customer', '=', True)])
def productIdsFromSupplier(shopName, productCode):
shop_id = getId('res.partner', [('name', '=', shopName)])
try:
supplierinfo_id=getId('product.supplierinfo',[('name','=',shop_id),('product_code','=',productCode)])
except NotFound:
return {'product':None, 'supplierinfo':None, 'shop': shop_id}
product_id=readProperty('product.supplierinfo', supplierinfo_id, 'product_id', True)
return {'product':product_id, 'supplierinfo':supplierinfo_id}
def getDefault(db, fields):
return oerp.execute(db, 'default_get', fields) # TODO does not send the id of the currently edited record like the web-GUI does. still works.
# call onchange handler to fetch related default values
# usage example:
# data=callOnchangeHandler(...)
# data.update(callOnchangeHandler(db, field2, value2))
# data["foo"]="bar"
# data.update(....)
# oerp.create(db, data)
def callOnchangeHandler(db, field, value):
reply=oerp.execute(db, 'onchange_'+field, [], value) # TODO does not send the id of the currently edited record like the web-GUI does. still works.
assert (not reply.has_key('warning') or not reply["warning"]), "failed calling onchange-handler, reply:"+str(reply)
update=dict(reply["value"])
update[field]=value
return update
# functions helping with the "newer style" .browse() API
def searchAndBrowse(db, filter):
return oerp.browse(db, oerp.search(db, filter))