11
.. i18n: How to load data ?
12
.. i18n: ==================
18
.. i18n: #. Postgresql
19
.. i18n: * Simple, standard
20
.. i18n: * Does not respect the WORKFLOW !!!
21
.. i18n: #. XML files (with –update=)
23
.. i18n: * Script, same as website interface
28
* Does not respect the WORKFLOW !!!
29
#. XML files (with –update=)
31
* Script, same as website interface
33
.. i18n: How to backup/restore a Postgresql database?
34
.. i18n: ============================================
37
How to backup/restore a Postgresql database?
38
============================================
45
.. i18n: pg_dump terp >terp.sql
48
pg_dump terp >terp.sql
55
.. i18n: createdb terp --encoding=unicode
56
.. i18n: psql terp < terp.sql
58
.. i18n: psql -d terp -f terp.sql
61
createdb terp --encoding=unicode
64
psql -d terp -f terp.sql
66
.. i18n: The objects methods
67
.. i18n: ===================
73
.. i18n: #. create({'field':'value'})
74
.. i18n: * return ID created
75
.. i18n: #. search([('arg1','=','value1')...], offset=0, limit=1000)
76
.. i18n: * return [IDS] found
77
.. i18n: #. read([IDS], ['field1','field2',...])
78
.. i18n: * return [{'id':1, 'field1':..., 'field2':..., ...}, ...]
79
.. i18n: #. write([IDS], {'field1':'value1','field2':3})
80
.. i18n: * return True
81
.. i18n: #. unlink([IDS])
82
.. i18n: * return True
85
#. create({'field':'value'})
87
#. search([('arg1','=','value1')...], offset=0, limit=1000)
89
#. read([IDS], ['field1','field2',...])
90
* return [{'id':1, 'field1':..., 'field2':..., ...}, ...]
91
#. write([IDS], {'field1':'value1','field2':3})
96
.. i18n: =================
97
.. i18n: WebservicesXMLRPC
98
.. i18n: =================
106
.. i18n: * standard: http://www.xmlrpc.org
107
.. i18n: * RPC Over HTTP
108
.. i18n: * Function Parameters & Result encoded in XML
109
.. i18n: #. Principle;
110
.. i18n: * calls to objects methodes;
111
.. i18n: - read, write
113
.. i18n: - unlink (=delete)
117
* standard: http://www.xmlrpc.org
119
* Function Parameters & Result encoded in XML
121
* calls to objects methodes;
126
.. i18n: XML-RPC is known as a web service. Web services are a set of tools that let one build distributed applications on top of existing web infrastructures. These applications use the Web as a kind of "transport layer" but don't offer a direct human interface via the browser.[1] Extensible Markup Language (XML) provides a vocabulary for describing Remote Procedure Calls (RPC), which is then transmitted between computers using the HyperText Transfer Protocol (HTTP). Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network. These interfaces can be as simple as a single function call or as complex as a large API.
129
XML-RPC is known as a web service. Web services are a set of tools that let one build distributed applications on top of existing web infrastructures. These applications use the Web as a kind of "transport layer" but don't offer a direct human interface via the browser.[1] Extensible Markup Language (XML) provides a vocabulary for describing Remote Procedure Calls (RPC), which is then transmitted between computers using the HyperText Transfer Protocol (HTTP). Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network. These interfaces can be as simple as a single function call or as complex as a large API.
131
.. i18n: XML-RPC therefore allows two or more computers running different operating systems and programs written in different languages to share processing. For example, a Java application could talk with a Perl program, which in turn talks with Python application that talks with ASP, and so on. System integrators often build custom connections between different systems, creating their own formats and protocols to make communications possible, but one can often end up with a large number of poorly documented single-use protocols. The RPC approach spares programmers the trouble of having to learn about underlying protocols, networking, and various implementation details.
134
XML-RPC therefore allows two or more computers running different operating systems and programs written in different languages to share processing. For example, a Java application could talk with a Perl program, which in turn talks with Python application that talks with ASP, and so on. System integrators often build custom connections between different systems, creating their own formats and protocols to make communications possible, but one can often end up with a large number of poorly documented single-use protocols. The RPC approach spares programmers the trouble of having to learn about underlying protocols, networking, and various implementation details.
136
.. i18n: XML-RPC can be used with Python, Java, Perl, PHP, C, C++, Ruby, Microsoft’s .NET and many other programming languages. Implementations are widely available for platforms such as Unix, Linux, Windows and the Macintosh.
139
XML-RPC can be used with Python, Java, Perl, PHP, C, C++, Ruby, Microsoft’s .NET and many other programming languages. Implementations are widely available for platforms such as Unix, Linux, Windows and the Macintosh.
141
.. i18n: An XML-RPC call is conducted between two parties: the client (the calling process) and the server (the called process). A server is made available at a particular URL (such as http://example.org:8080/rpcserv/).
144
An XML-RPC call is conducted between two parties: the client (the calling process) and the server (the called process). A server is made available at a particular URL (such as http://example.org:8080/rpcserv/).
146
.. i18n: The above text just touches the surface of XML-RPC. I recommend O'Reilly's "Programming Web Service with XML-RPC" for further reading. One may also wish to review the following links:
149
The above text just touches the surface of XML-RPC. I recommend O'Reilly's "Programming Web Service with XML-RPC" for further reading. One may also wish to review the following links:
151
.. i18n: XML-RPC Home Page\\\\ XML-RPC for C and C++\\\\ The Apache XML-RPC Project\\\\ Expat: The XML Parser\\\\
154
XML-RPC Home Page\\\\ XML-RPC for C and C++\\\\ The Apache XML-RPC Project\\\\ Expat: The XML Parser\\\\
156
.. i18n: =================
157
.. i18n: An example in PHP
158
.. i18n: =================
165
.. i18n: Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
168
Here is an example on how to insert a new partner using PHP. This example makes use the phpxmlrpc library, available on sourceforge.
170
.. i18n: .. code-block:: php
174
.. i18n: include('xmlrpc.inc');
176
.. i18n: $arrayVal = array(
177
.. i18n: 'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
178
.. i18n: 'vat'=>new xmlrpcval('BE477472701' , "string")
181
.. i18n: $client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
183
.. i18n: $msg = new xmlrpcmsg('execute');
184
.. i18n: $msg->addParam(new xmlrpcval("dbname", "string"));
185
.. i18n: $msg->addParam(new xmlrpcval("3", "int"));
186
.. i18n: $msg->addParam(new xmlrpcval("demo", "string"));
187
.. i18n: $msg->addParam(new xmlrpcval("res.partner", "string"));
188
.. i18n: $msg->addParam(new xmlrpcval("create", "string"));
189
.. i18n: $msg->addParam(new xmlrpcval($arrayVal, "struct"));
191
.. i18n: $resp = $client->send($msg);
193
.. i18n: if ($resp->faultCode())
195
.. i18n: echo 'Error: '.$resp->faultString();
199
.. i18n: echo 'Partner '.$resp->value()->scalarval().' created !';
208
include('xmlrpc.inc');
211
'name'=>new xmlrpcval('Fabien Pinckaers', "string") ,
212
'vat'=>new xmlrpcval('BE477472701' , "string")
215
$client = new xmlrpc_client("http://localhost:8069/xmlrpc/object");
217
$msg = new xmlrpcmsg('execute');
218
$msg->addParam(new xmlrpcval("dbname", "string"));
219
$msg->addParam(new xmlrpcval("3", "int"));
220
$msg->addParam(new xmlrpcval("demo", "string"));
221
$msg->addParam(new xmlrpcval("res.partner", "string"));
222
$msg->addParam(new xmlrpcval("create", "string"));
223
$msg->addParam(new xmlrpcval($arrayVal, "struct"));
225
$resp = $client->send($msg);
227
if ($resp->faultCode())
229
echo 'Error: '.$resp->faultString();
233
echo 'Partner '.$resp->value()->scalarval().' created !';
237
.. i18n: ====================
238
.. i18n: An example in Python
239
.. i18n: ====================
246
.. i18n: Example of creation of a partner and his address.
249
Example of creation of a partner and his address.
251
.. i18n: .. code-block:: python
253
.. i18n: import xmlrpclib
255
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
257
.. i18n: pwd = 'demo'
260
.. i18n: 'title': 'Monsieur',
261
.. i18n: 'name': 'Fabien Pinckaers',
262
.. i18n: 'lang': 'fr',
263
.. i18n: 'active': True,
266
.. i18n: partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
269
.. i18n: 'partner_id': partner_id,
270
.. i18n: 'type': 'default',
271
.. i18n: 'street': 'Rue du vieux chateau, 21',
272
.. i18n: 'zip': '1457',
273
.. i18n: 'city': 'Walhain',
274
.. i18n: 'phone': '(+32)10.68.94.39',
275
.. i18n: 'fax': '(+32)10.68.94.39',
278
.. i18n: sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
281
.. code-block:: python
285
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
291
'name': 'Fabien Pinckaers',
296
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
299
'partner_id': partner_id,
301
'street': 'Rue du vieux chateau, 21',
304
'phone': '(+32)10.68.94.39',
305
'fax': '(+32)10.68.94.39',
308
sock.execute(dbname, uid, pwd, 'res.partner.address', 'create', address)
310
.. i18n: To get the UID of a user, you can use the following script:
313
To get the UID of a user, you can use the following script:
315
.. i18n: .. code-block:: python
317
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
318
.. i18n: UID = sock.login('terp3', 'admin', 'admin')
321
.. code-block:: python
323
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
324
UID = sock.login('terp3', 'admin', 'admin')
326
.. i18n: CRUD example:
331
.. i18n: .. code-block:: python
334
.. i18n: :The login function is under
335
.. i18n: :: http://localhost:8069/xmlrpc/common
336
.. i18n: :For object retrieval use:
337
.. i18n: :: http://localhost:8069/xmlrpc/object
339
.. i18n: import xmlrpclib
341
.. i18n: user = 'admin'
342
.. i18n: pwd = 'admin'
343
.. i18n: dbname = 'terp3'
344
.. i18n: model = 'res.partner'
346
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
347
.. i18n: uid = sock.login(dbname ,user ,pwd)
349
.. i18n: sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
351
.. i18n: # CREATE A PARTNER
352
.. i18n: partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
353
.. i18n: partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
355
.. i18n: # The relation between res.partner and res.partner.category is of type many2many
356
.. i18n: # To add categories to a partner use the following format:
357
.. i18n: partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
358
.. i18n: # Where [3, 2, 1] are id fields of lines in res.partner.category
360
.. i18n: # SEARCH PARTNERS
361
.. i18n: args = [('vat', '=', 'ZZZZZ'),]
362
.. i18n: ids = sock.execute(dbname, uid, pwd, model, 'search', args)
364
.. i18n: # READ PARTNER DATA
365
.. i18n: fields = ['name', 'active', 'vat', 'ref']
366
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
367
.. i18n: print results
369
.. i18n: # EDIT PARTNER DATA
370
.. i18n: values = {'vat':'ZZ1ZZ'}
371
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
373
.. i18n: # DELETE PARTNER DATA
374
.. i18n: results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
377
.. code-block:: python
380
:The login function is under
381
:: http://localhost:8069/xmlrpc/common
382
:For object retrieval use:
383
:: http://localhost:8069/xmlrpc/object
390
model = 'res.partner'
392
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
393
uid = sock.login(dbname ,user ,pwd)
395
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
398
partner_data = {'name':'Tiny', 'active':True, 'vat':'ZZZZZ'}
399
partner_id = sock.execute(dbname, uid, pwd, model, 'create', partner_data)
401
# The relation between res.partner and res.partner.category is of type many2many
402
# To add categories to a partner use the following format:
403
partner_data = {'name':'Provider2', 'category_id': [(6,0,[3, 2, 1])]}
404
# Where [3, 2, 1] are id fields of lines in res.partner.category
407
args = [('vat', '=', 'ZZZZZ'),]
408
ids = sock.execute(dbname, uid, pwd, model, 'search', args)
411
fields = ['name', 'active', 'vat', 'ref']
412
results = sock.execute(dbname, uid, pwd, model, 'read', ids, fields)
416
values = {'vat':'ZZ1ZZ'}
417
results = sock.execute(dbname, uid, pwd, model, 'write', ids, values)
419
# DELETE PARTNER DATA
420
results = sock.execute(dbname, uid, pwd, model, 'unlink', ids)
422
.. i18n: :PRINT example:
427
.. i18n: 1. PRINT INVOICE
428
.. i18n: 2. IDS is the invoice ID, as returned by:
429
.. i18n: 3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
433
2. IDS is the invoice ID, as returned by:
434
3. ids = sock.execute(dbname, uid, pwd, 'account.invoice', 'search', [('number', 'ilike', invoicenumber), ('type', '=', 'out_invoice')])
436
.. i18n: .. code-block:: python
439
.. i18n: import base64
440
.. i18n: printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
441
.. i18n: model = 'account.invoice'
442
.. i18n: id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
443
.. i18n: time.sleep(5)
444
.. i18n: state = False
446
.. i18n: while not state:
447
.. i18n: report = printsock.report_get(dbname, uid, pwd, id_report)
448
.. i18n: state = report['state']
449
.. i18n: if not state:
450
.. i18n: time.sleep(1)
451
.. i18n: attempt += 1
452
.. i18n: if attempt>200:
453
.. i18n: print 'Printing aborted, too long delay !'
455
.. i18n: string_pdf = base64.decodestring(report['result'])
456
.. i18n: file_pdf = open('/tmp/file.pdf','w')
457
.. i18n: file_pdf.write(string_pdf)
458
.. i18n: file_pdf.close()
461
.. code-block:: python
465
printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
466
model = 'account.invoice'
467
id_report = printsock.report(dbname, uid, pwd, model, ids, {'model': model, 'id': ids[0], 'report_type':'pdf'})
472
report = printsock.report_get(dbname, uid, pwd, id_report)
473
state = report['state']
478
print 'Printing aborted, too long delay !'
480
string_pdf = base64.decodestring(report['result'])
481
file_pdf = open('/tmp/file.pdf','w')
482
file_pdf.write(string_pdf)