-
Notifications
You must be signed in to change notification settings - Fork 0
/
odooconnector.py
147 lines (131 loc) · 5.2 KB
/
odooconnector.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#######################################
#
# Simple odoo connector
#
# www.studio73.es
#
#######################################
import xmlrpclib
import threading
URL = 'http://localhost'
PORT = 8069
USER = 'admin'
PSWD = 'admin'
class odooconnector:
def __init__(self, db, url=URL, port=PORT, user=USER, pswd=PSWD):
self.threads = []
self.url_port = '%s:%s' %(url,port)
self.db = db
self.url = url
self.port = port
self.user = user
self.password = pswd
self.common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(self.url_port))
self.models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(self.url_port))
self.uid = self.common.authenticate(self.db, self.user, self.password, {})
#
# Basic functions
#
def call(self, model=None, ids=[], function=None, args={}):
"""
:param model: model to read i.e., 'res.partner'
:type model: string
:param ids: list of integers (Ids of models)
:type ids: list
:param args: Dictionary with function to call arguments
:type args: dict
"""
if model and isinstance(model, str):
res = self.models.execute_kw(self.db, self.uid, self.password, model, function, [ids], args)
return res
def search(self, model=None, domain=[], args={}):
"""
:param model: model to search i.e., 'res.partner'
:type model: string
:param domain: list of conditions
:type domain: list
:param args: Dictionary with search arguments
:type args: dict
"""
if model and isinstance(model, str):
return self.models.execute_kw(self.db, self.uid, self.password, model, 'search', [domain], args)
def read(self, model=None, ids=[], fields=[], args={}):
"""
:param model: model to read i.e., 'res.partner'
:type model: string
:param ids: list of integers (Ids of models)
:type ids: list
:param fields: list of fields to read
:type fields: list
:param args: Dictionary with read arguments
:type args: dict
"""
if model and isinstance(model, str):
args.update({'fields': fields})
return self.models.execute_kw(self.db, self.uid, self.password, model, 'read', [ids], args)
def search_read(self, model=None, domain=[], fields=[], args={}):
"""
:param model: model to search and read i.e., 'res.partner'
:type model: string
:param domain: list of conditions
:type domain: list
:param fields: list of fields to read
:type fields: list
:param args: Dictionary with search_read arguments
:type args: dict
"""
if model and isinstance(model, str):
args.update({'fields': fields})
return self.models.execute_kw(self.db, self.uid, self.password, model, 'search_read', [domain], args)
def write(self, model=None, ids=[], values={}):
"""
:param model: model to write i.e., 'res.partner'
:type model: string
:param ids: list of integers (Ids of models)
:type ids: list
:param values: values dict of new record
:type values: dict
"""
if model and isinstance(model, str):
return self.models.execute_kw(self.db, self.uid, self.password, model, 'write', [ids, values])
def create(self, model=None, values={}):
"""
:param model: model to create i.e., 'res.partner'
:type model: string
:param values: values dict of new record
:type values: dict
"""
if model and isinstance(model, str):
return self.models.execute_kw(self.db, self.uid, self.password, model, 'create', [values])
def unlink(self, model=None, ids=[]):
"""
:param model: model to unlink i.e., 'res.partner'
:type model: string
:param ids: list of integers (Ids of models)
:type ids: list
"""
if model and isinstance(model, str):
return self.models.execute_kw(self.db, self.uid, self.password, model, 'unlink', [ids])
#
# TODO: Test and improve!
# Threading functions
#
def multi_create(self, model=None, values=[], threads=2):
if model and isinstance(model, str):
for chunck_list in self._chunks(values,int(len(values)/threads)+1):
th = threading.Thread(target=self._create_worker,args=(model, chunck_list,))
self.threads.append(th)
th.start()
def threads_join(self):
for t in self.threads:
t.join()
def _create_worker(self, model, values):
for val in values:
self.create(model,val)
def _chunks(self, l, n):
#Thanks to http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
n = max(1, n)
return [l[i:i + n] for i in range(0, len(l), n)]