创建模块

Getting the skeleton directory

你可以从其他任意模块中复制文件__openerp__.py和__init__.py到一个新目录来创建一个新模块。

Ubuntu中一个例子:

  1. $ cd ~/workspace/stable/stable_addons_5.0/
  2. $ mkdir travel
  3. $ sudo cp ~/workspace/stable/stable_addons_5.0/hr/__openerp__.py ~/workspace/stable/stable_addons_5.0/travel
  4. sudo cp ~/workspace/stable/stable_addons_5.0/hr/__init__.py ~/workspace/stable/stable_addons_5.0/travel

你如果想修改这个目录,你需要设置自己的权限在这个目录上:

  1. $ sudo chown -R `whoami` travel

进入新模块的目录,里面有个框架结构,你仍需要去更改模块定义里面的东西。

Changing the default definition

为了更改模块“travel”里面的默认设置,我们需要进入“travel”目录,编辑__openerp__.py文件。

  1. $ cd travel
  2. $ gedit __openerp__.py

文件里面类似下面:

  1. {
  2. "name" : "Human Resources",
  3. "version" : "1.1",
  4. "author" : "Tiny",
  5. "category" : "Generic Modules/Human Resources",
  6. "website" : "http://www.openerp.com",
  7. "description": """
  8. Module for human resource management. You can manage:
  9. * Employees and hierarchies
  10. * Work hours sheets
  11. * Attendances and sign in/out system
  12. Different reports are also provided, mainly for attendance statistics.
  13. """,
  14. 'author': 'Tiny',
  15. 'website': 'http://www.openerp.com',
  16. 'depends': ['base', 'process'],
  17. 'init_xml': [],
  18. 'update_xml': [
  19. 'security/hr_security.xml',
  20. 'security/ir.model.access.csv',
  21. 'hr_view.xml',
  22. 'hr_department_view.xml',
  23. 'process/hr_process.xml'
  24. ],
  25. 'demo_xml': ['hr_demo.xml', 'hr_department_demo.xml'],
  26. 'installable': True,
  27. 'active': False,
  28. 'certificate': '0086710558965',
  29. }

你可能会更改任意你觉得正确的东西,像下面这样:

  1. {
  2. "name" : "Travel agency module",
  3. "version" : "1.1",
  4. "author" : "Tiny",
  5. "category" : "Generic Modules/Others",
  6. "website" : "http://www.openerp.com",
  7. "description": "A module to manage hotel bookings and a few other useful features.",
  8. "depends" : ["base"],
  9. "init_xml" : [],
  10. "update_xml" : ["travel_view.xml"],
  11. "active": True,
  12. "installable": True
  13. }

注意“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.

  1. The documentation below is overlapping the two next step in this wiki tutorial,
  2. so just consider them as a help and head towards the next two pages first...

travel.py文件应该看起来是这样:

  1. from osv import osv, fields
  2. class travel_hostel(osv.osv):
  3. _name = 'travel.hostel'
  4. _inherit = 'res.partner'
  5. _columns = {
  6. 'rooms_id': fields.one2many('travel.room', 'hostel_id', 'Rooms'),
  7. 'quality': fields.char('Quality', size=16),
  8. }
  9. _defaults = {
  10. }
  11. travel_hostel()

理想情况下,你会拷贝那些代码几次来创建你所需要的实体(travel_airport, travel_room, travel_flight)。这就是你的对象的数据库结构,但是你真的不需要担心数据库端。当你安装模块时,这个文件会为你创建系统架构。

Customizing the view

接下来你可以编辑视图。编辑custom_view.xml文件,像这样:

  1. <openerp>
  2. <data>
  3. <record model="res.groups" id="group_compta_user">
  4. <field name="name">grcompta</field>
  5. </record>
  6. <record model="res.groups" id="group_compta_admin">
  7. <field name="name">grcomptaadmin</field>
  8. </record>
  9. <menuitem name="Administration" groups="admin,grcomptaadmin"
  10. icon="terp-stock" id="menu_admin_compta"/>
  11. </data>
  12. </openerp>

就像你看到的,这是个accounting系统的例子。

定义视图就是定义访问你的模块时的用户界面。这里定义的这些字段已经是一个完整的界面。然而,由于做这个的复杂性,我们建议,再一次,从链接http://www.openerp.com/download/modules/5.0/下载travel agent模块。

下次你可以使用其他的文件来定义不同的视图,并且在你的basic/admin视图中分开它们。