From 1367cbf7b26fea319e967b564c618b0394b76408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20V=C3=A9zina?= Date: Sat, 1 Feb 2025 07:59:27 -0500 Subject: [PATCH] nested fallback to odoo native way --- .../models/ir_attachment.py | 75 +++++++++---------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/msg_attachments_to_mail_message/models/ir_attachment.py b/msg_attachments_to_mail_message/models/ir_attachment.py index 48b7d59..a67364f 100644 --- a/msg_attachments_to_mail_message/models/ir_attachment.py +++ b/msg_attachments_to_mail_message/models/ir_attachment.py @@ -510,6 +510,31 @@ class IrAttachment(models.Model): html_content = f"
{content}
" return html_sanitize(html_content) + def _convert_body_to_html(self, body, content_type): + """ + Convert the message body to HTML based on its content type. + + Args: + body (str): The message body. + content_type (str): The content type of the body (e.g., 'text/plain', 'text/html', 'text/rtf'). + + Returns: + str: The message body converted to HTML. + """ + if content_type == 'text/plain': + # Convert plain text to HTML + return f"
{body}
" + elif content_type == 'text/rtf': + # Convert RTF to HTML (simplified example) + # Note: A proper RTF to HTML converter would be needed for production use + return f"
{body}
" + elif content_type == 'text/html': + # Return HTML as is + return body + else: + # Default to plain text conversion for unknown types + return f"
{body}
" + def _get_review_dir(self, error_type): """ Get the review directory path. @@ -621,6 +646,9 @@ class IrAttachment(models.Model): _logger.error("Failed to convert MSG to EML: %s", self.name) return False + # Convert the body to HTML + eml_content = self._convert_body_to_html(eml_content, 'text/plain') + # Get thread model based on res_model thread_model = ( self.env[self.res_model] if self.res_model else self.env["mail.thread"] @@ -707,10 +735,6 @@ class IrAttachment(models.Model): if log_errors: error_msg = f"Error processing MSG file: {self.name} - {str(e)}" _msg_import_logger.error(error_msg, exc_info=True) - - self._notify_error( - "Processing Error", _("Could not process the MSG file: %s") % str(e) - ) return False def _notify_error(self, title, message): @@ -735,43 +759,14 @@ class IrAttachment(models.Model): }, ) - @api.model_create_multi + @api.model def create(self, vals_list): - """Override create to handle problematic files.""" - if not isinstance(vals_list, list): - vals_list = [vals_list] - - attachments = super().create(vals_list) - - for attachment in attachments: - try: - # Check if it's a PDF file - if attachment.mimetype == 'application/pdf' and attachment.store_fname: - # Try to open and read the PDF - try: - import PyPDF2 - with open(attachment._full_path(attachment.store_fname), 'rb') as pdf_file: - try: - pdf_reader = PyPDF2.PdfReader(pdf_file) - num_pages = len(pdf_reader.pages) - _logger.info("[%s] PDF processed successfully, pages: %d", attachment.name or 'Unknown', num_pages) - except Exception as e: - _logger.warning( - "[%s] Failed to process PDF: %s", - attachment.name or 'Unknown', str(e) - ) - attachment._move_to_review_dir('pdf_error') - except Exception as e: - _logger.error("[%s] Error accessing PDF file: %s", attachment.name or 'Unknown', str(e)) - - # Process MSG files - if attachment._is_msg_file(): - attachment.process_msg_as_email() - - except Exception as e: - _logger.error("[%s] Error processing attachment: %s", attachment.name or 'Unknown', str(e)) - - return attachments + try: + return super().create(vals_list) + except Exception as e: + self.env.cr.rollback() # Annuler la transaction en cas d'erreur + _logger.error("Failed to create attachment: %s", str(e)) + raise UserError(_("Failed to create attachment: %s") % str(e)) def write(self, vals): """Override write to handle problematic files on update."""