Skip to Content
Menu
This question has been flagged
1 Reply
8663 Views

Hi Guys,

I'm working on a script to import a list of product in my Odoo database. The script is working really fine with the Odoo API, except for importing the image.
What I have done so far, is to import an image from an URL in python, and then, encoding it in base64: 

        response = requests.get(image_url)
img_obj = Image.open(BytesIO(response.content))
buffer = BytesIO()
img_obj.save(buffer, format="JPEG")
img = base64.encodestring(buffer.getvalue()).strip()
try:
base64.b64decode(img)
except TypeError:
padding = len(img) % 4
if padding == 1:
img = "Invalid base64 string: {}".format(img)
elif padding == 2:
img += b'=='
elif padding == 3:
img += b'='

This code is working great. But, when I run the python code to import it with Odoo API, here is what I have:
mlrpc.client.Fault: <Fault 1: 'Traceback (most recent call last):\n File "/opt/odoo/openerp/service/wsgi_server.py", line 56, in xmlrpc_return\n result = openerp.http.dispatch_rpc(service, method, params)\n File "/opt/odoo/openerp/http.py", line 114, in dispatch_rpc\n result = dispatch(method, params)\n File "/opt/odoo/openerp/service/model.py", line 37, in dispatch\n res = fn(db, uid, *params)\n File "/opt/odoo/openerp/service/model.py", line 173, in execute_kw\n return execute(db, uid, obj, method, *args, **kw or {})\n File "/opt/odoo/openerp/service/model.py", line 118, in wrapper\n return f(dbname, *args, **kwargs)\n File "/opt/odoo/openerp/service/model.py", line 181, in execute\n res = execute_cr(cr, uid, obj, method, *args, **kw)\n File "/opt/odoo/openerp/service/model.py", line 170, in execute_cr\n return getattr(object, method)(cr, uid, *args, **kw)\n File "/opt/odoo/openerp/api.py", line 238, in wrapper\n return old_api(self, *args, **kwargs)\n File "/opt/odoo/addons/stock/product.py", line 518, in write\n return super(product_template, self).write(cr, uid, ids, vals, context=context)\n File "/opt/odoo/openerp/api.py", line 238, in wrapper\n return old_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 369, in old_api\n result = method(recs, *args, **kwargs)\n File "/opt/odoo/addons/account/models/product.py", line 45, in write\n res = super(ProductTemplate, self).write(vals)\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 562, in new_api\n result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)\n File "/opt/odoo/addons/product/product.py", line 705, in write\n res = super(product_template, self).write(cr, uid, ids, vals, context=context)\n File "/opt/odoo/openerp/api.py", line 238, in wrapper\n return old_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 369, in old_api\n result = method(recs, *args, **kwargs)\n File "/opt/odoo/addons/mail/models/mail_thread.py", line 274, in write\n result = super(MailThread, self).write(values)\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/models.py", line 3820, in write\n self._write(old_vals)\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 562, in new_api\n result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)\n File "/opt/odoo/openerp/models.py", line 3971, in _write\n result += self._columns[field].set(cr, self, id, field, vals[field], user, context=rel_context) or []\n File "/opt/odoo/openerp/osv/fields.py", line 646, in set\n \'datas\': value,\n File "/opt/odoo/openerp/api.py", line 236, in wrapper\n return new_api(self, *args, **kwargs)\n File "/opt/odoo/openerp/api.py", line 478, in new_api\n result = method(self._model, cr, uid, *args, **old_kwargs)\n File "/opt/odoo/openerp/addons/base/ir/ir_attachment.py", line 459, in create\n values[\'mimetype\'] = self._compute_mimetype(values)\n File "/opt/odoo/openerp/addons/base/ir/ir_attachment.py", line 252, in _compute_mimetype\n mimetype = guess_mimetype(values[\'datas\'].decode(\'base64\'))\n File "/usr/lib/python2.7/xmlrpclib.py", line 503, in decode\n self.data = base64.decodestring(data)\n File "/usr/lib/python2.7/base64.py", line 321, in decodestring\n return binascii.a2b_base64(s)\nError: Incorrect padding\n'>

I have tried to correct the base64 padding, (as you can see in my previous code), but that's not working. Did anyone know how can I fixe this problem?

Thanks,

Don

Avatar
Discard
Best Answer

   path = 'logo1.png'

   # get file

   with open(path, 'rb') as f:

        contents = f.read()

   imageBase64 = base64.b64encode(contents) #convert to base64

   imageBase64.decode() #pass the value of this decode result


Mail me for any question: shreeramyadav1993@gmail.com

Avatar
Discard
Related Posts Replies Views Activity
1
Jan 23
1316
1
Feb 22
2619
1
Feb 22
4809
0
Nov 21
2747
0
Jun 21
1405