Tuesday, 27 September 2016

Understanding of __openerp__.py file - Odoo

Greetings Everyone,

In my previous post i provide overview module structure of odoo(OpenERP), in that i had provide basic information about __openerp__.py file. Here i will explain __openerp__.py file in detail. like what this file contain and why all contain provided in this file.

__openerp__.py : This is the manifest file of the module. It contains various information about your module i.e. Title, Description, Summary etc. You can put some light on the person installing it as why you are building this module, what is the purpose of this module and what your module really does.

Now let see what __openerp__.py file contain:

  1.  name : Name of your module. (required).
  2.  version : A current version of your module. if new module then version must be 1.0 after some updation change your version. for example as android is changing its version.
  3. complexity : Used to provide complexity level of you module.
  4. description : A long description of your module. this is most important part of to describe. With this you can guide the user who will gonna use this module. In this you can explain about the functionality of your module.
  5. category : Here you need to mention in which section your module belongs. if you open some of module of odoo's addons, each module reside in some category like Sales, Purchase, Inventory etc.
  6. author : The author of module, name of person/company who is authorized.
  7. website : Your website or company website. URL of the website of the module.
  8. license : The license of the module (default: LGPL-3).
  9. contributors : Here you can specify the name of person who have contribute his/her time, efforts, mind, money etc. to produce module.
  10. depends : List of modules on which this module depends beside base. for e.g if you have inherit sale.order then you have to specify sale module as a depends in your module.
  11. sequence : Sequence is the number position of your module while module is loaded in odoo module list.
  12. data : List of files to load when the module is installed or updated. It can contain the following files,
    1. The view files [.xml file]
    2. The data files [.xml] (for some dummy data)
    3. The security file [.xml] #this file contains some groups and record rules
    4. The CSV files. #this file contains access control over user or groups
  13. qweb : For web interface, you can place your all template file here.
  14. js : Specify all your js file here.
  15. test : Here, you can put your test cases file.
  16. images : Specify images used in your module.
  17. installable : True or False. Determines whether the module is installable or not. 
  18. auto_install : True or False (default: False). If set to ``True``, the module is a link module. It will be installed as soon as all its dependencies are installed.
  19. application : True
  20. price : price of your module.
  21. currency : 'INR'

Click to enlarge image.

To learn about the Module Structure of Odoo, Click Here

Saturday, 20 August 2016

Odoo Module Structure

Hello Everyone,

Today i am going to guide you about the modular structure of odoo.
for example-what a module consists of, its structure and how it is divided.

Take a look at odoo modules and you will find a specific format/structure followed to create a module. Each file and folder has a unique purpose and you need to follow the file/folder structure to build your custom module.

[ Odoo Module Directory Structure ]

Now let's get the detail information about each file/folder:

  • __init__.py : This file is used to initialize your module and its mandatory in each and every module you create. It contains import statements linking to folders and files in the module.
          for example : 1- Import all files & directories containing python code.
                                2- import models, wizard, report.
  • __openerp__.py : This is the manifest file of the module. It contains various information about your module i.e. Title, Description, Summary etc. You can put some light on the person installing it as why you are building this module, what is the purpose of this module and what your module really does.
  • models : This folder contains all your python (.py) files. All the business logic files, having extension .py
          for example : account.py, sale.py, etc.
  • views : This folder contains all your view (.xml) files. The view file can contain a form, tree, search views, action etc.
  • static : Since Odoo 8, website feature is introduced. This folder is basically used to store website related data. It has a subfolder called src and inside src, there are subfolders like,
    • src : 
      • js – contains .js files
      • img – contains images
      • css – stores .css files for designing
      • xml – used for qweb templates
    • description :  It contains an index.html file which is used to give a graphical introduction to your module. Moreover, it contains icon.png which is set as the logo of the module.
  • data : Contains data files (.xml files). Sometimes data needs to be inserted along with the module every time the module is installed. You can specify such data in a data file and place it inside this folder. For e.g. Country, States, Unit of Measures
  • demo : Contains demo data files (.xml files). This data is useful when you want the user to see a glimpse about how the entries are made in your module. For e.g. Customer invoice, Sale Orders, Quotations. This data is only installed if ‘Load Demo Data’ option is enabled at the time of database creation.
  • security : This folder is used to give user roles and permissions. The groups/record rules are  defined in an xml file and put in this folder. Moreover, it has ir.model.access.csv file which provides model level security i.e. read, write, create and unlink(delete) permission.
  • controller : Basically what a controller does is it gets a request from the client and inquires the server. It gets the work done at the server side and returns back to the client. This folder contains python (.py) files.
  • wizard : This folder contains transient models and their views (.py & .xml files). The data is stored temporarily in this model/table and are vacuumed/deleted at regular interval.
  • test : Contains .py files to make test cases.
  • report : This folder consists of report views (.xml) and parser files (.py).
  • workflow : Used to specify a complete business process.
  • i18n : means Internationalization, comprises of .po files which are used for translating the text inside your module into several languages. The .po files are named on the basis of the language code. For e.g. – en.po

Saturday, 6 August 2016

Odoo - Basic Introduction

  • Odoo (formerly known as OpenERP and before that, TinyERP) is released under GNU – General Public License and completely Open source ERP software. The software works on Python Language and PostgreSQL database. Being Open source Odoo is completely cost effective and perfect software for every business
  • Odoo was started in the year 2005 by Fabien Pinckaers (CEO) and have a strong community & Partners who contribute day to day towards enrichment of Odoo software.
  • Odoo system is completely Open Source ERP solution that offers you with license free usage that includes variety of modules to choose from. Odoo support many verticals such as Billing, CRM, CMS, Accounting, Manufacturing, Purchasing, Warehouse Management, and Project Management and so on. 

Thursday, 28 July 2016

Find foreign keys referencing a table: postgres

SELECT tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_name
AS foreign_table_name, ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
AND ccu.table_name='myTableName'

Monday, 18 July 2016

Calculate Days Between Two Dates

from datetime import date

d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d0 - d1 
print delta.days

Monday, 27 June 2016

List of List-Python

Today I am sharing a real time issue.
Currently i am working on reporting module. where to print a report of selled product i need to send product list in report file.
so, I am passing that product list like following.

product_list = [['product_1']*4]*4

Here, total sell of product_1 is 16, and in a one row of A4 size paper i want 4 product label.
so, this will give me like following result:
product_list = [['product_1', 'product_1', 'product_1', 'product_1'],
        ['product_1', 'product_1', 'product_1', 'product_1'],
        ['product_1', 'product_1', 'product_1', 'product_1'],
        ['product_1', 'product_1', 'product_1', 'product_1']]

In product_list[0][1] index i need to change 'product_1' to 'product_2'.
so, I wrote following code.

product_list[0][1] = 'product_2'

Here, i expected output is like following:

product_list = [['product_1', 'product_2', 'product_1', 'product_1'],
        ['product_1', 'product_1', 'product_1', 'product_1'],
        ['product_1', 'product_1', 'product_1', 'product_1'],
        ['product_1', 'product_1', 'product_1', 'product_1']]

But i got following result.

product_list = [['product_1', 'product_2', 'product_1', 'product_1'],
        ['product_1', 'product_2', 'product_1', 'product_1'],
        ['product_1', 'product_2', 'product_1', 'product_1'],
        ['product_1', 'product_2', 'product_1', 'product_1']]

Do you see the problem, second element of all the list changed to 'product_2'.
So it means [['product_1']*4]*4 creates a list that contains reference to the same list ['product_1', 'product_2', 'product_1', 'product_1']

To avoid this we can write following code:

n,m = 4,4
product_list = []
for x in xrange(n):
product_list.append(['product_1'] * m)
print product_list
o/p : [['product_1', 'product_1', 'product_1', 'product_1'],
       ['product_1', 'product_1', 'product_1', 'product_1'],
       ['product_1', 'product_1', 'product_1', 'product_1'],
       ['product_1', 'product_1', 'product_1', 'product_1']]

product_list[0][1] = 'product_2'
print "After Update : ",product_list
After Update : [['product_1', 'product_2', 'product_1', 'product_1'],
               ['product_1', 'product_1', 'product_1', 'product_1'],
               ['product_1', 'product_1', 'product_1', 'product_1'],
               ['product_1', 'product_1', 'product_1', 'product_1']]

Thursday, 23 June 2016

Group dictionary key values in python

Here is a tutorial for group list of dictionary by dictionary key values.

First you needs to sort your list of dictionaries.

For Ex.
animals = [{'name': 'cow', 'size': 'large'},
           {'name': 'bird', 'size': 'small'},
           {'name': 'fish', 'size': 'small'},
           {'name': 'rabbit', 'size': 'medium'},
           {'name': 'pony', 'size': 'large'},
           {'name': 'squirrel', 'size': 'medium'},
           {'name': 'fox', 'size': 'medium'}] 

Here is my list of dictionary.
To sort lits of dictionary you can use following code.

import itertools
from operator import itemgetter
sorted_animals = sorted(animals, key=itemgetter('size'))

Then use following code to group your list of dictionary.

for key, group in itertools.groupby(sorted_animals, key=lambda x:x['size']):
    print key,
    print list(group)

After you will get following result.

large [{'name': 'cow', 'size': 'large'},
       {'name': 'pony', 'size': 'large'}]
medium [{'name': 'rabbit', 'size': 'medium'},
        {'name': 'squirrel', 'size': 'medium'},
        {'name': 'fox', 'size': 'medium'}]
small [{'name': 'bird', 'size': 'small'},
       {'name': 'fish', 'size': 'small'}]

Enjoy, Your list of dictionary is group by dictionary key values.

Generate Random String-Python

Today I am sharing a small piece of Python code to generate random string that i have wrote when i was just started Python Programming. The string will have only the characters: 0-9, A-Z, a-z.


import random
random_word = ''
for i in range(len_of_word):
    random_word += random.choice('ABCDPEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz') 
print random_word

Monday, 20 June 2016

Read And Write in JSON file-Python

Here, i am going to demonstrate real life problem.
before some days, one of my friend was gone for an interview in one of the reputed company.
In interview he has given a following problem.

PROBLEM :
Write a program in python which reads a json file containing list of URLs.
Check if any url is non functional.
Save that data to another json file called : output.json

File : input.json
 
{
"url": ["http://www.programminggit.blogspot.in",
          "http://media.caranddriver.com/images/media/638444/porsche-cayman-photo-640546-s-original.jpg",
          "http://static2.consumerreportscdn.org/content/dam/cro/cars/sports/buying_lg_sports_cars.jpg",
          "http://pop.h-cdn.co/assets/cm/15/05/54cb1d27a519c_-_analog-sports-cars-01-1013-de.jpg",
          "http://pop.h-cdn.co/assets/cm/15/05/54cb1d27a519c_-_analog-sports-cars-01-10"]
}


SOLUTION :

import json

data = {}
# read a json file
with open('input.json') as f:
    json_data = json.load(f)
    for key, value in json_data.iteritems():
        data.update({key: []})
        for each_item in value:
        if str(each_item).find('pop') < 0:
            data.get(key).append(each_item)
# write in json file
with open('output.json', 'w') as f:
    json.dump(data, f)


OUTPUT : output.json

{
"url":  ["http://www.programminggit.blogspot.in",
             "http://media.caranddriver.com/images/media/638444/porsche-cayman-photo-640546-s-original.jpg",
              "http://static2.consumerreportscdn.org/content/dam/cro/cars/sports/buying_lg_sports_cars.jpg"]
}

Friday, 17 June 2016

Swap Values-Python

It's a very well known program given to the beginners, 'swap values of two variables'. In our first introductory programming course (structured programming in C) we solved it in different ways. Most of us used another temporary variable. Some of us did some math tricks. I remember that when i was beginner in programming of C i wrote the following code:

int a, b;
scanf("%d %d", &a, &b);
printf("%d %d\n", b, a);

And it made all other laugh :-D

Here is the best way of doing swapping in python. Try the following code:
a, b = 2,3
print a, b
a, b = b, a
print a, b

- Enjoy. :)