How to create an OpenERP module: the easy way

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.

The development package it’s on github. Feel free to fork and contribute back or to submit issues.

NOTE: the package is still in alpha state so it could be a little bit buggy ;)

Facebook Twitter Linkedin Digg Delicious Reddit Stumbleupon Tumblr Posterous Email Snailmail

Written by on Tuesday, April 3rd, 2012

  • Anonymous

    very nice, thanks for this, I love OpenERP as a platform and this looks like a handy shortcut for new modules.

    • http://www.domsense.com/ Simone Orsi

      Good! Just remember: any feedback/help is much appreciated :)

  • http://twitter.com/TimothySolomon Timothy Solomon

    Awesome! going to test it out soon. thanks for sharing!
    (PS where can I find more info on themeing openerp? how do you call the theme? Is it the same as ?kitten=1)

    • http://www.domsense.com/ Simone Orsi

      Actually I hadn’t look at this :) The thing is that if you don’t override the basic CSS you won’t get the theme applied for anonymous if your module is installed into a specific db

      • http://www.domsense.com/ Simone Orsi

        just had a look at it and ‘kitten’ is working because is hardcoded :S take a look at these:

        web/6.1/addons/web/static/src/js/chrome.js:        if (jQuery.param !=
        undefined && jQuery.deparam(jQuery.param.querystring()).kitten
        != undefined) {
        web/6.1/addons/web/static/src/js/chrome.js:            this.$element.addClass(“kitten-mode-activated”);

      • http://twitter.com/TimothySolomon Timothy Solomon

        ok, thanks. Next question.. Got it installed in windows using easy_install, but how do I use paster? does it create and exe that I can just add to the PATH?

        • http://www.domsense.com/ Simone Orsi

          just call ‘paster’. if you are not using a virtualenv you should find it as a global command.

  • nycasual

    That was a great post Simahawk.  We are looking for a person or company to help us with OpenERP customization and creation of new modules, and someone with your knowledge could be very helpful. If you or someone you know is interested in a freelance OpenERP customization project, please email me at jeff.beach@newyorkcasual.com 

    • http://www.domsense.com/ Simone Orsi

      hi, feel free to contact us at info [at] domsense [dot] com

  • Neil Juan

    Learned a lot from here. Simplified.

  • http://lcaballero.wordpress.com/ Leonardo J. Caballero G.

    Simone Orsi Thanks, that was a great post! for help you to spread this methodology for create module for OpenERP, I wrote a article (in Spanish) check out http://goo.gl/1V5vi

  • Carlos Paz

    Can you make a tutorial how to customize predefined modules, like… the POS, for example ?

  • Amr Shahin

    Hey and thanks,

    i tried your tool on v7 and it doesn’t see the module ( doesn’t see a module i created manually either ). Any idea about that ?