Creating a new module

Our module will be a very simple application to keep to-do tasks. These tasks will have a single text field, for the description, and a checkbox to mark them as complete. We will also have a button to clean the to-do list from the old completed tasks.

These are very simple specifications, but throughout the book we will gradually add new features to it, to make it more interesting for the users.

Enough talk, let's start coding and create our new module.

Following the instructions in Chapter 1, Getting Started with Odoo Development, we should have the Odoo server at /odoo-dev/odoo/. To keep things tidy, we will create a new directory alongside it to host our custom modules:

$ mkdir ~/odoo-dev/custom-addons

An Odoo module is a directory containing an __openerp__.py descriptor file. This is still a legacy from when Odoo was named OpenERP, and in the future is expected to become __odoo__.py.

It also needs to be Python importable, so it must also have an __init__.py file.

The module's directory name will be its technical name. We will use todo_app for it. The technical name must be a valid Python identifier: it should begin with a letter and can only contain letters, numbers, and the underscore character. The following commands create the module directory and create an empty __init__.py file in it:

$ mkdir ~/odoo-dev/custom-addons/todo_app
$ touch ~/odoo-dev/custom-addons/todo_app/__init__.py

Next we need to create the descriptor file. It should contain only a Python dictionary with about a dozen possible attributes, of which only the name attribute is required. A longer description attribute and the author also have some visibility and are advised.

We should now add an __openerp__.py file alongside the __init__.py file with the following content:

{
    'name': 'To-Do Application',
    'description': 'Manage your personal Tasks with this module.',
    'author': 'Daniel Reis',
    'depends': ['mail'],
    'application': True,
}

The depends attribute can have a list of other modules required. Odoo will have them automatically installed when this module is installed. It's not a mandatory attribute, but it's advised to always have it. If no particular dependencies are needed, we should depend on the special base module. You should be careful to ensure all dependencies are explicitly set here, otherwise the module may fail to install in a clean database (due to missing dependencies) or have loading errors, if the other needed modules are loaded afterwards. For our application, we want to depend on the mail module because that is the module that adds the Messaging top menu, and we will want to include our new menu options there.

To be concise, we chose to use very few descriptor keys, but in a real word scenario it is recommended to also use these additional keys, since they are relevant for the Odoo app store:

  • summary is displayed as a subtitle for the module.
  • version, by default, is 1.0. Should follow semantic versioning rules (see semver.org for details).
  • license identifier, by default is AGPL-3.
  • website is a URL to find more information about the module. This can help people to find more documentation or the issue tracker to file bugs and suggestions.
  • category is the functional category of the module, which defaults to Uncategorized. The list of existing categories can be found in the security Groups form (Settings | User | Groups menu), in the Application field drop-down list.

These other descriptor keys are also available:

  • installable is by default True, but can be set to False to disable a module.
  • auto_install if this is set to True this module is automatically installed if all its dependencies are already installed. It is used for glue modules.

Since Odoo 8.0, instead of the description key we can use a README.rst or README.md file in the module's top directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset