This question has been flagged
1 Reply
28681 Views

I'm still working on porting the module https://www.odoo.com/apps/modules/10.0/product_default_image/ to Odoo 11.0.

When trying to save an PNG image with the following code in the file rescompany.py:

@api.model
def _get_default_image(self):
with open(modules.get_module_resource('product_default_image',
'static/src/img',
'glob_prod_img.png'),
'rb') as f:
return base64.b64encode(f.read())

product_image = fields.Binary(
string='Global Product Image',
default=_get_default_image,
help='Use as the global image for all product default images. '
'Limited to 1024x1024.',
)


@api.multi
def write(self, vals):
""" Changes product images if target or product_image have changed """

target = vals.get('product_image_target')

if 'product_image' not in vals and not target:
return super(ResCompany, self).write(vals)

if 'product_image' in vals:
vals['product_image'] = \
tools.image_resize_image_big(
base64_source=vals['product_image'],
)

I get the following error message:

Error:
Odoo Server Error

Traceback (most recent call last):
File "/usr/lib/python3.6/base64.py", line 510, in _input_type_check
m = memoryview(s)
TypeError: memoryview: a bytes-like object is required, not 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3.6/encodings/base64_codec.py", line 19, in base64_decode
return (base64.decodebytes(input), len(input))
File "/usr/lib/python3.6/base64.py", line 545, in decodebytes
_input_type_check(s)
File "/usr/lib/python3.6/base64.py", line 513, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/http.py", line 653, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 312, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 87, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/http.py", line 695, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 344, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 337, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 939, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 517, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 934, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 926, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 687, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 672, in call_kw_model
result = method(recs, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/addons/account/models/res_config_settings.py", line 152, in create
return super(ResConfigSettings, self).create(values)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 3402, in create
self._fields[key].determine_inverse(record)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1082, in determine_inverse
self.inverse(records)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 599, in _inverse_related
other[field.name] = record_value[record]
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4794, in __setitem__
return self._fields[key].__set__(self, value)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 987, in __set__
record.write({self.name: write_value})
File "/usr/lib/python3/dist-packages/odoo/addons/product_default_image/models/res_company.py", line 76, in write
base64_source=vals['product_image'],
File "/usr/lib/python3/dist-packages/odoo/tools/image.py", line 143, in image_resize_image_big
return image_resize_image(base64_source, size, encoding, filetype, avoid_if_small)
File "/usr/lib/python3/dist-packages/odoo/tools/image.py", line 56, in image_resize_image
image_stream = io.BytesIO(codecs.decode(base64_source, encoding))
TypeError: decoding with 'base64' codec failed (TypeError: expected bytes-like object, not str)

How do I correctly code this for Python3/Odoo 11?

Thanks for any hints.




Avatar
Discard
Best Answer

Hi,

Refer this link

https://stackoverflow.com/questions/2323128/convert-string-in-base64-to-image-and-save-on-filesystem-in-python


Avatar
Discard
Author

Thank you for your hint, I understand the direction, but why is it no handled by the tools from image.py in Odoo 11? I have amended my code in the post. Is there something missing in my @api.model?