Monday, July 22, 2013

OpenERP XMLRPC Essentials

OVERVIEW



Technology Used:
- OpenERP
- Django Framework 1.5
- xmlrpclib

PROBLEM



Task:
- Create an attendance user interface.

Problem:
- Difficulty in getting desired layout of the kiosk using.

SOLUTION



What:
- Create a Django instance for the kiosk and use XMLRPC as a Protocol for data synchronization between the Django instance and the OpenERP instance.

Why:
- To easily implement the layout desired for the kiosk, since Django's templating engine is far easier to manipulate.

How:
- Using the builtin python module to fetch the data, process it within the django kiosk.
- Also, since the OpenERP xmlrpc module also have a submit method flow, the django kiosk can still use the python module xmlrpclib to send data to the OpenERP server.
xmlrpclib

To start off, here are the necessary variables to establish an XMLRPC connection:
server_url = "http://your.open-instance.com"
dbname = "YOUR_OpenERP_DATABASE"
username = "admin" #Must be admin user
password = "Y3AH!"

Next, here is a basic example of fetching data from an OpenERP server instance:
sock = xmlrpclib.ServerProxy(server_url + 'xmlrpc/common')
uid = sock.login(dbname, username, password)
sock = xmlrpclib.ServerProxy(server_url + 'xmlrpc/object')

sock.execute(dbname,uid,password,model,'search',criteria)

The xmlrpc ServerProxy execute() method returns a list of dictionaries (yes it's automatically converted from xml format). Now you can easily iterate over the result object and handle it the way you want it.

Now, here's how you send data.
model = 'some_model'
data = { 'key': value, 'another_key': another_value }
id = sock._sock.execute(dbname,uid,password,model,'create',data )
return id #openerp model instance id


And that's it! That's essentially how OpenERP's xmlrpc transactions are done.

For a deper look into the methods and example usage in other programming languages, checkout their documentation at https://doc.openerp.com/6.0/developer/6_22_XML-RPC_web_services/.
And for advanced usage of the python xmlrpclib, handling Fault objects and multi calls, checkout their documentation at http://docs.python.org/2/library/xmlrpclib.html

No comments:

Post a Comment