My favorites | Sign in
Logo
                
Search
for
Updated Dec 10, 2009 by juanf.ja...@miginternacional.com
Labels: Featured
Locales: en, fr
HelloWorld  
Creating a new Module

Module Creation

We will start by creating a module called HelloWorld, in the beginning our module will only have an entry in the main menu. We will add more features later on. You can download the source from here: helloWorld.tar.gz. Now let's get started.

Step 1: Creating the HelloWorld module

Create the directory called helloWorld in the trytond/modules directory.

In Linux you create the directory like this:

cd  /usr/local/tryton/trytond/trytond/modules/
mkdir helloWorld

Inside this directory put the file __init__.py, this file will call our .py files, as in the following line:

from hello import *

Then create the file __tryton__.py with the following lines:

{
    'name' : 'Hello World',
    'version' : '0.0.1',
    'author' : 'nickname',
    'email': 'correo@example.com',
    'website': 'http://www.tryton.org/',
    'description': 'Hello World with menus',
    'depends' : [
        'ir',
    ],
    'xml' : [
        'hello.xml',
    ],
    'translation': [
    ],
}

Add the file hello.py

from trytond.model import ModelView, ModelSQL, fields
class Hello(ModelSQL, ModelView):
    'HelloWorld'
    _name = 'hello.hello'
    _description = __doc__

Hello()

And then in the last file hello.xml we put:

<?xml version="1.0"?>

<tryton>
    <data>
        <menuitem name="Hello World" id="menu_hello_world" sequence="10" icon="tryton-users"/>
    </data>
</tryton>

With this we have the first module to import, which will show in the main menu without any information.

Note If you have problem with the icon, remove it from the hello.xml file.

Step 2: Extending our model and adding views

Now that our module is working we will add some more features. Let's extend our model and add two views to the module. To extend our model we edit the file hello.py and add these two fields to the class Hello:

    name = fields.Char('Name')
    greeting = fields.Char('Greeting')

with this our file will be:

from trytond.model import ModelView, ModelSQL, fields
class Hello(ModelSQL, ModelView):
    'HelloWorld'
    _name = 'hello.hello'
    _description = __doc__
    name = fields.Char('Name')
    greeting = fields.Char('Greeting')

Hello()

This tells tryton to create two fields, both of type Varchar or String.

After this we edit the file hello.xml and add the following groups of lines. Each group of lines is preceded with an explaination. For more information read the Views section of the Tryton documentation found at tryton.org.

First we create a view of type tree which will list our model hello.hello with its two fields: name and greeting.

        <!-- Vista para el Arbol del saludo-->
        <record model="ir.ui.view" id="hello_view_tree">
            <field name="model">hello.hello</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <![CDATA[
                <tree string="Hello World">
                    <field name="name" select="1"/>
                    <field name="greeting" select="1"/>
                </tree>
                ]]>
            </field>
        </record>

Secondly we create a view of type form which will show a single record of our model hello.hello in a form with with the two labels and two fields: name and greeting.

       <record model="ir.ui.view" id="hello_view_form">
            <field name="model">hello.hello</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
              <![CDATA[
              <form string="Hello">
                <label name="name"/>
                <field name="name"/>
                <label name="greeting"/>
                <field name="greeting"/> 
              </form>
              ]]>
            </field>
       </record>

Create an event to open a new tab(window). This event will be used to connect the menu Hello World with the tree and form views. Note the id of this event as it will be used later.

        <!-- View for the main menu and the event -->
        <record model="ir.action.act_window" id="act_hello_world_form">
            <field name="name">Hello World</field>
            <field name="res_model">hello.hello</field>
            <field name="view_type">form</field>
        </record>

With the ids of the event and the two views defined before we add two fragments to attach the id to the window and the view used to populate the window. The field view indicates which view will be used and the field act_window indicates the event that will activate the view.

        <!-- View that connect the form in the tree with the specification -->
        <record model="ir.action.act_window.view" id="act_hello_form_view1">
            <field name="sequence" eval="10"/>
            <field name="view" ref="hello_view_tree"/>
            <field name="act_window" ref="act_hello_world_form"/>
        </record>

        <!-- View for the edition or the Form of hello-->
        <record model="ir.action.act_window.view" id="act_hello_form_view2">
            <field name="sequence" eval="20"/>
            <field name="view" ref="hello_view_form"/>
            <field name="act_window" ref="act_hello_world_form"/>
        </record>

We use ids for each record throughout the xml file. This allows other modules to identify and reuse or extend parts of our module.

Now finally at the end of our xml file we add one more fragment to join the views with the last menu:

        <menuitem parent="menu_hello_world" sequence="1"
            id="menu_hello_world_form" icon="tryton-list" action="act_hello_world_form"/>

Step 3: Create a Menu entry in the Admin Menu

First we repeat the pattern we used above for the view and the action act_hello_admin_form by adding these lines:

        <record model="ir.action.act_window" id="act_hello_admin_form">
            <field name="name">Hello Admin</field>
            <field name="res_model">hello.hello</field>
            <field name="view_type">form</field>
        </record>
        <record model="ir.action.act_window.view" id="act_hello_admin_form_view1">
            <field name="sequence" eval="10"/>
            <field name="view" ref="hello_view_tree"/>
            <field name="act_window" ref="act_hello_admin_form"/>
        </record>
        <record model="ir.action.act_window.view" id="act_hello_admin_form_view2">
            <field name="sequence" eval="20"/>
            <field name="view" ref="hello_view_form"/>
            <field name="act_window" ref="act_hello_admin_form"/>
        </record>

This indicates that we will put in the admin the same views that we created for the user, if you desire you could change to other views, or reports.

To put one link in the admin menu just set parent attribute of the menuitem to ir.menu_administration, this can be done by adding this line:

        <menuitem parent="ir.menu_administration" action="act_hello_admin_form"
            id="menu_admin_hello_form"/>

Step 4: Creating a report

Add the following lines to the file 'hello.xml' into the /data tag:

        <record model="ir.action.report" id="report_hello">
            <field name="name">Hello</field>
            <field name="model">hello.hello</field>
            <field name="report_name">hello.helloworld</field>
            <field name="report">helloWorld/hello.odt</field>
        </record>
        <record model="ir.action.keyword" id="report_hello_world">
            <field name="keyword">form_print</field>
            <field name="model">hello.hello,0</field>
            <field name="action" ref="report_hello"/>
        </record>

Now create the file hello.odt inside the directory .../trytond/trytond/modules/helloWorld.

In this file add the lines:

<for each="hello in objects">
<hello.name>
</for>

Each line, must be added using OpenOffice writer, in the menu: Insert->Fields->Others..., in the tag 'Functions', with the type 'Placeholder' and the format 'Text', and fill the field Placeholder with each line, vg: 'for each="hello in objects"'.

Other way to do this is coping the file '.../trytond/trytond/modules/party/label.odt', and editing it by pressing the right button in the mouse, and copping and pasting to add new lines.


Comment by ggdasa, Sep 04, 2009

Hi!

Thanks a lot for making this page much more clear than before.

thumb up for extending/improving the docs.

Sincerely, Gour


Sign in to add a comment
Hosted by Google Code