Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
15386 Lượt xem

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.


Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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)

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 2 19
3287
1
thg 1 23
9345
2
thg 11 21
7987
1
thg 10 21
5017
0
thg 3 21
2243