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.

2

How to fix base64.encodestring issue?

By
Anirudh Lou
on 11/13/14, 8:44 PM 1,761 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

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 11/14/14, 12:34 AM

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

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

1 follower(s)

Stats

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