创建模块
Getting the skeleton directory
你可以从其他任意模块中复制文件__openerp__.py和__init__.py到一个新目录来创建一个新模块。
Ubuntu中一个例子:
$ cd ~/workspace/stable/stable_addons_5.0/
$ mkdir travel
$ sudo cp ~/workspace/stable/stable_addons_5.0/hr/__openerp__.py ~/workspace/stable/stable_addons_5.0/travel
sudo cp ~/workspace/stable/stable_addons_5.0/hr/__init__.py ~/workspace/stable/stable_addons_5.0/travel
你如果想修改这个目录,你需要设置自己的权限在这个目录上:
$ sudo chown -R `whoami` travel
进入新模块的目录,里面有个框架结构,你仍需要去更改模块定义里面的东西。
Changing the default definition
为了更改模块“travel”里面的默认设置,我们需要进入“travel”目录,编辑__openerp__.py文件。
$ cd travel
$ gedit __openerp__.py
文件里面类似下面:
{
"name" : "Human Resources",
"version" : "1.1",
"author" : "Tiny",
"category" : "Generic Modules/Human Resources",
"website" : "http://www.openerp.com",
"description": """
Module for human resource management. You can manage:
* Employees and hierarchies
* Work hours sheets
* Attendances and sign in/out system
Different reports are also provided, mainly for attendance statistics.
""",
'author': 'Tiny',
'website': 'http://www.openerp.com',
'depends': ['base', 'process'],
'init_xml': [],
'update_xml': [
'security/hr_security.xml',
'security/ir.model.access.csv',
'hr_view.xml',
'hr_department_view.xml',
'process/hr_process.xml'
],
'demo_xml': ['hr_demo.xml', 'hr_department_demo.xml'],
'installable': True,
'active': False,
'certificate': '0086710558965',
}
你可能会更改任意你觉得正确的东西,像下面这样:
{
"name" : "Travel agency module",
"version" : "1.1",
"author" : "Tiny",
"category" : "Generic Modules/Others",
"website" : "http://www.openerp.com",
"description": "A module to manage hotel bookings and a few other useful features.",
"depends" : ["base"],
"init_xml" : [],
"update_xml" : ["travel_view.xml"],
"active": True,
"installable": True
}
注意“active”字段变成了true。
Changing the main module file
Now you need to update the travel.py script to suit the needs of your module. We suggest you follow the Flash tutorial for this or download the travel agency module from the 20 minutes tutorial page.
The documentation below is overlapping the two next step in this wiki tutorial,
so just consider them as a help and head towards the next two pages first...
travel.py文件应该看起来是这样:
from osv import osv, fields
class travel_hostel(osv.osv):
_name = 'travel.hostel'
_inherit = 'res.partner'
_columns = {
'rooms_id': fields.one2many('travel.room', 'hostel_id', 'Rooms'),
'quality': fields.char('Quality', size=16),
}
_defaults = {
}
travel_hostel()
理想情况下,你会拷贝那些代码几次来创建你所需要的实体(travel_airport, travel_room, travel_flight)。这就是你的对象的数据库结构,但是你真的不需要担心数据库端。当你安装模块时,这个文件会为你创建系统架构。
Customizing the view
接下来你可以编辑视图。编辑custom_view.xml文件,像这样:
<openerp>
<data>
<record model="res.groups" id="group_compta_user">
<field name="name">grcompta</field>
</record>
<record model="res.groups" id="group_compta_admin">
<field name="name">grcomptaadmin</field>
</record>
<menuitem name="Administration" groups="admin,grcomptaadmin"
icon="terp-stock" id="menu_admin_compta"/>
</data>
</openerp>
就像你看到的,这是个accounting系统的例子。
定义视图就是定义访问你的模块时的用户界面。这里定义的这些字段已经是一个完整的界面。然而,由于做这个的复杂性,我们建议,再一次,从链接http://www.openerp.com/download/modules/5.0/下载travel agent模块。
下次你可以使用其他的文件来定义不同的视图,并且在你的basic/admin视图中分开它们。