Skip to Content
Menu
This question has been flagged

i everyone,

I would like to add a message on the chat every time that someone use the "Generate PDF" button.


With the message I would like to have a message like "PDF Attached" and the PDF on attachment, like in this picture:


 I create the PDF on ir.attachments, and im trying to use message_post()  method in order to add the ID of the created attachment, but it does not work.

def add_chat_message(self, body_msg):    
    #Test with the created attachment
    attachment =  self.env['ir.attachment'].browse(4749)
self.message_post(body=body_msg, attachments=attachment)

How should I use this method on Odoo11?



Thank a lot you for your help.


Avatar
Discard
Author Best Answer

Thanks for your reply!

But I finally got it by using attachment_ids argument on message_post, and storing the attachments in a list.

# Add attachments

for pdf in self.env['ir.attachment'].search([(...)]):

attachments.append(pdf.id)

and

self.message_post(body=body_msg, attachment_ids=attachments)

Avatar
Discard
Best Answer

Hi, 

We can create message to model 'mail.compose.message' or in 'mail.mail'

attachment_id = self.env['ir.attachment'].create({
'name': self.name,
'type': 'binary',
'datas': base64.b64encode(pdf),
'datas_fname': self.name + '.pdf',
'store_fname': self.name,
'res_model': self._name,
'res_id': self.id,
'mimetype': 'application/x-pdf'
})
ctx = {
'default_model': 'your.model',
'default_res_id': self.id,
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',

}
template = self.env['mail.template'].browse(template_id)
html_body = template.body_html
text = template.with_context(ctx).render_template(html_body, 'your.model', self.id)
compose = self.env['mail.mail'].with_context(ctx).create({
'subject': '%s' % mail_subject,
'body_html': text,
'attachment_ids': [(6, 0, [attachment_id.id])] or None,
})
if you need with in a specified template you can pass template id as above code.
when using model mail.compose.message we get attachment as well as the all data on template and use the function send_mail() to send by adding email_to 

Thanks

Avatar
Discard
Best Answer

Hi,

Would be helpful, if we want to attach report with creating attachments. This would be very helpful.

def _attach_sign(self):
""" Render the delivery report in pdf and attach it to the picking in `self`. """
self.ensure_one()
report = self.env['ir.actions.report']._render_qweb_pdf("stock.action_report_delivery", self.id)
filename = "%s_signed_delivery_slip" % self.name
if self.partner_id:
message = _('Order signed by %s') % (self.partner_id.name)
else:
message = _('Order signed')
self.message_post(
attachments=[('%s.pdf' % filename, report[0])],
body=message,
)
return True

Output would be like this.


Avatar
Discard
Related Posts Replies Views Activity
0
Feb 19
2615
1
Jan 23
8243
2
Nov 21
6685
1
Oct 21
4238
0
Mar 21
1667