Tuesday, December 28, 2010

Python files in openERP

All the necessary files needed for creating the module and connecting with the  view lies under the base module. Base module contain a folder called osv in which all the functions and classes needed for creating a model is defined here. So first of all Import fields and osv from osv.

To define a new object, you have to define a new Python class then instantiate it. This class must inherit from the osv class in the osv module.The first line of the object definition will always be of the form:
An object is defined by declaring some fields with predefined names in the class. Two of them are required (_name and _columns), the rest are optional. The predefined fields are:
  • _auto
  • _columns (required)
  • _constraints
  • _sql_constraints
  • _defaults
  • _inherit
  • _inherits
  • _log_access
  • _name (required)
  • _order
  • _rec_name
  • _sequence
  • _sql
  • _table
In the database, tables will be created with the table name given in the _name field (all the dot"." will be replaced by underscore"_" in table creation)

Fields for the database table is specified inside the "_columns". Columns is an associative array(dictionary) in which we specify the field name as the key and field type as the value.

Types of fields
Basic types
boolean:
A boolean (true, false).
Syntax:
fields.boolean('Field Name' [, Optional Parameters]),
 
integer:
An integer.
Syntax:
fields.integer('Field Name' [, Optional Parameters]),
 
float:
A floating point number.
Syntax:
fields.float('Field Name' [, Optional Parameters]),
char:
A string of limited length. The required size parameter determines its size.
Syntax:
fields.char(
        'Field Name',
        size=n [,
        Optional Parameters]), # where ''n'' is an integer.
text:
A text field with no limit in length.
Syntax:
fields.text('Field Name' [, Optional Parameters]),
 
date:
A date.
Syntax:
fields.date('Field Name' [, Optional Parameters]),
 
datetime:
Allows to store a date and the time of day in the same field.
Syntax:
fields.datetime('Field Name' [, Optional Parameters]),
 
binary:
A binary chain. We can import(upload) our files using binary.
selection:
A field which allows the user to make a selection between various predefined values.
Syntax:
fields.selection((('n','Unconfirmed'), ('c','Confirmed')),
                   'Field Name' [, Optional Parameters]),
 
Relational fields

  • one2many and many2one
    • An example is relationship between employee and department. A department may have so many employees and employees will belong to only one department.
    • When defining a many2one relationship in open erp, at first there must have a one2many relationship. ie the constructor which have the one2many relation must be called before calling the constructor having many2one relation.

    • {"employee":fields.one2many("employee.class","department_id","Employees")}

In class department we define the field as dictionary values, “employee” as the key, “fields.one2many()” as the value with 3parameters. First parameter is the other class name(here employee.class ), then the key name of the other class which has the many2one as its value(here employee_id), then the field name.
    • {"department_id":fields.many2one("department.class","Department"),}

            In class employee we define the field as dictionary values, “department_id” as the key, “field.many2one()” as the value with 2parameters. First parameter is the other class name(here is the department.class) then the field name.

  • many2many
    • Example is the relationship between user and group. Users may belong to so many groups and a group may have so many users.
    • For showing a many2many relationship, we have to create a new relationship table in the database with fields user_id and group_id.
    • If we want a many2many relation between two classes say class abc and class xyz then we have to first define both classes. Now add the many2many relationship in the class which secondly created( here class xyz). Now define a new class which inherit the properties of the class abc. Define the many2many relationship here in the inherited class.
  
    • { 'user_ids': fields.many2many('group.class','user_group_rel', 'user_id','group_id','Users')}
    • The parameters are
      • other class name
      • relationship table name
      • my_relation_id
      • other_relation_id
      • field name

OpenERP(open Enterprise Resourse Planning)

        OpenERP is an open source comprehensive suite of business applications including Sales, CRM(Customer Relationship management), Project management, Warehouse management, Manufacturing, Accounting and Human Resources. OpenERP has separate client and server components. XML-RPC interfaces are available. It is based on a strong MVC architecture, flexible workflows, a dynamic GUI, an XML-RPC interface, and customizable reporting system with convenient OpenOffice.org integration.

An OpenERP system has three main components :
  1. PostgreSQL dataserver which has all the databases
  2. OpenERP application server that has all of the enterprise logic
  3. web server, a distinct application called the Open Object client-web, which allows you to connect to OpenERP from any web browser. This is not needed if the user connect through a GTK client.
         The server part is written in Python. Business functionality is organized into "modules". A module is a folder with a pre-defined structure containing Python code and XML files. A module defines data structure, forms, reports, menus, procedures, workflows etc. The client is thin as it contains no business logic. 

Working 
        I am using eclipse IDE for editing and using openERP version 5.0.14. We can download the server and client package from www.openerp.com , unzip the files and start the server. To start the server first go the openerp-server folder, then to the bin folder. There you can find the openerp-server.py 

     will run the server. Same process for running the client. We can find a file named openerp-client.py inside the bin folder present in the client. Provide the "-v" option to get the complete details of the module fields when mouse pointer pointed to it. Then a client interface with a pop-up window for login. If the database is not present or server is not yet started a error message will be shown. In openERP for the database creation postgresql is used. If there is no error then interface with all the functionalies will be shown.
This is the login window.
        Open eclipse and select the work space. The server code must be inside the workspace folder found in the home folder. After opening the eclipse go to the folder /bin/addons inside our openerp-server-5.x.x. The addons folder contains all the modules created by the openerp team. We have to add our module in this folder. Create a new directory and inside this directory we have to create all supporting files needed for our module.

Basic files needed for a module are
  1. __init__.py  -->In this file we have to specify the all the files needed for our module. That is we have to import all the needed *.py file names and folder names inside this init file.
  2. __terp__.py  -->This is the configuration file.Here we have to specify all the files and depending modules,demo files, author name, version etc.
  3. *.py file . --> In the .py file we define our model, and controller.
  4. *.xml file.--->The view part is defined in .xml files.
    ____________________________________________________________________________