[IMP] models: move prefetching of records back to method _prefetch_field
[odoo/odoo.git] / doc / 03_module_dev_04.rst
1 .. _module-dev-actions:
2
3 =================
4 Menus and Actions 
5 =================
6
7 Menus
8 =====
9
10 Menus are records in the ``ir.ui.menu`` table. In order to create a new
11 menu entry, you can directly create a record using the ``record`` tag.
12
13 .. code-block:: xml
14
15         <record id="menu_xml_id" model="ir.ui.menu">
16           <field name="name">My Menu</field>
17           <field name="action" ref="action_xml_id"/>
18           <field name="sequence" eval="<integer>"/>
19           <field name="parent_id" ref="parent_menu_xml_id"/>
20         </record>
21
22 There is a shortcut by using the ``menuitem`` tag that **you should use
23 preferentially**. It offers a flexible way to easily define the menu entry
24 along with icons and other fields.
25
26 .. code-block:: xml
27
28     <menuitem id="menu_xml_id" 
29         name="My Menu" 
30         action="action_xml_id" 
31         icon="NAME_FROM_LIST" 
32         groups="groupname" 
33         sequence="<integer>"
34         parent="parent_menu_xml_id"
35     />
36
37 Where
38
39  - ``id`` specifies the **xml identifier** of the menu item in the menu
40    items table. This identifier must be unique. Mandatory field.
41  - ``name`` defines the menu name that will be displayed in the client.
42    Mandatory field.
43  - ``action`` specifies the identifier of the attached action defined
44    in the action table (``ir.actions.act_window``). This field is not
45    mandatory : you can define menu elements without associating actions
46    to them. This is useful when defining custom icons for menu elements
47    that will act as folders. This is how custom icons for "Projects" or
48    "Human Resources" in OpenERP are defined).
49  - ``groups`` specifies which group of user can see the menu item.
50    (example : groups="admin"). See section " Management of Access Rights"
51    for more information. Multiple groups should be separated by a ','
52    (example: groups="admin,user")
53  - ``sequence`` is an integer that is used to sort the menu item in the
54    menu. The higher the sequence number, the downer the menu item. This
55    argument is not mandatory: if sequence is not specified, the menu item
56    gets a default sequence number of 10. Menu items with the same sequence
57    numbers are sorted by order of creation (``_order = "*sequence,id*"``).
58
59 The main current limitation of using ``menuitem`` is that the menu action must be an
60 ``act_window`` action. This kind of actions is the most used action in OpenERP.
61 However for some menus you will use other actions. For example, the Feeds
62 page that comes with the mail module is a client action. For this kind of
63 menu entry, you can combine both declaration, as defined in the mail module :
64
65 .. code-block:: xml
66
67         <!-- toplevel menu -->
68         <menuitem id="mail_feeds_main" name="Feeds" sequence="0"
69             web_icon="static/src/img/feeds.png"
70             web_icon_hover="static/src/img/feeds-hover.png" />
71         <record id="mail_feeds_main" model="ir.ui.menu">
72             <field name="action" ref="action_mail_all_feeds"/>
73         </record>
74
75 Actions
76 =======
77
78 The actions define the behavior of the system in response to the actions
79 of the users ; login of a new user, double-click on an invoice, click on the action button, ...
80
81 There are different types of simple actions:
82
83     * **Window**: Opening of a new window
84     * **Report**: The printing of a report
85           o Custom Report: The personalized reports
86           o RML Report: The XSL:RML reports
87     * **Execute**: The execution of a method on the server side
88     * **Group**: Gather some actions in one group
89
90 The actions are used for the following events:
91
92     * User connection,
93     * The user clicks on a menu,
94     * The user clicks on the icon 'print' or 'action'.
95
96 Opening of the menu
97 +++++++++++++++++++
98
99 When the user open the option of the menu "Operations > Partners > Partners Contact", the next steps are done to give the user information on the action to undertake.
100
101    1. Search the action in the IR.
102    2. Execution of the action
103          1. If the action is the type Opening the Window; it indicates to the user that a new window must be opened for a selected object and it gives you the view (form or list) and the filed to use (only the pro-forma invoice).
104          2. The user asks the object and receives information necessary to trace a form; the fields description and the XML view.
105
106 User connection
107 +++++++++++++++
108
109 When a new user is connected to the server, the client must search the action to use for the first screen of this user. Generally, this action is: open the menu in the 'Operations' section.
110
111 The steps are:
112
113    1. Reading of a user file to obtain ACTION_ID
114    2. Reading of the action and execution of this one
115
116 The fields
117 ++++++++++
118
119 **Action Name**
120         The action name
121 **Action Type**
122         Always 'ir.actions.act_window'
123 **View Ref**
124         The view used for showing the object
125 **Model**
126         The model of the object to post
127 **Type of View**
128         The type of view (Tree/Form)
129 **Domain Value**
130         The domain that decreases the visible data with this view
131
132 The view
133 --------
134 The view describes how the edition form or the data tree/list appear on screen. The views can be of 'Form' or 'Tree' type, according to whether they represent a form for the edition or a list/tree for global data viewing.
135
136 A form can be called by an action opening in 'Tree' mode. The form view is generally opened from the list mode (like if the user pushes on 'switch view').
137
138 .. _domain:
139 .. _domains:
140
141 The domain
142 ----------
143
144 This parameter allows you to regulate which resources are visible in a selected view.(restriction)
145
146 For example, in the invoice case, you can define an action that opens a view that shows only invoices not paid.
147
148 The domains are written in python; list of tuples. The tuples have three elements;
149
150     * the field on which the test must be done
151     * the operator used for the test (<, >, =, like)
152     * the tested value
153
154 For example, if you want to obtain only 'Draft' invoice, use the following domain; [('state','=','draft')]
155
156 In the case of a simple view, the domain define the resources which are the roots of the tree. The other resources, even if they are not from a part of the domain will be posted if the user develop the branches of the tree.
157
158 Window Action
159 -------------
160
161 Actions are explained in more detail in section "Administration Modules - Actions". Here's the template of an action XML record :
162 ::
163
164         <record model="ir.actions.act_window" id="action_id_1">
165             <field name="name">action.name</field>
166             <field name="view_id" ref="view_id_1"/>
167             <field name="domain">["list of 3-tuples (max 250 characters)"]</field>
168             <field name="context">{"context dictionary (max 250 characters)"}</field>
169             <field name="res_model">Open.object</field>
170             <field name="view_type">form|tree</field>
171             <field name="view_mode">form,tree|tree,form|form|tree</field>
172             <field name="usage">menu</field>
173             <field name="target">new</field>
174         </record>
175
176 **Where**
177
178     * **id** is the identifier of the action in the table "ir.actions.act_window". It must be unique.
179     * **name** is the name of the action (mandatory).
180     * **view_id** is the name of the view to display when the action is activated. If this field is not defined, the view of a kind (list or form) associated to the object res_model with the highest priority field is used (if two views have the same priority, the first defined view of a kind is used).
181     * **domain** is a list of constraints used to refine the results of a selection, and hence to get less records displayed in the view. Constraints of the list are linked together with an AND clause : a record of the table will be displayed in the view only if all the constraints are satisfied.
182     * **context** is the context dictionary which will be visible in the view that will be opened when the action is activated. Context dictionaries are declared with the same syntax as Python dictionaries in the XML file. For more information about context dictionaries, see section " The context Dictionary".
183     * **res_model** is the name of the object on which the action operates.
184     * **view_type** is set to form when the action must open a new form view, and is set to tree when the action must open a new tree view.
185     * **view_mode** is only considered if view_type is form, and ignored otherwise. The four possibilities are :
186           - **form,tree** : the view is first displayed as a form, the list view can be displayed by clicking the "alternate view button" ;
187           - **tree,form** : the view is first displayed as a list, the form view can be displayed by clicking the "alternate view button" ;
188           - **form** : the view is displayed as a form and there is no way to switch to list view ;
189           - **tree** : the view is displayed as a list and there is no way to switch to form view.
190
191 (version 5 introduced **graph** and **calendar** views)
192
193     * **usage** is used [+ ***TODO*** +]
194     * **target** the view will open in new window like wizard.
195     * **context** will be passed to the action itself and added to its global context
196
197       .. code-block:: xml
198
199           <record model="ir.actions.act_window" id="a">
200               <field name="name">account.account.tree1</field> 
201               <field name="res_model">account.account</field> 
202               <field name="view_type">tree</field> 
203               <field name="view_mode">form,tree</field> 
204               <field name="view_id" ref="v"/> 
205               <field name="domain">[('code','=','0')]</field> 
206               <field name="context">{'project_id': active_id}</field> 
207           </record>
208
209
210
211 They indicate at the user that he has to open a new window in a new 'tab'.
212
213 Administration > Custom > Low Level > Base > Action > Window Actions
214
215 .. .. figure::  images/module_base_action_window.png
216 ..    :scale: 85
217 ..    :align: center
218
219 Examples of actions
220 +++++++++++++++++++
221
222 This action is declared in server/bin/addons/project/project_view.xml.
223 ::
224
225     <record model="ir.actions.act_window" id="open_view_my_project">
226         <field name="name">project.project</field>
227         <field name="res_model">project.project</field>
228         <field name="view_type">tree</field>
229         <field name="domain">[('parent_id','=',False), ('manager', '=', uid)]</field>
230         <field name="view_id" ref="view_my_project" />
231     </record>
232
233 This action is declared in server/bin/addons/stock/stock_view.xml.
234 ::
235
236     <record model="ir.actions.act_window" id="action_picking_form">
237         <field name="name">stock.picking</field>
238         <field name="res_model">stock.picking</field>
239         <field name="type">ir.actions.act_window</field>
240         <field name="view_type">form</field>
241         <field name="view_id" ref="view_picking_form"/>
242         <field name="context">{'contact_display': 'partner'}</field>
243     </record>
244
245 Url Action
246 -----------
247
248 Report Action
249 -------------
250
251 Report declaration
252 ++++++++++++++++++
253
254 Reports in OpenERP are explained in chapter "Reports Reporting". Here's an example of a XML file that declares a RML report :
255 ::
256
257     <?xml version="1.0"?>
258     <openerp>
259         <data>
260         <report id="sale_category_print"
261                 string="Sales Orders By Categories"
262                 model="sale.order"
263                 name="sale_category.print"
264                 rml="sale_category/report/sale_category_report.rml"
265                 menu="True"
266                 auto="False"/>
267          </data>
268     </openerp>
269
270 A report is declared using a **report tag** inside a "data" block. The different arguments of a report tag are :
271
272     * **id** : an identifier which must be unique.
273     * **string** : the text of the menu that calls the report (if any, see below).
274     * **model** : the OpenERP object on which the report will be rendered.
275     * **rml** : the .RML report model. Important Note : Path is relative to addons/ directory.
276     * **menu** : whether the report will be able to be called directly via the client or not. Setting menu to False is useful in case of reports called by wizards.
277     * **auto** : determines if the .RML file must be parsed using the default parser or not. Using a custom parser allows you to define additional functions to your report.
278
279
280
281
282
283 Action creation
284 ---------------
285
286 Linking events to action
287 ++++++++++++++++++++++++
288
289 The available type of events are:
290
291     * **client_print_multi** (print from a list or form)
292     * **client_action_multi** (action from a list or form)
293     * **tree_but_open** (double click on the item of a tree, like the menu)
294     * **tree_but_action** (action on the items of a tree) 
295
296 To map an events to an action:
297
298 .. code-block:: xml
299
300     <record model="ir.values" id="ir_open_journal_period">
301         <field name="key2">tree_but_open</field>
302         <field name="model">account.journal.period</field>
303         <field name="name">Open Journal</field>
304         <field name="value" eval="'ir.actions.wizard,%d'%action_move_journal_line_form_select"/>
305         <field name="object" eval="True"/>
306     </record>
307
308 If you double click on a journal/period (object: account.journal.period), this will open the selected wizard. (id="action_move_journal_line_form_select").
309
310 You can use a res_id field to allow this action only if the user click on a specific object.
311
312 .. code-block:: xml
313
314     <record model="ir.values" id="ir_open_journal_period">
315         <field name="key2">tree_but_open</field>
316         <field name="model">account.journal.period</field>
317         <field name="name">Open Journal</field>
318         <field name="value" eval="'ir.actions.wizard,%d'%action_move_journal_line_form_select"/>
319         <field name="res_id" eval="3"/>
320         <field name="object" eval="True"/>
321     </record>
322
323 The action will be triggered if the user clicks on the account.journal.period n°3.
324
325 When you declare wizard, report or menus, the ir.values creation is automatically made with these tags:
326
327   * <menuitem... />
328   * <report... /> 
329
330 So you usually do not need to add the mapping by yourself.
331