odoo V10开发文档(第四章:国际化、报表、webservice)

国际化

每个模块可以提供一个国际化翻译模块,存放在模块的i18n文件夹中的xx.po文件中,如中文就是zh_CN.po,所有翻译文件在odoo启动时会自动加载。一般默认用英语来开发模块,然后用odoo内置的导出翻译功能导出生成pot模板,然后就可以根据这个模板编辑成其他语言的了。

报表

报表打印

odoo从8.0开始使用qweb引警和bootstrap 、Wkhtmltopdf来打印报表
报表由两个元素组成

  • report标签的ir.actions.report.xml,它会初始化一些报表的基本参数如默认类型、生成后是否保存到数据库

  • 一个标准的Qweb视图用于实际生成报表

    
        
            

Report title

报表是标准的web页面,所以是可以通过链接直接访问的。如发票的html版本报表可以通过http://localhost:8069/report/html/account.report_invoice/1来访问,pdf版本通过http://localhost:8069/report/pdf/account.report_invoice/1查看
如果发现生成的pdf样式不对,那可能是wkhtmltopdf没办法访问web服务器导致的。wkhtmltopdf进程使用web.base.url作为根url来生成对应文件路径,但这个地址是每次管理员登录都会自动更新的,如果使用了代理,可能就出现访问不了的情况,可以通过添加一个系统参数来解决:report.url指向一个可访问的地址如localhost:8069等、这个是报表打印专用参数,或者用web.base.url.freeze,设置为True时会停止自动更新。

实例:为session模型制作报表

#__manifest__.py
'data': [
        'security/security.xml',
        'security/ir.model.access.csv',
        'views/views.xml',
        'views/partner.xml',
        'report.xml',
    ],
    
#report.xml


    

    


    

仪表板

实例:制作一个仪表板,包含已经创建的日历视图、图形视图、和课程列表,并将它加到菜单项中,当进入academy模块时自动显示仪表板内容。

#__manifest__.py
'depends': ['base', 'board'],

# always loaded
'data': [
    'security/security.xml',
    'security/ir.model.access.csv',
    'views/views.xml',
    'views/partner.xml',
    'views/session_board.xml',
    'reports.xml',
    'views/session_workflow.xml',
    #'views/templates.xml',
    #'data/data.xml'
],

#views/session_board.xml


    
        
            Attendees by course
            openacademy.session
            form
            graph
            
        
        
            Sessions
            openacademy.session
            form
            calendar
            
        
        
            Courses
            openacademy.courses
            form
            tree,form
        
        
            Session Dashboard Form
            board.board
            form
            
                
Session Dashboard board.board form form menu

WebServices

WebServices模块为所有web服务提供XML-RPC和JSON-RPC接口,所有业务对象都可通过webservice操作

  • XML-RPC库

例:使用xmlrpclib访问odoo服务的python脚本

import xmlrpclib

root = 'http://%s:%d/xmlrpc/' % (HOST, PORT)

uid = xmlrpclib.ServerProxy(root + 'common').login(DB, USER, PASS)
print "Logged in as %s (uid: %d)" % (USER, uid)

# Create a new note
sock = xmlrpclib.ServerProxy(root + 'object')
args = {
    'color' : 8,
    'memo' : 'This is a note',
    'create_uid': uid,
}
note_id = sock.execute(DB, uid, PASS, 'note.note', 'create', args)

例:通过xmlrpc发送请求到odoo服务器,显示全部session及座位数、还可以为课程创建新session

# -*- coding: utf-8 -*-

import functools
import xmlrpclib
HOST = 'localhost'
PORT = 8069
DB = 'odoo'
USER = 'admin'
PASS = '123456'
ROOT = 'http://%s:%d/xmlrpc/' % (HOST,PORT)

# 1. Login
uid = xmlrpclib.ServerProxy(ROOT + 'common').login(DB,USER,PASS)
print "Logged in as %s (uid:%d)" % (USER,uid)

call = functools.partial(
    xmlrpclib.ServerProxy(ROOT + 'object').execute,
    DB, uid, PASS)

# 2. Read the sessions
sessions = call('openacademy.session','search_read', [], ['name','seats'])
for session in sessions:
    print "Session %s (%s seats)" % (session['name'], session['seats'])
# 3.create a new session
session_id = call('openacademy.session', 'create', {
    'name' : 'My session',
    'course_id' : 2,
})
  • JSON-RPC库

例1:通过python的urllib2和json类库与odoo服务器进行交互的python脚本

import json
import random
import urllib2

HOST = 'localhost'
PORT = 8069
DB = 'odoo'
USER = 'admin'
PASS = '123456'

def json_rpc(url, method, params):
    data = {
        "jsonrpc": "2.0",
        "method": method,
        "params": params,
        "id": random.randint(0, 1000000000),
    }
    req = urllib2.Request(url=url, data=json.dumps(data), headers={
        "Content-Type":"application/json",
    })
    reply = json.load(urllib2.urlopen(req))
    if reply.get("error"):
        raise Exception(reply["error"])
    return reply["result"]

def call(url, service, method, *args):
    return json_rpc(url, "call", {"service": service, "method": method, "args": args})

# log in the given database
url = "http://%s:%s/jsonrpc" % (HOST, PORT)
uid = call(url, "common", "login", DB, USER, PASS)

print "login successed ,uid : %s" % uid

例2:使用jsonrpclib类库来实现一样的功能

import jsonrpclib

HOST = 'localhost'
PORT = 8069
DB = 'odoo'
USER = 'admin'
PASS = '123456'

# server proxy object
url = "http://%s:%s/jsonrpc" % (HOST, PORT)
server = jsonrpclib.Server(url)

# log in the given database
uid = server.call(service="common", method="login", args=[DB, USER, PASS])

print "login successed ,uid : %s" % uid

odoo有很多经过封装的api包,可以省去很多麻烦

  • ruby版:https://github.com/akretion/ooor
  • python版:
    https://github.com/syleam/openobject-library
    https://github.com/nicolas-van/openerp-client-lib
    http://pythonhosted.org/OdooRPC/
  • php版:https://github.com/abhishek-jaiswal/php-openerp-lib
rpc里的service和对应的method:
common=》(login,authenticate,version,about,set_loglevel)
db=》(dbcreate_database,duplicate_database,drop,dump,restore,rename,change_admin_password,migrate_database,db_exist,list,list_lang,list_countries,server_version)
object=》(execute,execute_kw,execute_workflow)
report=》(report,report_get,render_report)

内容发布自http://www.jianshu.com/u/6fdae8ec06bc,转载请注明出处

你可能感兴趣的:(odoo V10开发文档(第四章:国际化、报表、webservice))