~openerp-community/openobject-doc/6.1

« back to all changes in this revision

Viewing changes to source/developer/2_4_module_development/4_3_create_module.rst

  • Committer: TruongSinh Tran
  • Date: 2009-07-19 19:02:35 UTC
  • Revision ID: truongsinh@vipescoserver-20090719190235-fu3bxcrbpvwuk5h7
[FIX] build_i18n.py .. raw:: html

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
Create Module
 
3
=============
 
4
 
 
5
Getting the skeleton directory
 
6
------------------------------
 
7
 
 
8
Creating a new module is quickly done by copying the module called "simple" or
 
9
"custom" (depending on your OpenERP version) into a new directory.
 
10
 
 
11
As an example on Ubuntu:
 
12
::
 
13
 
 
14
    $ cd /usr/lib/openerp-server/addons/
 
15
    $ sudo cp -r custom travel
 
16
 
 
17
You will need to give yourself permissions over that new directory if you want
 
18
to be able to modify it: ::
 
19
 
 
20
    $ sudo chown -R `whoami` travel
 
21
 
 
22
You got yourself the directory for a new module there, and a skeleton
 
23
structure, but you still need to change a few things inside the module's
 
24
definition...
 
25
 
 
26
Changing the default definition
 
27
-------------------------------
 
28
 
 
29
To change the default settings of the custom module (which is now the "travel" module),
 
30
get yourself into the "travel" directory and edit *__terp__.py* (with *gedit*,
 
31
for example, a simple text editor. Feel free to use another one) ::
 
32
 
 
33
    $ cd travel
 
34
    $ gedit __terp__.py
 
35
 
 
36
The file looks like this:
 
37
 
 
38
.. code-block:: python
 
39
 
 
40
    #
 
41
    # Use the custom module to put your specific code in a separate module.
 
42
    #
 
43
    {
 
44
        "name" : "Module for custom developments",
 
45
        "version" : "1.0",
 
46
        "author" : "Tiny",
 
47
        "category" : "Generic Modules/Others",
 
48
        "website": "http://www.tinyerp.com",
 
49
        "description": "Sample custom module where you can put your customer specific developments.",
 
50
        "depends" : ["base"],
 
51
        "init_xml" : [],
 
52
        "update_xml" : ["custom_view.xml"],
 
53
        "active": False,
 
54
        "installable": True
 
55
    }
 
56
 
 
57
You will want to change whichever settings you feel right and get something like this:
 
58
 
 
59
.. code-block:: python
 
60
 
 
61
    {
 
62
        "name" : "Travel agency module",
 
63
        "version" : "1.0",
 
64
        "author" : "Tiny",
 
65
        "category" : "Generic Modules/Others",
 
66
        "website": "http://www.tinyerp.com",
 
67
        "description": "A module to manage hotel bookings and a few other useful features.",
 
68
        "depends" : ["base"],
 
69
        "init_xml" : [],
 
70
        "update_xml" : ["custom_view.xml"],
 
71
        "active": True,
 
72
        "installable": True
 
73
    }
 
74
 
 
75
 
 
76
Note the "active" field becomes true.
 
77
 
 
78
Changing the main module file
 
79
-----------------------------
 
80
 
 
81
Now you need to update the custom.py script to suit the needs of your module.
 
82
We suggest you follow the Flash tutorial for this or download the travel agency
 
83
module from the 20 minutes tutorial page.  ::
 
84
 
 
85
    The documentation below is overlapping the two next step in this wiki tutorial,
 
86
    so just consider them as a help and head towards the next two pages first...
 
87
 
 
88
The custom.py file should initially look like this (intentionally removing the comments):
 
89
 
 
90
.. code-block:: python
 
91
 
 
92
    from osv import osv, fields
 
93
 
 
94
    #class custom_material(osv.osv):
 
95
    #       _name = 'network.material'
 
96
    #       _inherit = 'network.material'
 
97
    #       _columns = {
 
98
    #       }
 
99
    #       _defaults = {
 
100
    #       }
 
101
    #custom_material()
 
102
 
 
103
The '#' signs represent comments. You'll have to remove them, rename the class
 
104
and its attributes to something like this:
 
105
 
 
106
.. code-block:: python
 
107
 
 
108
    from osv import osv, fields
 
109
 
 
110
    class travel_hostel(osv.osv):
 
111
           _name = 'travel.hostel'
 
112
           _inherit = 'res.partner'
 
113
           _columns = {
 
114
           'rooms_id': fields.one2many('travel.room', 'hostel_id', 'Rooms'),
 
115
           'quality': fields.char('Quality', size=16),
 
116
           }
 
117
           _defaults = {
 
118
           }
 
119
    travel_hostel()
 
120
 
 
121
Ideally, you would copy that bunch of code several times to create all the
 
122
entities you need (travel_airport, travel_room, travel_flight). This is what
 
123
will hold the database structure of your objects, but you don't really need to
 
124
worry too much about the database side. Just filling this file will create the
 
125
system structure for you when you install the module.
 
126
 
 
127
Customizing the view
 
128
--------------------
 
129
 
 
130
You can now move on to editing the views. To do this, edit the custom_view.xml file. It should first look like this:
 
131
 
 
132
.. code-block:: xml
 
133
 
 
134
    <terp>
 
135
    <data>
 
136
        <record model="res.groups" id="group_compta_user">
 
137
                <field name="name">grcompta</field>
 
138
        </record>
 
139
        <record model="res.groups" id="group_compta_admin">
 
140
                <field name="name">grcomptaadmin</field>
 
141
        </record>
 
142
        <menuitem name="Administration" groups="admin,grcomptaadmin" icon="terp-stock" id="menu_admin_compta"/>
 
143
    </data>
 
144
    </terp>
 
145
 
 
146
This is, as you can see, an example taken from an accounting system (French
 
147
people call accounting "comptabilité", which explains the compta bit).
 
148
 
 
149
Defining a view is defining the interfaces the user will get when accessing
 
150
your module. Just defining a bunch of fields here should already get you
 
151
started on a complete interface. However, due to the complexity of doing it
 
152
right, we recommend, once again, that you take a look at the 20 minutes Flash
 
153
tutorial or download the travel agency module example.
 
154
 
 
155
Next you should be able to create different views using other files to separate
 
156
them from your basic/admin view.
 
157