odoo二次开发

#odoo Odoo 的前身是 OpenERP,是一个开源的企业 ERP 系统。odoo官网
通过官网可以看到odoo可以干很多事情,例如销售、CRM等等。
#odoo文档 我们可以在odoo文档看到用户手册、开发者文档、API、安装说明、白皮书、法律文件。
由于我们要对odoo进行二次开发,则我们需要访问API文档。 #Web Service API 在odoo文档-API部分,我们将会访问的是web Service API,通过该文档,我们可以了解到,odoo的API通信方式,是通过xmlrpc方式进行远程调用。

#测试代码

# encoding=utf8
import xmlrpclib

# Connection
url = "https://demo.odoo.com"
db = ""
username = "admin"
password = "admin"

# 通过demo服务器的start函数获取,实际测试的服务器的url,db,username,password
info = xmlrpclib.ServerProxy('https://demo.odoo.com/start').start()
url, db, username, password = info['host'], info['database'], info['user'], info['password']
print "[url]:" + url
print "[db]:" + db
print "[username]:" + username
print "[password]:" + password

# Logging
common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
print "Method version:" + str(common.version())

uid = common.authenticate(db, username, password, {})
# print "[uid]:" + str(uid)
# # Calling methods
models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
result = models.execute_kw(db, uid, password,
                  'res.partner', 'check_access_rights',
                  ['read'], {'raise_exception': False})

print result

# Create records
id = models.execute_kw(db, uid, password, 'res.partner', 'create', [{
    'name': "中文测试",
}])
print "[id]:" + str(id)
[record] = models.execute_kw(db, uid, password,
    'res.partner', 'read', [id])
print record

#形成类 20160816添加测试类 ##测试类

from unittest import TestCase

import datetime

from OdooServerApi import OdooServerApi


class TestOdooServerApi(TestCase):
    def test_get_uid(self):
        print "test_get_uid"
        server = OdooServerApi()
        print "[uid]:" + str(server.get_uid())
        pass


    def test_get_mac_address(self):
        server = OdooServerApi()
        print "[mac_address]:" + str(server.get_mac_address())
        pass

    def test_update(self):
        start_time = datetime.datetime.now() - datetime.timedelta(hours=8)
        print start_time
        end_time = start_time + datetime.timedelta(hours=10)
        print end_time

        server = OdooServerApi()
        server.update(start_time,end_time)
        pass

##OdooServerApi类

import uuid
import xmlrpclib

class OdooServerApi:
    url = "http://192.168.1.120:8069"
    db = "Workload Account"
    username = "testimporter@test.test"
    password = "123456"
    uid = None
    mac_address = None

    def __init__(self, url=None,db=None,username=None,password=None):
        if url is not None:
            self.url = url
        if db is not None:
            self.db = db
        if username is not None:
            self.username = username
        if password is not None:
            self.password = password

    def get_uid(self):
        common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(self.url))
        print "Method version:" + str(common.version())
        uid = common.authenticate(self.db, self.username, self.password, {})
        print "[uid]:" + str(uid)
        self.uid = uid
        return self.uid

    def get_mac_address(self):
        mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
        self.mac_address = ":".join([mac[e:e+2] for e in range(0,11,2)])
        return self.mac_address

    def update(self, start_time, end_time, mac_address=None,uid=None):
        if uid is None:
            self.get_uid()
        if mac_address is None:
            self.get_mac_address()

        models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(self.url))
        models.execute_kw(self.db, self.uid, self.password, 'workload_account.raw_data', 'create', [{
            'mac_address': self.mac_address,
            'start_time': str(start_time),
            'end_time': str(end_time)
        }])

转载于:https://my.oschina.net/hava/blog/731865

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值