second submodule: website_house_booking
[odoo/odoo.git] / doc / 03_module_dev_01.rst
1 .. _module-dev-structure:
2
3 Module structure
4 ================
5
6 A module can contain the following elements:
7
8  - **Business object** : declared as Python classes extending the class
9    osv.Model, the persistence of these resource is completly managed by
10    OpenERP's ORM.
11  - **Data** : XML/CSV files with meta-data (views and workflows declaration), 
12    configuration data (modules parametrization) and demo data (optional but 
13    recommended for testing),
14  - **Reports** : RML (XML format). HTML/MAKO or OpenOffice report templates, to
15    be merged with any kind of business data, and generate HTML, ODT or PDF
16    reports.
17
18 .. figure:: _static/03_module_gen_view.png
19    :width: 75%
20    :alt: Module composition
21    :align: center
22    
23    Module composition
24
25 Each module is contained in its own directory within either the server/bin/addons 
26 directory or another directory of addons, configured in server installation.
27 To create a new module for example the 'OpenAcademy' module, the following
28 steps are required:
29
30  - create a ``openacademy`` subdirectory in the source/addons directory
31  - create the module import file ``__init__.py``
32  - create the module manifield file ``__openerp__.py``
33  - create **Python** files containing **objects**
34  - create **.xml files** holding module data such as views, menu entries 
35    or demo data
36  - optionally create **reports** or **workflows**
37
38 Python import file __init__.py
39 ++++++++++++++++++++++++++++++
40
41 The ``__init__.py`` file is the Python import file, because an OpenERP module
42 is also a regular Python module. The file should import all the other python
43 file or submodules.
44
45 For example, if a module contains a single python file named ``openacademy.py``,
46 the file should look like:
47
48     import openacademy
49
50 Manifest file __openerp__.py
51 +++++++++++++++++++++++++++++++
52
53 In the created module directory, you must add a **__openerp__.py** file.
54 This file, which must be a Python dict literal, is responsible to
55
56    1. determine the *XML files that will be parsed* during the initialization
57       of the server, and also to
58    2. determine the *dependencies* of the created module.
59    3. declare additional meta data
60
61 This file must contain a Python dictionary with the following values:
62
63 ::
64
65   name             The name of the module in English.
66   version          The version of the module.
67   summary          Short description or keywords
68   description      The module description (text).
69   category         The categrory of the module
70   author           The author of the module.
71   website          URL of the website of the module.
72   license          The license of the module (default: AGPL-3).
73   depends          List of modules on which this module depends beside base.
74   data             List of .xml files to load when the module is installed or updated.
75   demo             List of additional .xml files to load when the module is
76                    installed or updated and demo flag is active.
77   installable      True or False. Determines whether the module is installable 
78                    or not.
79   auto_install     True or False (default: False). If set to ``True``, the
80                    module is a link module. It will be installed as soon
81                    as all its dependencies are installed.
82
83 For the ``openacademy`` module, here is an example of ``__openerp__.py``
84 declaration file:
85
86 .. code-block:: python
87
88     {
89         'name' : "OpenAcademy",
90         'version' : "1.0",
91         'author' : "OpenERP SA",
92         'category' : "Tools",
93         'depends' : ['mail'],
94         'data' : [
95             'openacademy_view.xml',
96             'openacademy_data.xml',
97             'report/module_report.xml',
98             'wizard/module_wizard.xml',
99         ],
100         'demo' : [
101             'openacademy_demo.xml'
102         ],
103         'installable': True,
104     }
105
106 Objects
107 +++++++
108
109 All OpenERP resources are objects: invoices, partners. Metadata are also object
110 too: menus, actions, reports...  Object names are hierarchical, as in the
111 following examples:
112
113     * account.transfer : a money transfer
114     * account.invoice : an invoice
115     * account.invoice.line : an invoice line
116
117 Generally, the first word is the name of the module: account, stock, sale.
118
119 Those object are declared in python be subclassing osv.Model
120
121 The ORM of OpenERP is constructed over PostgreSQL. It is thus possible to
122 query the object used by OpenERP using the object interface (ORM) or by
123 directly using SQL statements.
124
125 But it is dangerous to write or read directly in the PostgreSQL database, as
126 you will shortcut important steps like constraints checking or workflow
127 modification.
128
129 .. .. figure::  images/pom_3_0_3.png
130 ..    :scale: 50
131 ..    :align: center
132
133 ..    *The Physical Objects Model of [OpenERP version 3.0.3]*
134
135
136 XML Files
137 +++++++++
138
139 XML files located in the module directory are used to initialize or update the
140 the database when the module is installed or updated. They are used for many
141 purposes, among which we can cite :
142
143     * initialization and demonstration data declaration,
144     * views declaration,
145     * reports declaration,
146     * workflows declaration.
147
148 General structure of OpenERP XML files is more detailed in the 
149 :ref:`xml-serialization` section. Look here if you are interested in learning 
150 more about *initialization* and *demonstration data declaration* XML files. The 
151 following section are only related to XML specific to *actions, menu entries, 
152 reports, wizards* and *workflows* declaration.
153
154 Data can be inserted or updated into the PostgreSQL tables corresponding to the
155 OpenERP objects using XML files. The general structure of an OpenERP XML file
156 is as follows:
157
158 .. code-block:: xml
159
160    <?xml version="1.0"?>
161    <openerp>
162      <data>
163        <record model="model.name_1" id="id_name_1">
164          <field name="field1"> "field1 content" </field>
165          <field name="field2"> "field2 content" </field>
166          (...)
167        </record>
168        <record model="model.name_2" id="id_name_2">
169            (...)
170        </record>
171        (...)
172      </data>
173    </openerp>
174
175 ``<record>``
176 ////////////
177
178 Defines a new record in a specified OpenERP model.
179
180 ``@model`` (required)
181
182     Name of the model in which this record will be created/inserted.
183
184 ``@id`` (optional)
185
186     :term:`external ID` for the record, also allows referring to this record in
187     the rest of this file or in other files (through ``field/@ref`` or the
188     :py:func:`ref() <openerp.tools.convert._ref>` function)
189
190 A record tag generally contains multiple ``field`` tags specifying the values
191 set on the record's fields when creating it. Fields left out will be set to
192 their default value unless required.
193
194 ``<field>``
195 ///////////
196
197 In its most basic use, the ``field`` tag will set its body (as a string) as
198 the value of the corresponding ``record``'s ``@name`` field.
199
200 Extra attributes can either preprocess the body or replace its use entirely:
201
202 ``@name`` (mandatory)
203
204     Name of the field in the containing ``record``'s model
205
206 ``@type`` (optional)
207
208     One of ``char``, ``int``, ``float``, ``list``, ``tuple``, ``xml`` or
209     ``html``, ``file`` or ``base64``. Converts the ``field``'s body to the
210     specified type (or validates the body's content)
211
212     * ``xml`` will join multiple XML nodes under a single ``<data>`` root
213     * in ``xml`` and ``html``, external ids can be referenced using
214       ``%(id_name)s``
215     * ``list`` and ``tuple``'s element are specified using ``<value>``
216       sub-nodes with the same attributes as ``field``.
217     * ``file`` expects a module-local path and will save the path prefixed with
218       the current module's name, separated by a ``,`` (comma). For use with
219       :py:func:`~openerp.modules.module.get_module_resource`.
220     * ``base64`` expects binary data, encodes it to base64 and sets it. Mostly
221       useful with ``@file``
222
223 ``@file``
224
225     Can be used with types ``char`` and ``base64``, sources the field's content
226     from the specified file instead of the field's text body.
227
228 ``@model``
229
230     Model used for ``@search``'s search, or registry object put in context for
231     ``@eval``. Required if ``@search`` but optional if ``@eval``.
232
233 ``@eval`` (optional)
234
235     A Python expression evaluated to obtain the value to set on the record
236
237 ``@ref`` (optional)
238
239     Links to an other record through its :term:`external id`. The module prefix
240     may be ommitted to link to a record defined in the same module.
241
242 ``@search`` (optional)
243
244     Search domain (evaluated Python expression) into ``@model`` to get the
245     records to set on the field.
246
247     Sets all the matches found for m2m fields, the first id for other field
248     types.
249
250 **Example**
251
252 .. code-block:: xml
253
254     <record model="ir.actions.report.xml" id="l0">
255          <field name="model">account.invoice</field>
256          <field name="name">Invoices List</field>
257          <field name="report_name">account.invoice.list</field>
258          <field name="report_xsl">account/report/invoice.xsl</field>
259          <field name="report_xml">account/report/invoice.xml</field>
260     </record>
261
262 Let's review an example taken from the OpenERP source (base_demo.xml in the base module):
263
264 .. code-block:: xml
265
266        <record model="res.company" id="main_company">
267            <field name="name">Tiny sprl</field>
268            <field name="partner_id" ref="main_partner"/>
269            <field name="currency_id" ref="EUR"/>
270        </record>
271
272 .. code-block:: xml
273
274        <record model="res.users" id="user_admin">
275            <field name="login">admin</field>
276            <field name="password">admin</field>
277            <field name="name">Administrator</field>
278            <field name="signature">Administrator</field>
279            <field name="action_id" ref="action_menu_admin"/>
280            <field name="menu_id" ref="action_menu_admin"/>
281            <field name="address_id" ref="main_address"/>
282            <field name="groups_id" eval="[(6,0,[group_admin])]"/>
283            <field name="company_id" ref="main_company"/>
284        </record>
285
286 This last record defines the admin user :
287
288     * The fields login, password, etc are straightforward.
289     * The ref attribute allows to fill relations between the records :
290
291 .. code-block:: xml
292
293        <field name="company_id" ref="main_company"/>
294
295 The field **company_id** is a many-to-one relation from the user object to the company object, and **main_company** is the id of to associate.
296
297     * The **eval** attribute allows to put some python code in the xml: here the groups_id field is a many2many. For such a field, "[(6,0,[group_admin])]" means : Remove all the groups associated with the current user and use the list [group_admin] as the new associated groups (and group_admin is the id of another record).
298
299     * The **search** attribute allows to find the record to associate when you do not know its xml id. You can thus specify a search criteria to find the wanted record. The criteria is a list of tuples of the same form than for the predefined search method. If there are several results, an arbitrary one will be chosen (the first one):
300
301 .. code-block:: xml
302
303        <field name="partner_id" search="[]" model="res.partner"/>
304
305 This is a classical example of the use of **search** in demo data: here we do not really care about which partner we want to use for the test, so we give an empty list. Notice the **model** attribute is currently mandatory.
306
307 Function tag
308 ////////////
309
310 A function tag can contain other function tags.
311
312 model : mandatory
313   The model to be used
314
315 name : mandatory
316   the function given name
317
318 eval
319   should evaluate to the list of parameters of the method to be called, excluding cr and uid
320
321 **Example**
322
323 .. code-block:: xml
324
325     <function model="ir.ui.menu" name="search" eval="[[('name','=','Operations')]]"/>
326
327
328 Views
329 +++++
330
331 Views are a way to represent the objects on the client side. They indicate to the client how to lay out the data coming from the objects on the screen.
332
333 There are two types of views:
334
335     * form views
336     * tree views
337
338 Lists are simply a particular case of tree views.
339
340 A same object may have several views: the first defined view of a kind (*tree, form*, ...) will be used as the default view for this kind. That way you can have a default tree view (that will act as the view of a one2many) and a specialized view with more or less information that will appear when one double-clicks on a menu item. For example, the products have several views according to the product variants.
341
342 Views are described in XML.
343
344 If no view has been defined for an object, the object is able to generate a view to represent itself. This can limit the developer's work but results in less ergonomic views.
345
346
347 Usage example
348 /////////////
349
350 When you open an invoice, here is the chain of operations followed by the client:
351
352     * An action asks to open the invoice (it gives the object's data (account.invoice), the view, the domain (e.g. only unpaid invoices) ).
353     * The client asks (with XML-RPC) to the server what views are defined for the invoice object and what are the data it must show.
354     * The client displays the form according to the view
355
356 .. .. figure::  images/arch_view_use.png
357 ..    :scale: 50
358 ..    :align: center
359
360 To develop new objects
361 //////////////////////
362
363 The design of new objects is restricted to the minimum: create the objects and optionally create the views to represent them. The PostgreSQL tables do not have to be written by hand because the objects are able to automatically create them (or adapt them in case they already exist).
364
365 Reports
366 """""""
367
368 OpenERP uses a flexible and powerful reporting system. Reports are generated either in PDF or in HTML. Reports are designed on the principle of separation between the data layer and the presentation layer.
369
370 Reports are described more in details in the `Reporting <http://openobject.com/wiki/index.php/Developers:Developper%27s_Book/Reports>`_ chapter.
371
372
373 Workflow
374 """"""""
375
376 The objects and the views allow you to define new forms very simply, lists/trees and interactions between them. But that is not enough, you must define the dynamics of these objects.
377
378 A few examples:
379
380     * a confirmed sale order must generate an invoice, according to certain conditions
381     * a paid invoice must, only under certain conditions, start the shipping order
382
383 The workflows describe these interactions with graphs. One or several workflows may be associated to the objects. Workflows are not mandatory; some objects don't have workflows.
384
385 Below is an example workflow used for sale orders. It must generate invoices and shipments according to certain conditions.
386
387 .. .. figure::  images/arch_workflow_sale.png
388 ..    :scale: 85
389 ..    :align: center
390
391
392 In this graph, the nodes represent the actions to be done:
393
394     * create an invoice,
395     * cancel the sale order,
396     * generate the shipping order, ...
397
398 The arrows are the conditions;
399
400     * waiting for the order validation,
401     * invoice paid,
402     * click on the cancel button, ...
403
404 The squared nodes represent other Workflows;
405
406     * the invoice
407     * the shipping
408
409
410 i18n
411 ----
412
413 .. versionchanged:: 5.0
414
415 Each module has its own ``i18n`` folder. In addition, OpenERP can now deal with
416 ``.po`` [#f_po]_ files as import/export format. The translation files of the
417 installed languages are automatically loaded when installing or updating a
418 module.
419
420 Translations are managed by the `Launchpad Web interface
421 <https://translations.launchpad.net/openobject>`_. Here, you'll find the list
422 of translatable projects.
423
424 Please read the `FAQ <https://answers.launchpad.net/rosetta/+faqs>`_ before asking questions.
425
426
427 .. [#f_po] http://www.gnu.org/software/autoconf/manual/gettext/PO-Files.html#PO-Files
428
429