Files
odoo_source/addons/mail/models/mail_thread_main_attachment.py
T
Yolann Sabaux 71ed187223 [FIX] account: display pdf attachment
Steps to reproduce:
- Install l10n_mx modules
- Switch to MX company
- Create invoice and confirm it
- Send it to PAC in test environment (Process Now button)
- Click Send & Print button

Issue:
- XML Preview does not display, only shows the file name in the top right corner of the chatter.

Cause:
In l10n_mx_edi, when posting the invoice, the only attachment available is the xml sent to the government in `_message_set_main_attachment_id`.
Therefore the xml is set as the main attachment.
When clicking on "Send and Print", the pdf is generated but the main attachment is still the xml

Solution:
Redefine the main attachment everytime the the main attachment is an xml

Note:
- octet-stream have also been filtered out

opw-3085934

closes odoo/odoo#116784

X-original-commit: 48e6e81a47f5371f5985d135e56f39f445eb2854
Related: odoo/enterprise#38861
Signed-off-by: Yolann Sabaux (yosa) <yosa@odoo.com>
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2023-03-28 13:43:57 +02:00

42 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class MailMainAttachmentMixin(models.AbstractModel):
""" Mixin that adds main attachment support to the MailThread class. """
_name = 'mail.thread.main.attachment'
_inherit = 'mail.thread'
_description = 'Mail Main Attachment management'
message_main_attachment_id = fields.Many2one(string="Main Attachment", comodel_name='ir.attachment', copy=False)
def _message_post_after_hook(self, message, msg_values):
""" Set main attachment field if necessary """
super()._message_post_after_hook(message, msg_values)
self.sudo()._message_set_main_attachment_id([
attachment_command[1]
for attachment_command in (msg_values['attachment_ids'] or [])
])
def _message_set_main_attachment_id(self, attachment_ids):
if attachment_ids and not self.message_main_attachment_id:
# we filter out attachment with 'xml' and 'octet' types
attachments = self.env['ir.attachment'].browse(attachment_ids).filtered(lambda r: not r.mimetype.endswith('xml')
and not r.mimetype.endswith('application/octet-stream'))
# Assign one of the attachments as the main according to the following priority: pdf, image, other types.
if attachments:
self.with_context(tracking_disable=True).message_main_attachment_id = max(
attachments,
key=lambda r: (r.mimetype.endswith('pdf'), r.mimetype.startswith('image'))
).id
def _get_mail_thread_data(self, request_list):
res = super()._get_mail_thread_data(request_list)
if 'attachments' in request_list:
res['mainAttachment'] = {'id': self.message_main_attachment_id.id} if self.message_main_attachment_id else [('clear',)]
return res