(07)odoo扩展API

* 打开XML-RPC 连接
    >>> import xmlrpclib
    >>> srv, db = 'http://localhost:8069', 'v8dev'
    >>> user, pwd = 'admin', 'admin'
    >>> common = xmlrpclib.ServerProxy('%s/xmlrpc/2/common' % srv)
    >>> common.version()
    {'server_version_info': [8, 0, 0, 'final', 0], 'server_serie':
    '8.0', 'server_version': '8.0', 'protocol_version': 1}
   
    依赖 xmlrpclib 
   
    #验证
        >>> uid = common.authenticate(db, user, pwd, {})
        >>> print uid
   
    #取数据
        >>> api = xmlrpclib.ServerProxy('%s/xmlrpc/2/object' % srv)
       
       
        >>> api.execute_kw(db, uid, pwd, 'res.partner', 'search_count', [[]])
            得到记录集的总数
       
        >>> api.execute_kw(db, uid, pwd, 'res.partner', 'search',
            [[('country_id', '=', 'be'), ('parent_id', '!=', False)]])
           
            [43, 42]
            得到记录集的id序列
           
        >>> api.execute_kw(db, uid, pwd, 'res.partner', 'read', [[43]],
            {'fields': ['id', 'name', 'parent_id']})
           
            [{'parent_id': [7, 'Agrolait'], 'id': 43, 'name': 'Michel
            Fletcher'}]   
            得到记录
           
        >>> api.execute_kw(db, uid, pwd, 'res.partner', 'search_read',
            [[('country_id', '=', 'be'), ('parent_id', '!=', False)]],
            {'fields': ['id', 'name', 'parent_id']})
       
    #其它方法
    >>> api.execute_kw(db, uid, pwd, 'res.partner', 'create', [{'name':
        'Packt'}])
        75
    >>> api.execute_kw(db, uid, pwd, 'res.partner', 'write', [[75],
        {'name': 'Packt Pub'}])
        True
    >>> api.execute_kw(db, uid, pwd, 'res.partner', 'read', [[75], ['id',
        'name']])
        [{'id': 75, 'name': 'Packt Pub'}]
    >>> api.execute_kw(db, uid, pwd, 'res.partner', 'unlink', [[75]])
        True
       
* 做一个桌面应用 用到接口
   note_api.py
   ----------------
    import xmlrpclib


    class NoteAPI():

        def __init__(self, srv, db, user, pwd):
            common = xmlrpclib.ServerProxy( '%s/xmlrpc/2/common' % srv)
            self.api = xmlrpclib.ServerProxy('%s/xmlrpc/2/object' % srv)
            self.uid = common.authenticate(db, user, pwd, {})
            self.pwd = pwd
            self.db = db
            self.model = 'todo.task'
       
        def execute(self, method, arg_list, kwarg_dict=None):
            return self.api.execute_kw(
            self.db, self.uid, self.pwd, self.model,
            method, arg_list, kwarg_dict or {})   
       
        def get(self, ids=None):
            domain = [('id',' in', ids)] if ids else []
            fields = ['id', 'name']
            return self.execute('search_read', [domain, fields])
       
        def set(self, text, id=None):
            if id:
              self.execute('write', [[id], {'name': text}])
            else:
              vals = {'name': text, 'user_id': self.uid}
              id = self.execute('create', [vals])
            return id
       
        if __name__ == '__main__':
            srv, db = 'http://localhost:8069', 'v8dev'
            user, pwd = 'admin', 'admin'
            api = NoteAPI(srv, db, user, pwd)
            from pprint import pprint
            pprint(api.get())
   ----------------
   note_gui.py 
   ----------------
   from Tkinter import Text, Tk
import tkMessageBox
from note_api import NoteAPI

class NoteText(Text):
    def __init__(self, api, text='', id=None):
        self.master = Tk()
        self.id = id
        self.api = api
        Text.__init__(self, self.master, bg='#f9f3a9',
        wrap='word', undo=True)
        self.bind('<Control-n>', self.create)
        self.bind('<Control-s>', self.save)
        if id:
            self.master.title('#%d' % id)
        self.delete('1.0', 'end')
        self.insert('1.0', text)
        self.master.geometry('220x235')
        self.pack(fill='both', expand=1)
       
    def create(self, event=None):
        NoteText(self.api, '')
       
    def save(self, event=None):
        text = self.get('1.0', 'end')
        self.id = self.api.set(text, self.id)
        tkMessageBox.showinfo('Info', 'Note %d Saved.' % self.id)   
       
       
    if __name__ == '__main__':
        srv, db = 'http://localhost:8069', 'v8dev'
        user, pwd = 'admin', 'admin'
        api = NoteAPI(srv, db, user, pwd)
        for note in api.get():
            x = NoteText(api, note['name'], note['id'])
        x.master.mainloop()   
   ----------------
  
* ERPpeek
   首先要安装 erppeek
   $ pip install -U erppeek
  
   #API 用法:
    >>> import erppeek
    >>> api = erppeek.Client('http://localhost:8069', 'v8dev',
        'admin', 'admin')
    >>> api.common.version()
    >>> api.count('res.partner', [])
    >>> api.search('res.partner', [('country_id', '=', 'be'),
        ('parent_id', '!=', False)])
    >>> api.read('res.partner', [43], ['id', 'name', 'parent_id'])
   
   >>> m = api.model('res.partner')
   >>> m = api.ResPartner
  
   >>> m.count([('name', 'like', 'Packt%')]) 返回是数量
   >>> m.search([('name', 'like', 'Packt%')]) 返回是id
   >>> recs = m.browse([('name', 'like', 'Packt%')]) 返回是记录对象
  
   # CLI用法
     $ erppeek --help  可以看到详细的信息
    
    
* 到这里插件开发就讲完了    

你可能感兴趣的:((07)odoo扩展API)