~vijaykumar

[ Home | Feed | Twitter | Vector Art | Ascii Art | Tutorials ]

My First OpenERP Module

Wed, 06 May 2009

This tutorial shows how to create a very simple OpenERP module. A module to add Indian Rupees to the currency list will be taken as an example.

All OpenERP modules are Python packages. So each module should contain a module directory with atleast an empty __init__.py. We create a directory called rupees under the addons directory. And we create an empty __init__.py file.

openerp-server
   +- addons
      +- rupees
         +- __init__.py

All OpenERP modules should additionally have a __terp__.py python file, that contains meta information about the module. The meta information is provided as python dictionary.

{
    'name': 'Rupees Support',
    'version': '1.0',
    'category': 'Generic Modules',
    'description': """Add Rupees to the currency list.""",
    'author': 'Zilogic Systems',
    'website': 'http://www.zilogic.com',
    'depends': ['account'],
    'init_xml': [],
    'update_xml': ['data.xml'],
    'demo_xml': [],
    'installable': True,
    'active': False,
}

Most of the keys in the dictionary are self descriptive. The not so obvious ones are described below.

depends

specifies a list of OpenERP modules this module depends upon.

init_xml

yet to find out what this is for. Most modules keep provide an empty list.

update_xml

specifies a list of XML files to be processed when the module is installed(?). Here we specify data.xml that contains instructions to add Indian Rupees to the list of currencies.

demo_xml

specifies a list of XML files to be processed when the demo data is requested during module installation.

installable

specifies whether the module is available for installation.

active

specifies whether the module is installed by default. This is only set for the base module.

XML files in OpenERP are used for the following purposes:

  1. Adding records to any table in the database.

  2. Creating menu entries.

  3. Adding reports.

  4. Adding wizards.

In this example, the XML file data.xml will be used for adding records to the currency table.

<?xml version="1.0"?>
<openerp>
    <data>
        <record model="res.currency" id="rupees-id">
            <field name="name">INR</field>
            <field name="code">INR</field>
            <field name="rounding">0.01</field>
            <field name="accuracy">4</field>
        </record>
        <record id="rateINR" model="res.currency.rate">
            <field name="rate">66.00</field>
            <field name="currency_id" ref="rupees-id"/>
            <field eval="time.strftime('%Y-01-01')" name="name"/>
        </record>
    </data>
</openerp>

The currency is represented in two tables res.currency and res.currency.rate. The res.currency table contains general information about the currency that does not change. The res.currency.rate table contains the rate of a currency on (or rate of currency since?) a particular date. So a single currency can have multiple rates one on each date. The fields of the res.currency.rate table is shown below.

res.currency.rate
Field Description
currency_id Currency for which this rate corresponds
rate Equivalent of 1 Euro in this currency
name The date for which this rate corresponds

All tags are enclosed within the top level tags <openerp> and <data>.

The <record> tag is used to add entries to tables. The model attribute is used to specify the table in which entries are to be added. The id attribute is used to refer to the record later in the XML file.

The <field> tag is used to specify the value for each of the fields in an entry. The name attribute specifies the field, and the content of the tag specifies the value. In the following example, the code field will be given a value INR.

<field name="code">INR</field>

The first <record> tag adds a currency to the res.currency table. The second <record> tags adds a currency rate to the res.currency.rate table. Some points to note:

  1. The value for the currency_id field is specified by refering to the earlier record using the ref attribute.

  2. The value for the name field is dynamically calculated using the Python expression specified by the eval attribute.

Permalink | Add Comment | Share: Twitter, Facebook, Buzz, ... | Tags: python

blog comments powered by Disqus

Powered by Python | Made with PyBlosxom | Valid XHTML 1.1 | Best Viewed With Any Browser | Icon Credits | CC-BY-SA