How to create an OpenERP V 6.1 module


Every OpenERP developer or integrator knows how can be annoying to create a new module or to add new features (like views,actions, etc) to it. There are several approaches for bootstrapping it.

The first, the most common I think, is to copy & paste an existing module (or an empty one), rename it, and replace all the bits to fit your needs. The second is to use an IDE like Eclipse or TextMate or Sublime-text and create or load all the predefined snippets you need.

This requires a little time but still, can be avoided. Also, could lead to errors, typos, etc, and this must be avoided.

Starting from my Plone background where tools like ZopeSkel and Paste (coupled with Buildout ) are  today mainstream solutions for bootstrapping our  development environments and creating new packages, I tried to get rid of this c&p approach.

I released a package called openerp_bootstrap aimed at speeding up the creation of new modules and new features. For the time being there are only two templates ready to be used:

  • openerp_newmodule that lets you create a basic generic module
  • openerp_theme that lets you create a basic custom web theme

Let’s see how to use them. First of all install openerp_bootstrap using python setuptools or distribute:

sudo easy_install openerp_bootstrap
sudo pip install openerp_bootstrap

After that, a new executable called ‘paster’ will be available. You can list the available templates by running:

# paster create --list-templates
Available templates:
  basic_package:      A basic setuptools-enabled package
  openerp_newmodule:  Template for creating a basic openerp package skeleton
  openerp_theme:      Template for creating a basic openerp theme skeleton
  paste_deploy:       A web application deployed through paste.deploy

As you can see, there are two openerp_* templates. You can use them by calling ‘paster create -t $TEMPLATE_NAME’. When you call it you will be prompted with step-by-step questions that will allow you to customize your module while creating it. Let’s see an example:

# paster create -t openerp_newmodule
Selected and implied templates:
openerp-bootstrap#openerp_newmodule Template for creating a basic openerp package skeleton

Enter project name: my_new_module
Variables:
egg: my_new_module
package: my_new_module
project: my_new_module
Enter module_name (Module name (like "Project Issue")) ['My Module']: My new shiny module
Enter description (One-line description of the module) ['']: A module that does this and that
Enter version (Version) ['1.0']:
Enter author (Author name) ['']: John Doe
Enter author_email (Author email) ['']: john@doe.com
Enter category (Category) ['']:
Enter website (Website) ['']: www.johndoe.com
Enter depends (Dependencies [space-separated module names]) ['']: account
Enter is_web (Is web addon? [yes/no]) ['no']:
Creating template openerp_newmodule
Creating directory ./my_new_module
Copying __init__.py to ./my_new_module/__init__.py
Copying __openerp__.py_tmpl to ./my_new_module/__openerp__.py

And, here we go! We’ll find all the data we need into our module’s manifest (__openerp__.py):

# cat ./my_new_module/__openerp__.py
# -*- coding: utf-8 -*-
{
    'name': 'My new shiny module',
    'version': '1.0',
    'category': '',
    'description': """A module that does this and that""",
    'author': 'John Doe (john@doe.com)',
    'website': 'www.johndoe.com',
    'license': 'AGPL-3',
    'depends': ['account'],
    'init_xml': [],
    'update_xml': [],
    'demo_xml': [],
    'active': False,
    'installable': True,
}

Let’s create a theme now:

# bin/paster create -t openerp_theme
Selected and implied templates:
  openerp-bootstrap#openerp_theme  Template for creating a basic openerp theme skeleton

Enter project name: my_custom_theme
Variables:
  egg:      my_custom_theme
  package:  my_custom_theme
  project:  my_custom_theme
Enter module_name (Module name (like "My Theme")) ['My Theme']: My brand new theme
Enter description (One-line description of the module) ['']: Customer X project theme
Enter version (Version) ['1.0']:
Enter author (Author name) ['']: Mr Foo
Enter author_email (Author email) ['']: mr@foo.com
Enter category (Category) ['']:
Enter website (Website) ['']: www.foo.com
Enter depends (Dependencies [space-separated module names]) ['']: project
Enter has_css (Needs CSS? [yes/no]) ['yes']:
Enter has_js (Needs Javascript? [yes/no]) ['yes']:
Enter has_xml (Needs QWeb XML? [yes/no]) ['no']:
Creating template openerp_theme
Creating directory ./my_custom_theme
  Copying __init__.py to ./my_custom_theme/__init__.py
  Copying __openerp__.py_tmpl to ./my_custom_theme/__openerp__.py
  Recursing into static
    Creating ./my_custom_theme/static/
    Recursing into css
      Creating ./my_custom_theme/static/css/
      Copying +normalized_name+.css_tmpl to ./my_custom_theme/static/css/my_custom_theme.css
    Recursing into js
      Creating ./my_custom_theme/static/js/
      Copying +normalized_name+.js_tmpl to ./my_custom_theme/static/js/my_custom_theme.js
    Recursing into xml
      Creating ./my_custom_theme/static/xml/
      Copying +normalized_name+.xml_tmpl to ./my_custom_theme/static/xml/my_custom_theme.xml

This will create a web module with all the static resources you need ready to be customized.

# cat ./my_custom_theme/__openerp__.py
# -*- coding: utf-8 -*-

{
    'name': 'My brand new theme',
    'version': '1.0',
    'category': '',
    'description': """Customer X project theme""",
    'author': 'Mr Foo (mr@foo.com)',
    'website': 'www.foo.com',
    'license': 'AGPL-3',
    'depends': ['project', 'web'],
    'init_xml': [],
    'update_xml': [],
    'demo_xml': [],
    'active': False,
    'installable': True,
    'web':True,
    'css': [
        'static/css/my_custom_theme.css',
    ],
    'js': [
        'static/js/my_custom_theme.js',
    ],
}

# ls my_custom_theme/static/*
my_custom_theme/static/css:
my_custom_theme.css

my_custom_theme/static/js:
my_custom_theme.js

Was that difficult? :) And now the most important – but still missing – part: add modules’ contents like views, classes, fields, etc. Using PasteScript ‘local commands’ we’ll be able to add sub-templates for adding new objects and customize them on the fly, doing something like:

# cd my_new_module
# paster addcontent newmodel project.custom.model
[...]
# paster addcontent field project_custom_model
[...]

Wouldn’t be great? That’s all for the moment.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s