Skip to content

Instantly share code, notes, and snippets.

@zodman
Created October 16, 2011 06:17
Show Gist options
  • Save zodman/1290571 to your computer and use it in GitHub Desktop.
Save zodman/1290571 to your computer and use it in GitHub Desktop.
tryton admin jsonrpc
from jsonrpclib import Server as ServerProxy
import base64
import jsonrpclib
import json
class AdminTrytonException(Exception):
def __init__(self, result):
self.result = result
def __str__(self):
return self.result
class AdminTryton(object):
def __init__(self, url,tryton_admin_password):
self._tryton_admin_password = tryton_admin_password
self._server = ServerProxy(url, verbose=0)
def list_database(self):
return self.__execute(method="list")
def create_db(self, new_database_name, password_admin, lang):
return self.__execute("create", new_database_name, \
self._tryton_admin_password, lang, password_admin)
def db_exist(self, db_name):
return self.__execute(method="db_exist")
def list_lang(self):
return self.__execute(method="list_lang")
def dump(self, database_name, name="out.dump"):
data = self.__execute("dump", database_name, \
self._tryton_admin_password)
if data:
with open(name,"wb") as f:
f.write(base64.decodestring(data))
return True
else:
return False
def dump_to_string(self, database_name):
return self.__execute("dump", database_name, \
self._tryton_admin_password)
def __execute(self, method, *args):
server_exec = getattr(self._server.common.db, method)
try:
server_exec(None, None, *args)
except TypeError:
raise AdminTrytonException(self.__result())
return self.__result()
def __result(self):
a = json.loads( jsonrpclib.history.response)
return a.get("result", a.get("error"))
if __name__ == "__main__":
try:
a = AdminTryton("http://localhost:8000", "admin")
#print a.list_database()
#print a.list_lang()
#print a.db_exist("aa1eeeeeeeee")
#print a.dump_to_string("aa1")
a.dump("aa1")
#print a.create_db("test123","admin","en_EN")
except AdminTrytonException as e:
print e
@pcboey
Copy link

pcboey commented Apr 26, 2012

Thanks a lot for the script. It's a good starting point for my attempt to write an Android client.
I did come across 3 problems when running it :

  1. The tryton_admin_password in the constructor for TrytonAdmin class should actually be the name of the admin user, for me that's "admin". So I changed the line in the main block to
    a = AdminTryton("http://admin:admin@localhost:8000", "admin")
  2. The db_exist method should read as
    return self.__execute("db_exist", db_name)
  3. I had to change the str method to
    return str(self.result)

But is there any documentation on all the available methods or do I have to dig through the source?

@zodman
Copy link
Author

zodman commented Apr 26, 2012

the admin methods it will showed on http://hg.tryton.org/trytond/file/4929bf1c53d9/trytond/protocols/dispatcher.py and http://hg.tryton.org/proteus for manipulating data.

I write this script because i want to manage a trytond admin operations without have tryton installed.

@tejastank
Copy link

Really thanks for sharing code. What is best option for jsonrpc for tryton.

Does anyone have tryton jsonrpc code with twisted lib. ?

@tejastank
Copy link

how to login via rpc and execute model's methods ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment