OpenERP: Display Pictures in a Module’s Description

OpenERP: Display Pictures in a Module’s Description

1 Insert Pictures

For each OpenERP module, there is a file named __openerp__.py which is stored in the module’s folder. __openerp__.py uses a Python dict to represent various information of the module, and the value of key description is the module’s description. You could place pictures in the description by inserting lines such as:
.. image:: /module-name/path/to/your/image_file_name

For example, I have a module named wenshan-test, and in the module’s folder there is an image file static/src/img/roshan.jpg, and the content of wenshan-test/__openerp__.py is (see line #15):

 1:  # -*- encoding: utf-8 -*-
 2:  {
 3:      "name": "Wenshan Test",
 4:      "version": "1.0",
 5:      "depends": [
 6:      ],
 7:      "author": "任文山",
 8:      "category": "任",
 9:      "description": """
10:  Wenshan Test
11:  ============
12:  
13:  * Hello World
14:  
15:  .. image:: /wenshan-test/static/src/img/roshan.jpg
16:  
17:  * Python Sucks
18:  
19:      """,
20:      "update_xml": [
21:      ],
22:      "demo": [],
23:      "test": [],
24:      "installable": True,
25:      "active": False,
26:  }

The module’s information page in the OpenERP system will look like this:
./img/OpenERP-insert-pic-to-module-description.png

2 Code Analysis

This section records my process of solving this problem, it might be useful for those who are interested the OpenERP source code. Just like many other FLOSS (Free/Libre/Open-Source Software), OpenERP is not very well documented, you’ll have to dig into the code from time to time.

2.1 The Module for Managing Modules

The module for managing OpenERP modules is ir.module.module, which is located in openobject-server/openerp/addons/base/module.

Open the openobject-server/openerp/addons/base/module/module_view.xml and find the form view of ir.module.module. According to line #11, we know that a module’s description is retrieved from its description_html field:

 1:  <!-- Modules -->
 2:  <record id="module_form" model="ir.ui.view">
 3:    <field name="name">ir.module.module.form</field>
 4:    <field name="model">ir.module.module</field>
 5:    <field name="arch" type="xml">
 6:      <form create="0" edit="0" string="Module" version="7.0">
 7:        <sheet>
 8:          ...
 9:          <notebook>
10:            <page string="Description">
11:              <field name="description_html"/>
12:            </page>
13:            ...
14:          </notebook>
15:        </sheet>
16:      </form>
17:    </field>
18:  </record>

Then open openobject-server/openerp/addons/base/module/module.py and find the definition of ir.module.module. There are two fields that are related: description and description_html:

1:  _columns = {
2:      ...
3:      'description': fields.text("Description", readonly=True, translate=True),
4:      'description_html': fields.function(_get_desc, string='Description HTML', type='html', method=True, readonly=True),
5:      ...
6:  }

description is parsed from the value of key description in the __openerp__.py‘s dict, and description_html is a functional field that calls _get_desc() method to parse the description field and generate a HTML string, which will be later on rendered by the browser and seen by us.

Jump to the definition of _get_desc() (Press M-. in Emacs):

1:  def _get_desc(self, cr, uid, ids, field_name=None, arg=None, context=None):
2:      res = dict.fromkeys(ids, '')
3:      for module in self.browse(cr, uid, ids, context=context):
4:          overrides = dict(embed_stylesheet=False, doctitle_xform=False, output_encoding='unicode')
5:          output = publish_string(source=module.description, settings_overrides=overrides, writer=MyWriter())
6:          res[module.id] = output
7:      return res

Line #5 calls the publish_string() method, which is imported from docutils at the beginning of the file:

from docutils.core import publish_string

publish_string() takes the value of the description field as an input and outputs the result HTML string.

With the help of Google, I found that docutils makes use of the reStructuredText format. And from the documentation of reStructuredText, we could easily figure out how to insert pictures.

Actually, we could use other reStructuredText syntax as well, which is quite handy.

Happy Hacking!


Date: 2013-06-30

Author: 任文山 (Ren Wenshan)

Org version 7.9.3d with Emacs version 24

Validate XHTML 1.0

One thought on “OpenERP: Display Pictures in a Module’s Description

  1. Pingback: Starred Links – June 2013 | 肉山博客 (Wenshan's Blog)