This question has been flagged
2 Replies
19464 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 :)

Avatar
Discard
Best Answer

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.

Avatar
Discard

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

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

Best Answer

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

Avatar
Discard