This question has been flagged

Hi folks!

I am running Odoo-12 and I need help with debugging this error when trying to send an attachment to Odoo LEAD/OPPORTUNITY module using xmlrpc.client

So currently I am trying to get our website contact us form to send the leads to the leads module on Odoo CRM. Everything works fine except for the ir.attachment part. So the contact form has the option of attaching a pdf or doc file. The code im testing on is as below. The contact form information details are logged successfully but the pdf attachment is not.

The odoo instance is running on AWS provided by bitnami.

The below code fetching information from the contact page is successfully reflecting in Odoo. Which is the code below:

od.ODOO_OBJECT.execute_kw(od.DATA, od.UID, od.PASS, 'crm.lead', 'create', [{'name': "New inquiry for website", 'partner_name': "XZZ LTD", 'email_from': "example@exmaple.com", 'contact_name': "sal", 'phone': "244334", 'website': "www.example.com" , 'description': "Something"}])

But when trying to send an attachment via he contact form to Odoo, I am getting an error. The code used to send an attachment is as follows:

    values = {
    'name': 'filename',
    'type': 'binary',
    'res_id': ID of createded lead i.e(createLead),
    'res_model': 'crm.lead',
    'datas': actual file
    }

    createAttachment = od.ODOO_OBJECT.execute_kw(od.DATA, od.UID, od.PASS, 'ir.attachment', 'create', values)

Below is the error message I get when executing the ir.attachment code:

- faultCode: 1
- faultString: Traceback (most recent call last): File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/controllers/rpc.py", line 63, in xmlrpc_2 response = self._xmlrpc(service) File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/controllers/rpc.py", line 42, in _xmlrpc params, method = loads(data) File "/opt/bitnami/python/lib/python3.7/xmlrpc/client.py", line 1020, in loads p.close() File "/opt/bitnami/python/lib/python3.7/xmlrpc/client.py", line 448, in close parser.Parse(b"", True) # end of data xml.parsers.expat.ExpatError: no element found: line 1, column 0

Your help is much appreciated

Thanks!


Avatar
Discard

wrap values dict in list

---- 'ir.attachment', 'create', [values])

Author

Hi! Thank you for your reply :)

I tried this. And It looks like it addressed the issue. However, I am getting another error with regards to passing base64 encoded text.

Below is the code and error message

encoded = base64.b64encode(b"some text")

```

values = {

'name': 'filename',

'res_id': createLead,

'res_model': 'crm.lead',

'datas': encoded,

}

createAttachment = od.ODOO_OBJECT.execute_kw(od.DATA, od.UID, od.PASS, 'ir.attachment', 'create', [values])

```

This is the error message im getting:

```

xmlrpc.client.Fault: <Fault 1: 'Traceback (most recent call last):\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/controllers/rpc.py", line 63, in xmlrpc_2\n response = self._xmlrpc(service)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/controllers/rpc.py", line 43, in _xmlrpc\n result = dispatch_rpc(service, method, params)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/http.py", line 120, in dispatch_rpc\n result = dispatch(method, params)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/service/model.py", line 39, in dispatch\n res = fn(db, uid, *params)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/service/model.py", line 153, in execute_kw\n return execute(db, uid, obj, method, *args, **kw or {})\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/service/model.py", line 97, in wrapper\n return f(dbname, *args, **kwargs)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/service/model.py", line 160, in execute\n res = execute_cr(cr, uid, obj, method, *args, **kw)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/service/model.py", line 149, in execute_cr\n return odoo.api.call_kw(recs, method, args, kw)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/api.py", line 747, in call_kw\n return _call_kw_model_create(method, model, args, kwargs)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/api.py", line 727, in _call_kw_model_create\n result = method(recs, *args, **kwargs)\n File "<decorator-gen-281>", line 2, in create\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/api.py", line 430, in _model_create_single\n return create(self, arg)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/website/models/ir_attachment.py", line 25, in create\n return super(Attachment, self).create(vals)\n File "<decorator-gen-41>", line 2, in create\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/api.py", line 451, in _model_create_multi\n return create(self, [arg])\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/models/ir_attachment.py", line 504, in create\n values = self._check_contents(values)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/models/ir_attachment.py", line 255, in _check_contents\n mimetype = values[\'mimetype\'] = self._compute_mimetype(values)\n File "/opt/bitnami/apps/odoo/lib/odoo-12.0.post20190618-py3.7.egg/odoo/addons/base/models/ir_attachment.py", line 251, in _compute_mimetype\n mimetype = guess_mimetype(base64.b64decode(values[\'datas\']))\n File "/opt/bitnami/python/lib/python3.7/base64.py", line 80, in b64decode\n s = _bytes_from_decode_data(s)\n File "/opt/bitnami/python/lib/python3.7/base64.py", line 46, in _bytes_from_decode_data\n "string, not %r" % s.__class__.__name__) from None\nTypeError: argument should be a bytes-like object or ASCII string, not \'Binary\'\n'>

```

Best Answer

You need to decode it and pass the decoded_data to the key datas in your dictionary known as values.


with open('Your data/file', "rb") as pdf:
encoded_string = base64.b64encode(pdf.read())
decoded_data = encoded_string.decode('ascii')

Avatar
Discard