国际化
每个模块可以提供一个国际化翻译模块,存放在模块的i18n文件夹中的xx.po文件中,如中文就是zh_CN.po,所有翻译文件在odoo启动时会自动加载。一般默认用英语来开发模块,然后用odoo内置的导出翻译功能导出生成pot模板,然后就可以根据这个模板编辑成其他语言的了。
报表
报表打印
odoo从8.0开始使用qweb引警和bootstrap 、Wkhtmltopdf来打印报表
报表由两个元素组成
- report标签的ir.actions.report.xml,它会初始化一些报表的基本参数如默认类型、生成后是否保存到数据库
<report
id="account_invoices"
model="account.invoice"
string="Invoices"
report_type="qweb-pdf"
name="account.report_invoice"
file="account.report_invoice"
attachment_use="True"
attachment="(object.state in ('open','paid')) and
('INV'+(object.number or '').replace('/','')+'.pdf')"
/>
- 一个标准的Qweb视图用于实际生成报表
<t t-call="report.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="report.external_layout">
<div class="page">
<h2>Report title</h2>
</div>
</t>
</t>
</t>
报表是标准的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
<odoo>
<data>
<report
id="report_session"
model="openacademy.session"
string="Session Report"
name="openacademy.report_session_view"
file="openacademy.report_session"
report_type="qweb-pdf" />
<template id="report_session_view">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="report.external_layout">
<div class="page">
<h2 t-field="doc.name"/>
<p>From <span t-field="doc.start_date"/> to <span t-field="doc.end_date"/></p>
<h3>Attendees:</h3>
<ul>
<t t-foreach="doc.attendee_ids" t-as="attendee">
<li><span t-field="attendee.name"/></li>
</t>
</ul>
</div>
</t>
</t>
</t>
</template>
</data>
</odoo>
仪表板
实例:制作一个仪表板,包含已经创建的日历视图、图形视图、和课程列表,并将它加到菜单项中,当进入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
<?xml version="1.0"?>
<odoo>
<data>
<record model="ir.actions.act_window" id="act_session_graph">
<field name="name">Attendees by course</field>
<field name="res_model">openacademy.session</field>
<field name="view_type">form</field>
<field name="view_mode">graph</field>
<field name="view_id"
ref="openacademy_session_graph_view"/>
</record>
<record model="ir.actions.act_window" id="act_session_calendar">
<field name="name">Sessions</field>
<field name="res_model">openacademy.session</field>
<field name="view_type">form</field>
<field name="view_mode">calendar</field>
<field name="view_id" ref="session_calendar_view"/>
</record>
<record model="ir.actions.act_window" id="act_course_list">
<field name="name">Courses</field>
<field name="res_model">openacademy.courses</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<record model="ir.ui.view" id="board_session_form">
<field name="name">Session Dashboard Form</field>
<field name="model">board.board</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Session Dashboard">
<board style="2-1">
<column>
<action
string="Attendees by course"
name="%(act_session_graph)d"
height="150"
width="510"/>
<action
string="Sessions"
name="%(act_session_calendar)d"/>
</column>
<column>
<action
string="Courses"
name="%(act_course_list)d"/>
</column>
</board>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="open_board_session">
<field name="name">Session Dashboard</field>
<field name="res_model">board.board</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="usage">menu</field>
<field name="view_id" ref="board_session_form"/>
</record>
<menuitem
name="Session仪表板"
action="open_board_session"
sequence="1"
id="menu_board_session" icon="terp-graph"/>
</data>
</odoo>
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,转载请注明出处