Skip to content

Commit c86772c

Browse files
openerp-client-lib
1 parent 782c003 commit c86772c

File tree

7 files changed

+722
-0
lines changed

7 files changed

+722
-0
lines changed

openerp-client-lib-master/LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (C) 2011 Nicolas Vanhoren
4+
Copyright (C) Stephane Wirtel
5+
Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
OpenERP Client Library
3+
======================
4+
5+
6+
The OpenERP Client Library is a Python library to communicate with an OpenERP Server using its web
7+
services in an user-friendly way. It was created for those that doesn't want to code XML-RPC calls
8+
on the bare metal. It handles XML-RPC as well as JSON-RPC protocol and provides a bunch of syntaxic
9+
sugar to make things a lot easier.
10+
11+
Guide
12+
-----
13+
14+
First install the library: ::
15+
16+
sudo easy_install openerp-client-lib
17+
18+
Now copy-paste the following script describing a simple interaction with an OpenERP server: ::
19+
20+
import openerplib
21+
22+
connection = openerplib.get_connection(hostname="localhost", database="my_db", \
23+
login="my_user", password="xxx")
24+
user_model = connection.get_model("res.users")
25+
ids = user_model.search([("login", "=", "admin")])
26+
user_info = user_model.read(ids[0], ["name"])
27+
print user_info["name"]
28+
# will print "Administrator"
29+
30+
In the previous script, the get_connection() method creates a Connection object that represents a
31+
communication channel with authentification to an OpenERP server. By default, get_connection() uses
32+
XML-RPC, but you can specify it to use JSON-RPC. You can also change the port. Example with a JSON-RPC
33+
communication on port 6080: ::
34+
35+
connection = openerplib.get_connection(hostname="localhost", protocol="jsonrpc", port=6080, ...)
36+
37+
The get_model() method on the Connection object creates a Model object. That object represents a
38+
remote model on the OpenERP server (for OpenERP addon programmers, those are also called osv).
39+
Model objects are dynamic proxies, which means you can remotely call methods in a natural way.
40+
In the previous script we demonstrate the usage of the search() and read() methods. That scenario
41+
is equivalent to the following interaction when you are coding an OpenERP addon and this code is
42+
executed on the server: ::
43+
44+
user_osv = self.pool.get('res.users')
45+
ids = user_osv.search(cr, uid, [("login", "=", "admin")])
46+
user_info = user_osv.read(cr, uid, ids[0], ["name"])
47+
48+
Also note that coding using Model objects offer some syntaxic sugar compared to vanilla addon coding:
49+
50+
- You don't have to forward the "cr" and "uid" to all methods.
51+
- The read() method automatically sort rows according the order of the ids you gave it to.
52+
- The Model objects also provides the search_read() method that combines a search and a read, example: ::
53+
54+
user_info = user_model.search_read([('login', '=', 'admin')], ["name"])[0]
55+
56+
Here are also some considerations about coding using the OpenERP Client Library:
57+
58+
- Since you are using remote procedure calls, every call is executed inside its own transaction. So it can
59+
be dangerous, for example, to code multiple interdependant row creations. You should consider coding a method
60+
inside an OpenERP addon for such cases. That way it will be executed on the OpenERP server and so it will be
61+
transactional.
62+
- The browse() method can not be used. That method returns a dynamic proxy that lazy loads the rows' data from
63+
the database. That behavior is not implemented in the OpenERP Client Library.
64+
65+
Compatibility
66+
-------------
67+
68+
- XML-RPC: OpenERP version 6.1 and superior
69+
70+
- JSON-RPC: OpenERP version 8.0 (upcoming) and superior
71+
72+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Copyright (C) Stephane Wirtel
5+
# Copyright (C) 2011 Nicolas Vanhoren
6+
# Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions are met:
11+
#
12+
# 1. Redistributions of source code must retain the above copyright notice, this
13+
# list of conditions and the following disclaimer.
14+
# 2. Redistributions in binary form must reproduce the above copyright notice,
15+
# this list of conditions and the following disclaimer in the documentation
16+
# and/or other materials provided with the distribution.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
#
29+
##############################################################################
30+
31+
from .main import *
32+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Copyright (C) Stephane Wirtel
5+
# Copyright (C) 2011 Nicolas Vanhoren
6+
# Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions are met:
11+
#
12+
# 1. Redistributions of source code must retain the above copyright notice, this
13+
# list of conditions and the following disclaimer.
14+
# 2. Redistributions in binary form must reproduce the above copyright notice,
15+
# this list of conditions and the following disclaimer in the documentation
16+
# and/or other materials provided with the distribution.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
#
29+
##############################################################################
30+
31+
import datetime
32+
33+
DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
34+
DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S"
35+
DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % (
36+
DEFAULT_SERVER_DATE_FORMAT,
37+
DEFAULT_SERVER_TIME_FORMAT)
38+
39+
def str_to_datetime(str):
40+
"""
41+
Converts a string to a datetime object using OpenERP's
42+
datetime string format (exemple: '2011-12-01 15:12:35').
43+
44+
No timezone information is added, the datetime is a naive instance, but
45+
according to OpenERP 6.1 specification the timezone is always UTC.
46+
"""
47+
if not str:
48+
return str
49+
return datetime.datetime.strptime(str.split(".")[0], DEFAULT_SERVER_DATETIME_FORMAT)
50+
51+
def str_to_date(str):
52+
"""
53+
Converts a string to a date object using OpenERP's
54+
date string format (exemple: '2011-12-01').
55+
"""
56+
if not str:
57+
return str
58+
return datetime.datetime.strptime(str, DEFAULT_SERVER_DATE_FORMAT).date()
59+
60+
def str_to_time(str):
61+
"""
62+
Converts a string to a time object using OpenERP's
63+
time string format (exemple: '15:12:35').
64+
"""
65+
if not str:
66+
return str
67+
return datetime.datetime.strptime(str.split(".")[0], DEFAULT_SERVER_TIME_FORMAT).time()
68+
69+
def datetime_to_str(obj):
70+
"""
71+
Converts a datetime object to a string using OpenERP's
72+
datetime string format (exemple: '2011-12-01 15:12:35').
73+
74+
The datetime instance should not have an attached timezone and be in UTC.
75+
"""
76+
if not obj:
77+
return False
78+
return obj.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
79+
80+
def date_to_str(obj):
81+
"""
82+
Converts a date object to a string using OpenERP's
83+
date string format (exemple: '2011-12-01').
84+
"""
85+
if not obj:
86+
return False
87+
return obj.strftime(DEFAULT_SERVER_DATE_FORMAT)
88+
89+
def time_to_str(obj):
90+
"""
91+
Converts a time object to a string using OpenERP's
92+
time string format (exemple: '15:12:35').
93+
"""
94+
if not obj:
95+
return False
96+
return obj.strftime(DEFAULT_SERVER_TIME_FORMAT)
97+

0 commit comments

Comments
 (0)