Odoo Help

2

How to fix base64.encodestring issue?

By
Anirudh Lou
on 11/13/14, 8:44 PM 2,375 views

Hi everyone. Can anyone help me, pls!

I have my problem with base64 but i cant resolve it. What i'm doing is that i am trying to persist a file (either .DAT or .csv) on my database but upon running:

1. On my method file_writer, it says:

'file_data' : base64.encodestring(virtual_file),

File "/usr/lib/python2.7/base64.py", line 313, in encodestring for i in range(0, len(s), MAXBINSIZE):

AttributeError: StringIO instance has no attribute '__len__'

 

2. On my method csv_writer, it says:

    'name' : 'Studentlist',
  File "/usr/lib/python2.7/base64.py", line 313, in encodestring
    for i in range(0, len(s), MAXBINSIZE):
TypeError: object of type '_csv.writer' has no len()

 

Here are my methods:

1.

def file_writer(self, cr, uid, ids, context=None):
        
        virtual_file = StringIO()
        student_object = self.pool.get('student.student')
        active_id = student_object.search(cr,uid,[])
        for obj_list in student_object.browse(cr,uid,active_id,context=context):
            virtual_file.write(str(obj_list.last_name)+","+str(obj_list.first_name)+","+str(obj_list.middle_name))
        self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : base64.encodestring(virtual_file),
                            })
        return True

 

( Here, without base64 it persist into database but upon opening of downloaded file it shows something like this :

JÚâžJÚ➊{-jwjÝ1í÷5÷Î;kO|  )
    

2.

def csv_writer(self, cr, uid, ids, context=None):
        
        virtual_file = csv.writer(open("/tmp/Student.csv","wb"))
       student_object = self.pool.get('student.student')
        active_id = student_object.search(cr,uid,[])
        for obj_list in student_object.browse(cr,uid,active_id,context=context):
            virtual_file.writerow([str(obj_list.last_name),str(obj_list.first_name),str(obj_list.middle_name)])
        self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : base64.encodestring(virtual_file),
                            })
        return True

(Here, w/o base64 it shows:

  File "/usr/lib/python2.7/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

)

 

Any help is much appreciated.

 

Error image:

 

1

Hi,

You just need to close your file which is attached with your writer object. And then you have to again open for read and pass it to the base64.encodestring() function. Please have a look on the below.

virtual_file.seek(0)

self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : base64.encodestring(virtual_file.read()),
                            })

vrtual_file.close()

Note : You have passed writer object to the base64.encodestring() function thats why you face an error.

Please try according my solution as like below.

file_point = open('/tmp/student.csv','wb')

student_object = self.pool.get('student.student')
active_id = student_object.search(cr,uid,[])
for obj_list in student_object.browse(cr,uid,active_id,context=context):
    file_point.write(str(obj_list.last_name)+","+str(obj_list.first_name)+","+str(obj_list.middle_name))

file_point.close()
file_point = open('/tmp/student.csv','rb')
file_data = 
base64.encodestring(file_point.read())
self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : file_data,
                            })

Thank you sir for the response, i tried your suggestion but the terminal says: AttributeError: '_csv.writer' object has no attribute 'close'

Anirudh Lou
on 11/14/14, 12:45 AM

Is it ok for StringIO ? For the csv.writer just try without close.

Emipro Technologies Pvt. Ltd.
on 11/14/14, 1:01 AM

I have tried in my other method (file_writer method) and it rendered the view, however no object on fields file_data. In my csv_writer method it produce an AttributeError.

Anirudh Lou
on 11/14/14, 1:05 AM

which AttributeErro ? can you put entire trace into your question ? It is easy to resolve your issue.

Emipro Technologies Pvt. Ltd.
on 11/14/14, 1:19 AM

This is what it produce sir: Traceback (most recent call last): File "/opt/openerp/server-7/openerp/netsvc.py", line 296, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/opt/openerp/server-7/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, *params) File "/opt/openerp/server-7/openerp/osv/osv.py", line 190, in execute_kw return self.execute(db, uid, obj, method, *args, **kw or {}) File "/opt/openerp/server-7/openerp/osv/osv.py", line 132, in wrapper return f(self, dbname, *args, **kwargs) File "/opt/openerp/server-7/openerp/osv/osv.py", line 199, in execute res = self.execute_cr(cr, uid, obj, method, *args, **kw) File "/opt/openerp/server-7/openerp/osv/osv.py", line 187, in execute_cr return getattr(object, method)(cr, uid, *args, **kw) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist.py", line 131, in load_alphalist return self.pool.get('alphalist.minimum.prev.employer').load_all_employees(cr,uid,ids,data,context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 155, in load_all_employees id2 = self.call_file_writer(cr, uid, ids, context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 190, in call_file_writer return self.pool.get('file.holder').csv_writer(cr,uid,ids,context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 1351, in csv_writer virtual_file.close() AttributeError: '_csv.writer' object has no attribute 'close'

Anirudh Lou
on 11/14/14, 1:46 AM

I use virtual_file.close() on my csv_writer method but it produce an error like this one: virtual_file.close() AttributeError: '_csv.writer' object has no attribute 'close'

Anirudh Lou
on 11/14/14, 1:48 AM

Hi, I have update my answer. Please try it and let me know the result.

Emipro Technologies Pvt. Ltd.
on 11/14/14, 2:27 AM

Sir it produce another error, here is what it says: virtual_file.seek(0) AttributeError: '_csv.writer' object has no attribute 'seek'

Anirudh Lou
on 11/14/14, 2:37 AM

May I know that which library you have import in your module ?

Emipro Technologies Pvt. Ltd.
on 11/14/14, 4:30 AM

Here are some of my imports sir: import logging import base64 import csv

Anirudh Lou
on 11/14/14, 4:50 AM

Hi, I am confused because csv have all of those (close, seek, etc.) attributes but in your system it gives an attribute error. So, please try as I have given you in my updated answer.

Emipro Technologies Pvt. Ltd.
on 11/14/14, 5:11 AM

Have you tried updated answer and then you got an error while down load "Incorrect padding" ? ?

Emipro Technologies Pvt. Ltd.
on 11/14/14, 7:08 AM

I really don't know what is going on with the code, but every time i plug csv attributes it pops up AttributeError ('_csv.writer' object has no attribute 'close' and the like.)

Anirudh Lou
on 11/14/14, 8:18 PM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 11/13/14, 8:44 PM
Seen: 2375 times
Last updated: 3/16/15, 8:10 AM