Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
3073 Widoki

Hello all, I thought I would post this challenge I have had.


I am used to other ERPs where the API is RESTful and I found it a real challenge to get data from the online SAAS version of Odoo. Starting with an almost complete lack of documentation and no Swagger page, and ending with the archaic RPC API. But in the end, it is all possible, and seeing as there is very little posted here about how to do stuff like this, I thought I'd share it and hope others will weigh in with things they've worked on too.

First I figured out just how to get a contact with the server:


I used Python just because it was close to hand and I was using it for API calls to Epicor ERP recently.


import requests
import xmlrpc.client
import xml\.etree\.ElementTree\ as\ ET
import\ json

url\ =\ "http://\[myInstance\]\.odoo\.com"
db\ =\ "\[myDatabase\]"
usr\ =\ "myUser"
pwd\ =\ "myPassword"
nl\ =\ "\n"
res\ =\ ""

try:
\ \ \ \
\ \ \ \ common\ =\ xmlrpc\.client\.ServerProxy\('\{\}/xmlrpc/2/common'\.format\(url\)\)
\ \ \ \ uid\ =\ common\.authenticate\(db,\ usr,\ pwd,\ \{\}\)\ \ \
\ \ \ \ data\ =\ json\.dumps\(common\.version\(\)\)
\ \ \ \ for\ k,v\ in\ common\.version\(\)\.items\(\):
\ \ \ \ \ \ \ \ res\ \+=\ str\(k\)\ \+\ ":\ "\ \+\ str\(v\)\ \+\ nl
\ \ \ \ \ \ \ \
except\ Exception\ as\ e:
\ \ \ \ res\ \+=\ "except:\ "\ \+\ str\(e\)\ \+\ nl

finally:
\ \ \ \
\ \ \ \ print\(res\)

This\ got\ me\ the\ basic\ server\ info\ once\ I\ had\ it\ working:



So\ at\ least\ I\ knew\ I\ was\ contacting\ my\ database!


Then\ I\ figured\ out\ code\ to\ get\ me\ partner\ info,\ based\ on\ the\ documentation\.\ Frankly,\ the\ documentation\ leaves\ a\ lot\ out\.\ Maybe\ all\ you\ smart\ people\ already\ know\ how\ to\ parse\ out\ JSON\ and\ so\ on,\ but\ it\ was\ quite\ a\ challenge\ for\ me\.





import\ requests
import\ xmlrpc\.client
import\ json

url\ =\ "http://\[instance\]\.odoo\.com"
db\ =\ "\[dbName\]"
usr\ =\ "userName"
pwd\ =\ "password"
nl\ =\ "\n"
api\ =\ "/xmlrpc/2/"
rslt\ =\ ""
info\ =\ ""

try:

\ \ \ \ \#\ get\ the\ valid\ uid\ of\ an\ authenticated\ API\ user
\ \ \ \ common\ =\ xmlrpc\.client\.ServerProxy\('\{\}/xmlrpc/2/common'\.format\(url\)\)
\ \ \ \ uid\ =\ common\.authenticate\(db,\ usr,\ pwd,\ \{\}\)\ \ \

\ \ \ \ \#\ set\ up\ the\ ServerProxy
\ \ \ \ models\ =\ xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

# check access to the object ("partner" in this case)
access_check = models.execute_kw \
(db, uid, pwd, 'res.partner', 'check_access_rights',\
['read'], {'raise_exception': False})

if access_check == True:
# get the ids of partner objects that only are companies
ids = models.execute_kw(db, uid, pwd, 'res.partner', \
'search', [[['is_company','=', True]]])

# get the fields shown that match the ids
rslt = json.dumps(models.execute_kw (db, uid, pwd, 'res.partner', \
'read', [ids], {'fields': ['name', 'phone']}),indent=1)

data = json.loads(rslt)

for i in data:
info += i["name"] + nl

else:
raise Exception("Model not accessible")

except Exception as e:
rslt += "except: " + str(e) + nl

finally:

print(info)


This allowed me to get a list of partner names where the partner is a company. Odoo's unique model basically makes everybody you know a "Partner", so you then filter to figuer out who is a contact, a company, a vendor, a customer, etc.


Also notice that you first use filters to get the ids related to your search, then you actually read the database based on the ids you return. really weird for someone who grew up with linq, SQL, and REST!


Anyways, this gave me a list of partners that look something like this:



That's all in Python 3.11 and scripted in IDLE by someone who did Python in computer science 10 years ago and hasn't used it since. If I can do it so can you. COmment below, let's get this community interested in posting ideas and cool projects like the big ERPs!

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
3
cze 22
5735
2
gru 22
3202
0
gru 22
1419
5
wrz 19
7959
0
kwi 18
2483