Skip to content

Commit 5de8717

Browse files
author
Çağatay ÜRESİN
committed
[3] @cagatayuresin New Release
1 parent 5409344 commit 5de8717

File tree

8 files changed

+498
-0
lines changed

8 files changed

+498
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Odoo XMLRPC Wrapper
3+
4+
A simple Python to make CRUD process easier
5+
"""
6+
7+
__version__ = "1.0.1"
8+
__author__ = 'Cagatay URESIN'
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
import xmlrpc.client
2+
3+
4+
class Bot:
5+
"""
6+
A bot class to rule them all.
7+
"""
8+
9+
def __init__(
10+
self,
11+
host: str = None,
12+
db: str = None,
13+
userlogin: str = None,
14+
password: str = None,
15+
secured: bool = True,
16+
test: bool = False,
17+
) -> None:
18+
"""
19+
Bot instance initializer: If test = True other arguments muted. For http:// secured = False
20+
21+
Args:
22+
host (str, optional): The host 'odoo.yourhost.com'. Defaults to None.
23+
db (str, optional): Database to login. Defaults to None.
24+
userlogin (str, optional): Login string. Defaults to None.
25+
password (str, optional): Password. Defaults to None.
26+
secured (bool, optional): http:// or https://. Defaults to True.
27+
test (bool, optional): To use Odoo's own test servers. Defaults to False.
28+
"""
29+
self.__test_info = (
30+
xmlrpc.client.ServerProxy("https://demo.odoo.com/start").start()
31+
if test
32+
else "Not Test"
33+
)
34+
self.HOST = self.__test_info["host"][8:] if test else host
35+
self.URL = (
36+
f"https://{self.HOST}/xmlrpc/2"
37+
if secured
38+
else f"http://{self.HOST}/xmlrpc/2"
39+
)
40+
self.DB = self.__test_info["database"] if test else db
41+
self.USERLOGIN = self.__test_info["user"] if test else userlogin
42+
self.__PASSWORD = self.__test_info["password"] if test else password
43+
self.model = None
44+
self.__common = xmlrpc.client.ServerProxy(f"{self.URL}/common")
45+
self.version = self.__common.version()
46+
self.uid = self.__common.authenticate(
47+
self.DB, self.USERLOGIN, self.__PASSWORD, {}
48+
)
49+
if not self.uid:
50+
raise Exception(
51+
f"Wrong one ({self.HOST}, {self.DB}, {self.USERLOGIN}, PASSWORD)"
52+
)
53+
self.__orm = xmlrpc.client.ServerProxy(f"{self.URL}/object")
54+
self.profile = self.read("res.users", ids=self.uid, fields=["name"])[0]
55+
self.name = self.profile["name"]
56+
self.successful = True
57+
if self.successful:
58+
print(self.status)
59+
60+
def satus(self) -> str:
61+
return f"Successfully Logged\nName: {self.name}\nDB: {self.DB}\nHOST: {self.HOST}\nVERSION: {self.version['server_version']}"
62+
63+
def search_read(
64+
self,
65+
model: str = None,
66+
constraints: list = None,
67+
fields: list = None,
68+
limit: int = None,
69+
) -> list:
70+
"""
71+
Searches with constraints amd reads the results fields.
72+
73+
Args:
74+
model (str, optional): Model name. Defaults to None.
75+
constraints (list, optional): Search constraints. Defaults to None.
76+
fields (list, optional): Desired fields to read. Defaults to ["name"].
77+
limit (int, optional): Result limit. Defaults to None.
78+
79+
Returns:
80+
list: A list of results as dicts with desired fields.
81+
"""
82+
if model:
83+
self.model = model
84+
if fields is None:
85+
fields = ["name"]
86+
if constraints is None:
87+
constraints = [[]]
88+
else:
89+
constraints = [constraints]
90+
return self.__orm.execute_kw(
91+
self.DB,
92+
self.uid,
93+
self.__PASSWORD,
94+
self.model,
95+
"search_read",
96+
constraints,
97+
{"fields": fields} if limit is None else {"fields": fields, "limit": limit},
98+
)
99+
100+
def search(
101+
self,
102+
model: str = None,
103+
constraints: list = None,
104+
offset: int = None,
105+
limit: int = None,
106+
) -> list:
107+
"""
108+
Searches with constraints and returns results ids.
109+
110+
Args:
111+
model (str, optional): Model name. Defaults to None.
112+
constraints (list, optional): Search constraints. Defaults to None.
113+
offset (int, optional): Offset. Defaults to None.
114+
limit (int, optional): Result limit. Defaults to None.
115+
116+
Returns:
117+
list: A list of record ids as integers.
118+
"""
119+
if model:
120+
self.model = model
121+
if constraints is None:
122+
constraints = [[]]
123+
else:
124+
constraints = [constraints]
125+
return self.__orm.execute_kw(
126+
self.DB,
127+
self.uid,
128+
self.__PASSWORD,
129+
self.model,
130+
"search",
131+
constraints,
132+
{"offset": offset, "limit": limit}
133+
if offset and limit
134+
else {"offset": offset}
135+
if offset
136+
else {"limit": limit}
137+
if limit
138+
else {},
139+
)
140+
141+
def count(self, model: str = None, constraints: list = None) -> int:
142+
"""
143+
Length of records with constraints.
144+
145+
Args:
146+
model (str, optional): Model name. Defaults to None.
147+
constraints (list, optional): Search constraints. Defaults to None.
148+
149+
Returns:
150+
int: Count of records.
151+
"""
152+
return len(self.search(model, constraints))
153+
154+
def read(self, model: str = None, ids: list = None, fields: list = None) -> list:
155+
"""
156+
Reads ids with desired fields.
157+
158+
Args:
159+
model (str, optional): Model name. Defaults to None.
160+
ids (list, optional): List of ids to read. Defaults to None.
161+
fields (list, optional): Desired fields to read. Defaults to None.
162+
163+
Returns:
164+
list: A list of results as dicts with desired fields.
165+
"""
166+
if model:
167+
self.model = model
168+
return self.__orm.execute_kw(
169+
self.DB,
170+
self.uid,
171+
self.__PASSWORD,
172+
self.model,
173+
"read",
174+
[ids],
175+
{"fields": fields} if fields else {},
176+
)
177+
178+
def delete(self, model: str = None, ids: list = None) -> None:
179+
"""
180+
ID list to delete.
181+
182+
Args:
183+
model (str, optional): Model name. Defaults to None.
184+
ids (list, optional): List of ids to delete. Defaults to None.
185+
"""
186+
if model:
187+
self.model = model
188+
self.__orm.execute_kw(
189+
self.DB,
190+
self.uid,
191+
self.__PASSWORD,
192+
self.model,
193+
"unlink",
194+
[ids] if isinstance(ids, list) else [[ids]],
195+
)
196+
197+
def create(self, model: str = None, the_obj: dict = None) -> None:
198+
"""
199+
Creates new record.
200+
201+
Args:
202+
model (str, optional): Model name. Defaults to None.
203+
the_obj (dict, optional): The object as dict to create. Defaults to None.
204+
205+
Raises:
206+
ValueError: No Object
207+
"""
208+
if the_obj is None:
209+
raise ValueError("No Object")
210+
if model:
211+
self.model = model
212+
self.__orm.execute_kw(
213+
self.DB, self.uid, self.__PASSWORD, self.model, "create", [the_obj]
214+
)
215+
216+
def update(
217+
self, model: str = None, the_id: int = None, the_obj: dict = None
218+
) -> None:
219+
"""
220+
Updates a record.
221+
222+
Args:
223+
model (str, optional): Model name. Defaults to None.
224+
the_id (int, optional): The id as integer of the record. Defaults to None.
225+
the_obj (dict, optional): The object as dict to update. Defaults to None.
226+
227+
Raises:
228+
ValueError: No ID
229+
ValueError: No Object
230+
"""
231+
if id is None:
232+
raise ValueError("No ID")
233+
if the_obj is None:
234+
raise ValueError("No Object")
235+
if model:
236+
self.model = model
237+
self.__orm.execute_kw(
238+
self.DB, self.uid, self.__PASSWORD, self.model, "write", [[the_id], the_obj]
239+
)
240+
241+
def get_fields(self, model: str = None, attributes: list = None) -> dict:
242+
"""
243+
Model fields with desired infos.
244+
245+
Args:
246+
model (str, optional): Model name. Defaults to None.
247+
attributes (list, optional): Desired attributes of the fields. Defaults to None.
248+
249+
Returns:
250+
dict: Fields of the model.
251+
"""
252+
if model:
253+
self.model = model
254+
return self.__orm.execute_kw(
255+
self.DB,
256+
self.uid,
257+
self.__PASSWORD,
258+
self.model,
259+
"fields_get",
260+
[],
261+
{"attributes": attributes} if attributes else {},
262+
)
263+
264+
def custom(self, model: str = None, command: str = None, att=[[]]):
265+
"""
266+
Triggering a method remotely.
267+
268+
Args:
269+
model (str): Model name.
270+
command (str): Your custom command's name.
271+
att (list, optional): Sends attributes your custom method.
272+
273+
Returns:
274+
Your method's return.
275+
"""
276+
if model:
277+
self.model = model
278+
return self.__orm.execute_kw(
279+
self.DB, self.uid, self.__PASSWORD, self.model, command, att
280+
)
6.84 KB
Binary file not shown.
6.68 KB
Binary file not shown.

0 commit comments

Comments
 (0)