Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

make button run python script

By
Mohamed Magdy
on 6/4/13, 8:55 AM 4,747 views

within working on modifying hr_attendance module, so, it can convert MS Access file to postgresql we wrote this python script that can be used manually :

#!/usr/bin/python

import psycopg2         # module to connect to postgresql
import sys 
import csv          # to read csv files
from subprocess import call # to run system commands

mdb_path='/path/to/*.mdb file'      # path to MS Access file 
conversion_string="mdb-export %s CHECKINOUT | tail -n +2 | cut -d, -f1,2,3 | tr / - >/path/to/*.csv file"%mdb_path
call(conversion_string,shell=True)  # getting the .csv file from .mdb file using {mdb-tools >> in Redhat/Centos/Fedora} || {mdbtools >> in Debian/Ubuntu
con=None        # Initialize no connection
con = psycopg2.connect(database='DB_Name', user='postgres') # Connect to database named(test) with user(postgres)
cur = con.cursor()  
i=1     # counter to count the number of errors, i.e, number of not inserted rows that mean the data is already exist
table_name='hr_attendance'  # the table name to insert data 
with open('/path/to/*.csv file','r') as f:
  re = csv.reader(f,delimiter=',',quoting=csv.QUOTE_NONE)
  for r in re:
    employee_id= int(r[0])
    name=r[1].replace('\"','')
    checktype=r[2].replace('\"','')

    if checktype == "I":
      action = "sign_in"
    else:
      action = "sign_out"

    try:
      cur.execute("insert into %s (employee_id, name, action, day) values(%d,'%s','%s',date('%s'))"%(table_name,employee_id,name,action,name))
    except psycopg2.DatabaseError, e:
        print 'error %d'%i
        i+=1

    con.commit()

Now, I need to create a button to run this script to update the postgresql using the MS Accsess file

Thanks in advance :)

4

Borni DHIFI

--Borni DHIFI--
5102
| 6 8 9
Tunis, Tunisia
--Borni DHIFI--

Techno Functional Consultant Odoo(OpenERP) 

http://www.linkedin.com/in/borni

Skype : dhifi.borni

Borni DHIFI
On 6/4/13, 9:18 AM

Hi,

create a function to excute your code:

your_file.py:

 def access_to_postgresql(self, cr, uid, ids, context=None): 
     #your code here

your_file.xml

<button name="access_to_postgresql" string="Import" type="object"/>

the name of button is a name of function.

My py function returns a action url after that my button disables.Open erp adds an attribute disable="disable" to my button . I dnt want to disable button.Please help

Anuradha
on 9/27/13, 8:08 AM

is there a way to do that from wizard directly, without having to go over a button ?

wurst
on 4/2/14, 12:10 AM
0
Antanas
On 6/4/13, 9:17 AM

Maybe create a wizard, where this code would be a wizard method, and in a wizard view place a button to trigger this code?

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 6/4/13, 8:55 AM
Seen: 4747 times
Last updated: 3/16/15, 8:10 AM