You might want to build a module for OpenERP/Odoo for solving a specific problem or for sheer curiosity to build something. At Confianz we thought of helping you by coming up with a simple yet effective tutorial.
OpenERP Logger for Connectors
Development Strategy
1) Database Design
- Only one new table is required for this module.
Table name | logger |
Columns : | |
id | integer, primary key |
Log_date | datetime |
log_uid | integer , foreign key to res_users table |
res_id | integer (holds the resource id where error is reported) |
model_name | character (16) (holds the name of the model ) |
connector | character (32) ( this will hold the name of the connector is the shop) |
module | character( 32) (holds the name of the module reported the error) |
message | text (actual error messages) |
‘type’ | enum (import, export, delete) |
‘state’ | enum(info, error, warning) |
1) OpenERP module development
- The module is to be developed with a folder name as ‘ logger’.
- The directory structure will be as follows:
Security
ir.model.access.csv
Static
Src
img
icon.png
logger.py
Logger_view.xml
__ini
t__.py
__openerp__.py
- Security directory is used to specify the access controls for the class used with in the module.
- Static will be used to insert the module thumbnail icon, which will be displayed on the OpenERP module Kanban and form view.
- As an icon for this module, use the following PNG file sized 32*32
- Place this image file inside Static/Src/img folder as ‘png’ so that OpenERP will automatically pick this as the thumbnail
- For this module, we will not use any Wizards (popups). Hence, the Wizard directory is not required.
1) Files and Usage
i. logger.py
- This is the main Python file where the class the all the python modules will be defined.
- STEP 1: Import all the packages needed, by using the ‘import’
In this module, we will be using the following packages:
From openerp.osv import osv, fields
// ORM class used as the base class
import logging
// logging is a class used in openerp for terminal and file logging
import time
// time is a python class for operation on time
- STEP 2: Once all the packages are imported, declare the class as
class logger(osv.osv):
_name = ‘logger’
_description = ‘Log details’
-
The first line specifies the name of the class ‘logger’. This class inherits from the base class osv.osv where the ORM functions ( create, write, unlink, etc.) are defined.
-
_name is an attribute of ORM class which will be used as a reference to a class within OpenERP
-
In the XML files, this reference will be used to specify this class. In OpenERP, this is called a 'model' (i.e. an ORM class )
-
_ description is used to give a small deception to that model by the developer
- STEP 3: Define the columns within the attribute _columns, which is a dictionary. OpenERP will pick the elements inside the dictionary and create them as columns inside the database table automatically.
_columns = {
'log_date': fields.datetime('Time', readonly=True),
'log_uid': fields.many2one('res.users', "User"),
'res_id': fields.integer('Ressource id', readonly=True),
'connector':fields.char('Connector', size=156, readonly=True),
'model_name': fields.char('Model name', size=64, readonly=True),
'message': fields.text('Message', readonly=True),
'type':fields.selection([('import','Import'),('export','Export'),('delete','Delete'),],'Action', readonly=True),
'state':fields.selection([('info','Info'),('warning','Warning'),('error','Error')],'State', readonly=True),
'module':fields.char('Module', size=64, readonly=True),
}
- ‘fields’ is a class in ORM which defines the type and other attributes as a readonly, size etc. of the columns .Here we can keep all fields as readonly as log should be created by the system and not manually by the user.
- Now we can also specify the log_date as ‘Time’ by default by using the _default attribute in the ORM
_defaults = {
‘log_date’: lambda *a: time.strftime(‘%Y-%m-%d %H:%M:%S’)
}
- Next , we need the function which the connectors can use to register the log. This function will be named as: register_log
Method : register_log
Argument s :
self -- self (this) object
cr -- Database cursor object
uid - id of the logged in user
module -- character , name of the module from where the log is registered
connector_name -- character , name of the connector(sore)
model --- character , name of the model where error is reported
res_id -- int, resource where where is reported
syn_type -- character , operation type which cause the error ,import, export or delete
message -- text
state --- character
return : True
- Once we are done with the function, we can initialise the class by using:logger() This will end all the coding we need in the main py file.
i. logger_view.xml
- This is the view file for OpenERP which defines what and how data will be visible to the user in front end
- STEP1: Define the views. We require 3 types of view for this module which are
- Tree : To list the logs
In the tree view we require the following fields to give enough information to the user:
<field name=”log_date”/>
<field name=”module”/>
<field name=”connector”/>
<field name=”model_name”/>
<field name=”res_id”/>
<field name=”type”/>
<field name=”state”/>
- Form: To display the details of a particular logger
In form view we need all the fields to be available
- Search : To search for logs in OpenERP
In Search view, users should be able to search with date, type, state, store, module, and message. User should also be able to group the log with model name and resource where error is reported
A sample search view will be as follows:
<record id="log_search_view" model="ir.ui.view">
<field name="name">Log - Search</field>
<field name="model">log.details</field>
<field name="arch" type="xml">
<search string="Logs">
<field name="log_date"/>
<field name="type"/>
<field name="state"/>
<field name="module"/>
<field name="connector"/>
<field name="model_name"/>
<field name="message"/>
<newline/>
<group expand="0" string="Extended..." groups="base.group_extended">
<field name="res_id"/>
</group>
<newline/>
<group expand="0" string="Group By..." groups="base.group_extended">
<filter string="Model" domain="[]" context="{'group_by':'model_name'}"/>
</group>
</search>
</field>
</record>
- STEP 2: Once the views are defined, the next step is to define the action which will instruct OpenERP as to what view is used first:
-
<record id="act_log_details" model="ir.actions.act_window">
<field name="name">Logs</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">log.details</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
- In the action, res_model refers to the class reference used in python file
- STEP 3: Defining the Menus where user can see the log. Place that log inside Settings >Technical menu
<menuitem id=”menu_log_details” parent=”base.menu_custom” name=”log Details” sequence=”50″/>
<menuitem id=”menu_log_details_sub” parent=”menu_log_details” name=”log Details” action=”act_log_details” sequence=”5″/>
Now, that we are done with the view file as well.
-
i. __init__py
- All the python files used with the module is imported into the python init
- All the class inside logger.py file is initialized by using:
import logger
ii. __openerp__.py
- This file provides all information about this module to OpenERP and also specifies the cam files used
-
# -*- coding: utf-8 -*-
{
'name': ' Logger',
'version': '1.0',
'category': 'tools',
'sequence': 1,
'summary': "OpenERP Logger",
'description': """
This is a package developed for Logging error and warning from connector modules developed for openerp \n
""",
'author': 'Confianz Global',
'website': 'http://confianzit.com',
'images': [],
'data': [
'log.xml',
'security/ir.model.access.csv',
],
'depends': [base'],
'installable': True,
'auto_install': False,
'application': True,
}
- Once all these files are packed, this can be moved to OpenERP’s Addons directory and will be available in the modules list to install.
- – Happy Coding!
- Choosing the right ERP for your business can be tricky, but we’re here to help! Confianz has experts ready to help optimize your business. Confianz Global Inc., the best Odoo ERP development company in US provides Odoo services such as Odoo customization, implementation, Support & Maintenance Services to fulfill all your business needs.