Search code examples
pythonxmlrpclibopenerp-6

print multiple reports using a single method openerp 6.1


I'm trying to print multiple invoices using a single method, i have tried in two ways so far,from within openerp using a button and method called on that and by externally defining a python script that invokes the print externally but the script fails with internal server error. here are both the methods, 1 method called on the button

def update_invoice_report(self,cr,uid,ids,context=None):
    compare_date=datetime.now().date().strftime("%Y-%m-%d")
    val=[]
    count=0
    # file_name='/home/chaitalikelsukar/Downloads/invoice_print (*).pdf'
# for j in self.browse(cr,uid,ids):
    # if not j.model_name :
    #   raise osv.except_osv(('Alert'),('Select Model Invoice'))
    # if not j.unique_number:
    #   raise osv.except_osv(('Alert'),('Enter Number'))
    use_date=compare_date
    # if j.model_name=='invoice':
    query="select id from invoice_adhoc_master where invoice_date >= '2017-05-01' and invoice_date < '2017-06-01'"
    cr.execute(query)
    list1=cr.fetchall()
    print list1
    contract_search=[i[0] for i in list1 if i[0]]
    if contract_search:
        for i in contract_search:
            data =self.pool.get('invoice.adhoc.master').read(cr,uid,contract_search,context)
            datas={

                'ids':ids,
                'model':'invoice.adhoc.master',
                'form':data
            }
            return {
                'type':'ir.actions.report.xml',
                'report_name':'invoice_print'
                'datas':datas
            }

the above code returns all the invoices within the range but i'm unable to move it after being printed

2nd method:

def awesome():
        f = open('config.txt', 'r')
        lines = f.readlines()
        # line_iter= iter(lines)
        login_credentials = []
        file_name = ''
        dbname=''
        line =lines[0]
        string_pdf =''
        # print line
        # try:
        if (True):
            dbname=str(line.split('=')[1]).split('\n')[0]
            # print dbname

            if dbname:
                # connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+localhost+"' password='"+'openerp'+"' "
                # else:
                connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+'localhost'+"' password='"+'openerp'+"' "
                conn_pg = psycopg2.connect(connect_string)
                pg_cursor = conn_pg.cursor()
                # pg_cursor.execute("")
                pg_cursor.execute("SELECT id from res_company where dbname='%s'"%dbname)
                invoice_ids = pg_cursor.fetchall()
                if tuple(invoice_ids):
                    pg_cursor.execute("SELECT company_id from account_account limit 1")
                    invoice_ids =pg_cursor.fetchone()
                pg_cursor.execute("select vpn_ip_address,port,dbname,pwd,user_name from res_company where id =%s"%invoice_ids)
                company_data=pg_cursor.fetchone()
                # print invoice_ids,company_data
                vpn_ip_addr = company_data[0]
                port =company_data[1]
                dbname = company_data[2]
                pwd = company_data[3]
                user_name = company_data[4]
                pg_cursor.execute("select login from res_users where id='%s'"%user_name)
                user_name=pg_cursor.fetchone()[0]
                username = user_name
                pwd = pwd
                db=dbname = dbname
                # print dbname
                log = ('http://%s:%s/xmlrpc/common')%(vpn_ip_addr,port)
                obj = ('http://%s:%s/xmlrpc/object')%(vpn_ip_addr,port)
                print_sock = ('http://%s:%s/xmlrpc/report')%(vpn_ip_addr,port)
                model='invoice.adhoc.master'

                sock_common = xmlrpclib.ServerProxy (log)
                uid = sock_common.login(dbname, username, pwd)
                sock = xmlrpclib.ServerProxy(obj)
                print_sock=xmlrpclib.ServerProxy(print_sock)
                pg_cursor.execute("select id from invoice_adhoc_master where invoice_date >='2017-05-01' and invoice_date < '2017-05-31'")
                invoice_ids=pg_cursor.fetchall()
                for i in invoice_ids:
                    data= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'read',i)
                    print data
                    # id_report = print_sock.report(dbname, uid, pwd, model, i, {'model': 'invoice_print', 'id': i[0], 'report_type':'pdf'})
                    id_report = print_sock.report(dbname,uid,pwd,model,i,{'type':'ir.actions.report.xml','report_name':'invoice_print','datas':data})
            # return {
            #       'type': 'ir.actions.report.xml',
            #       'report_name': 'invoice_print',
            #       'datas': datas,
            #       }
                    partner_ids= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'print_invoice',i)
                    # id_report = print_sock.report(dbname,uid,pwd,model,i,partner_ids)
                    report = print_sock.report_get(db, uid, pwd, id_report)
                    # string_pdf = base64.decodestring(report)
                    file_pdf = open('/tmp/file1.pdf','w')
                    file_pdf.write(partner_ids)
                    file_pdf.close()
                    # print_sock.report_get(db, uid, pwd, id_report)
                    # print partner_ids,"\n\n\n\n",id_report
                    # m
                    # report_id = print_sock.report(db, uid, pwd, 'invoice.adhoc.master', partner_ids, {'model': 'invoice.adhoc.master', 'id': partner_ids, 'report_type': 'pdf'})
                    # time.sleep(3);
                    # state = False
                    # attempt = 0
                    # while not state:
                    #   state = report['state']
                    #   if not state:
                    #       time.sleep(1)
                    #       attempt += 1
                    #   if attempt > 200:
                    #       print 'Abort, too long delay'
                    return partner_ids
                    # return {

this fails on the line

                report = print_sock.report_get(db, uid, pwd, id_report)

with

xmlrpclib.protocolerror:<protocolerror for localhost:8765/xmlrpc/report: 500 internal server error>

Solution

  • I solved it:

    def awesome():
        f = open('config.txt', 'r')
        lines = f.readlines()
        # line_iter= iter(lines)
        login_credentials = []
        file_name = ''
        dbname=''
        line =lines[0]
        string_pdf =''
        base_path='/home/chaitalikelsukar/Desktop/report_dump/'
        # print line
        # try:
        if (True):
            dbname=str(line.split('=')[1]).split('\n')[0]
            # print dbname
    
            if dbname:
                # connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+localhost+"' password='"+'openerp'+"' "
                # else:
                connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+'localhost'+"' password='"+'openerp'+"' "
                conn_pg = psycopg2.connect(connect_string)
                pg_cursor = conn_pg.cursor()
                # pg_cursor.execute("")
                pg_cursor.execute("SELECT id from res_company where dbname='%s'"%dbname)
                invoice_ids = pg_cursor.fetchall()
                if tuple(invoice_ids):
                    pg_cursor.execute("SELECT company_id from account_account limit 1")
                    invoice_ids =pg_cursor.fetchone()
                pg_cursor.execute("select vpn_ip_address,port,dbname,pwd,user_name from res_company where id =%s"%invoice_ids)
                company_data=pg_cursor.fetchone()
                # print invoice_ids,company_data
                vpn_ip_addr = company_data[0]
                port =company_data[1]
                dbname = company_data[2]
                pwd = company_data[3]
                user_name = company_data[4]
                pg_cursor.execute("select login from res_users where id='%s'"%user_name)
                user_name=pg_cursor.fetchone()[0]
                username = user_name
                pwd = pwd
                db=dbname = dbname
                # print dbname
                log = ('http://%s:%s/xmlrpc/common')%(vpn_ip_addr,port)
                obj = ('http://%s:%s/xmlrpc/object')%(vpn_ip_addr,port)
                # print_sock = ('http://%s:%s/xmlrpc/report')%(vpn_ip_addr,port)
                printsock = xmlrpclib.ServerProxy('http://localhost:8765/xmlrpc/report')
                model='invoice.adhoc.master'
    
                sock_common = xmlrpclib.ServerProxy (log)
                uid = sock_common.login(dbname, username, pwd)
                sock = xmlrpclib.ServerProxy(obj)
                # print_sock=xmlrpclib.ServerProxy(print_sock)
                pg_cursor.execute("select id from invoice_adhoc_master where invoice_date >='2017-05-01' and invoice_date < '2017-05-31' and invoice_number is not null")
                invoice_ids=pg_cursor.fetchall()
                for i in invoice_ids:
                    # data= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'read',i)
                    pg_cursor.execute("select invoice_number from invoice_adhoc_master where id = '%s' limit 1"%i)
                    invoice_number=pg_cursor.fetchone()
                    file_name=base_path+invoice_number[0]+'.pdf'
                    # print data
                    # id_report = print_sock.report(dbname, uid, pwd, model, i, {'model': 'invoice.adhoc.master', 'id': i[0], 'report_type':'pdf'})
                    id_report = printsock.report(dbname, uid, pwd, 'invoice_print', i, {'model': 'invoice.adhoc.master', 'id': i[0], 'report_type':'pdf'})
                    time.sleep(5)
                    state = False
                    attempt = 0
                    while not state:
    
                        report = printsock.report_get(dbname, uid, pwd, id_report)
    
                        # id_report = print_sock.report(dbname,uid,pwd,model,i,{'type':'ir.actions.report.xml','report_name':'invoice_print','datas':data})
                # return {
                #       'type': 'ir.actions.report.xml',
                #       'report_name': 'invoice_print',
                #       'datas': datas,
                #       }
                        state = report['state']
                        if not state:
                            time.sleep(1)
                            attempt += 1
                        if attempt>200:
                            print 'Printing aborted, too long delay !'
                        # partner_ids= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'print_invoice',i)
                        # id_report = print_sock.report(dbname,uid,pwd,model,i,partner_ids)
                        # report = print_sock.report_get(dbname, uid, pwd, id_report)
                        print report
                        string_pdf = base64.decodestring(report['result'])
                        file_pdf = open(file_name,'w')
                        file_pdf.write(string_pdf)
                        file_pdf.close()
                        # print_sock.report_get(db, uid, pwd, id_report)
                        # print partner_ids,"\n\n\n\n",id_report
                        # m
                        # report_id = print_sock.report(db, uid, pwd, 'invoice.adhoc.master', partner_ids, {'model': 'invoice.adhoc.master', 'id': partner_ids, 'report_type': 'pdf'})
                        # time.sleep(3);
                        # state = False
                        # attempt = 0
                        # while not state:
                        #   state = report['state']
                        #   if not state:
                        #       time.sleep(1)
                        #       attempt += 1
                        #   if attempt > 200:
                        #       print 'Abort, too long delay'
        return True
    

    The problem was on the line:

                id_report = printsock.report(dbname, uid, pwd, 'invoice_print', i, {'model': 'invoice.adhoc.master', 'id': i[0], 'report_type':'pdf'})
    

    My report name in report's py was report.invoice_print instead of report.invoice.adhoc.master which is the default.