remove debug info logging from account_email_to_pdf

This commit is contained in:
Marc Durepos 2025-03-28 08:46:27 -04:00
parent 72530d8c35
commit 8162d367f0
2 changed files with 11 additions and 92 deletions

View file

@ -1,4 +1,3 @@
import base64
import logging
import os
import subprocess
@ -65,16 +64,10 @@ class AccountMove(models.Model):
# Check if the content is plain text (not HTML)
# More comprehensive check for HTML content
is_html = bool(re.search(r"<html", html_content, re.IGNORECASE))
_logger.info(f"Input content length: {len(html_content)}")
_logger.info(f"Is content already HTML? {is_html}")
if not is_html:
_logger.info(
"Content appears to be plain text or not a complete HTML document, wrapping in HTML tags"
)
# Escape the content if it's not already HTML
escaped_content = escape(html_content)
_logger.info(f"Escaped content length: {len(escaped_content)}")
# Wrap the content in basic HTML structure with proper styling for plain text
html_content = f"""
@ -92,7 +85,6 @@ class AccountMove(models.Model):
</body>
</html>
"""
_logger.info(f"Wrapped HTML content length: {len(html_content)}")
# Check if wkhtmltopdf is installed
wkhtmltopdf_bin = find_in_path("wkhtmltopdf")
@ -112,7 +104,6 @@ class AccountMove(models.Model):
# Write the HTML content to the temporary file
with closing(os.fdopen(html_file_fd, "wb")) as html_file:
encoded_content = html_content.encode("utf-8")
_logger.info(f"Encoded HTML content length: {len(encoded_content)}")
html_file.write(encoded_content)
# Close the PDF file descriptor as wkhtmltopdf will write to it
@ -129,7 +120,6 @@ class AccountMove(models.Model):
command.append(html_file_path)
command.append(pdf_file_path)
_logger.info(f"Running wkhtmltopdf command: {' '.join(command)}")
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
@ -139,27 +129,12 @@ class AccountMove(models.Model):
_logger.error(
"wkhtmltopdf failed with error code %s: %s", process.returncode, err
)
if out:
_logger.info(
f"wkhtmltopdf stdout: {out.decode('utf-8', errors='replace')[:200]}"
)
if err:
_logger.error(
f"wkhtmltopdf stderr: {err.decode('utf-8', errors='replace')}"
)
return False
# Read the generated PDF
try:
with open(pdf_file_path, "rb") as pdf_file:
pdf_content = pdf_file.read()
_logger.info(
f"Successfully read PDF file, size: {len(pdf_content)} bytes"
)
if pdf_content:
_logger.info(f"PDF file starts with: {pdf_content[:20]}")
return pdf_content
except Exception as e:
_logger.exception("Error reading generated PDF file: %s", e)
@ -168,7 +143,6 @@ class AccountMove(models.Model):
except Exception as e:
_logger.exception("Error during PDF generation: %s", e)
return False
finally:
# Clean up temporary files
try:
@ -201,7 +175,6 @@ class AccountMove(models.Model):
# Check if body is empty or None
if not body:
_logger.warning("Email body is empty, using placeholder content")
body = "<p>This email did not contain any body content.</p>"
# Check if the body is plain text based on content-type
@ -211,50 +184,35 @@ class AccountMove(models.Model):
# Try to get content type from various places in the message dict
if "content-type" in message_dict:
content_type = message_dict["content-type"].lower()
_logger.info(f"Found content-type directly in message_dict: {content_type}")
# Try to get from headers
headers = message_dict.get("headers", {})
_logger.info(f"Headers type: {type(headers)}")
if not content_type and headers:
if isinstance(headers, dict):
content_type = headers.get("Content-Type", "").lower()
_logger.info(f"Found Content-Type in headers dict: {content_type}")
elif isinstance(headers, list):
_logger.info(f"Headers is a list with {len(headers)} items")
for header in headers:
_logger.info(f"Header item: {header}")
if (
isinstance(header, tuple)
and len(header) >= 2
and header[0].lower() == "content-type"
):
content_type = header[1].lower()
_logger.info(
f"Found Content-Type in headers list: {content_type}"
)
break
# Try to determine from the body content if we still don't have a content type
if not content_type:
if re.search(r"<html", body, re.IGNORECASE):
content_type = "text/html"
_logger.info("Determined content type as HTML from body content")
else:
content_type = "text/plain"
_logger.info("Defaulting to plain text content type")
_logger.info(f"Final email content type determination: {content_type}")
# Convert plain text to HTML if needed
if "text/plain" in content_type:
_logger.info("Converting plain text email body to HTML")
# Replace newlines with <br> tags and wrap in paragraph tags
# Escape HTML special characters to prevent injection
body = f"<div style='white-space: pre-wrap; font-family: monospace;'>{escape(body)}</div>"
_logger.info(f"Converted plain text body length: {len(body)}")
_logger.info(f"First 100 chars of converted body: {body[:100]}")
# Create HTML content for the PDF
html_content = f"""
@ -283,15 +241,9 @@ class AccountMove(models.Model):
"""
# Convert HTML to PDF
_logger.info(f"HTML content length before PDF conversion: {len(html_content)}")
_logger.info(f"First 200 chars of HTML content: {html_content[:200]}")
pdf_content = self._html_to_pdf(html_content)
if pdf_content:
_logger.info(f"PDF generation successful, size: {len(pdf_content)} bytes")
_logger.info(f"PDF starts with: {pdf_content[:20]}")
else:
if not pdf_content:
_logger.error("PDF generation failed")
return False
@ -301,8 +253,6 @@ class AccountMove(models.Model):
# Note: No need to base64 encode the PDF content here
# When this attachment is added to message_dict["attachments"], Odoo expects raw binary data
# Odoo will handle the base64 encoding when creating the actual ir.attachment record
_logger.info(f"Raw PDF length: {len(pdf_content)} bytes")
attachment = {
"name": filename,
"datas": pdf_content,

View file

@ -2,9 +2,6 @@ from odoo.tests.common import TransactionCase
from odoo.tools.misc import find_in_path
from datetime import datetime
import base64
import logging
_logger = logging.getLogger(__name__)
class TestHtmlToPdf(TransactionCase):
@ -110,18 +107,12 @@ class TestHtmlToPdf(TransactionCase):
# Simple plain text content
plain_text = "This is a plain text email.\nIt has no HTML formatting.\nJust plain text content.\n\nRegards,\nTest Sender"
# Log the input content
_logger.info(f"Input plain text content: {plain_text}")
# Call the method to convert plain text to PDF
pdf_content = self.account_move._html_to_pdf(plain_text)
# Log the result
if pdf_content:
_logger.info(f"PDF content generated, size: {len(pdf_content)} bytes")
_logger.info(f"PDF starts with: {pdf_content[:20]}")
else:
_logger.error("Failed to generate PDF from plain text")
# Verify the PDF was created
self.assertTrue(
@ -130,7 +121,6 @@ class TestHtmlToPdf(TransactionCase):
# Verify it's a valid PDF
is_pdf = pdf_content and pdf_content.startswith(b"%PDF-")
_logger.info(f"Is valid PDF: {is_pdf}")
self.assertTrue(is_pdf, "Content should be a valid PDF")
self.assertTrue(len(pdf_content) > 100, "PDF should have reasonable size")
@ -236,7 +226,7 @@ Content-Transfer-Encoding: 7bit
the mail alias and verifies that an account move is created with a PDF
attachment generated from the email content.
"""
_logger.info("Starting test_plain_text_email_to_pdf_full_flow")
# Get the current timestamp
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
@ -278,24 +268,13 @@ Content-Transfer-Encoding: 7bit
)
# Process the email through the mail gateway
_logger.info("Processing plain text email through mail gateway")
_logger.info(f"Raw email length: {len(raw_email)}")
_logger.info(f"Raw email first 200 chars: {raw_email[:200]}")
try:
self.env["mail.thread"].with_context(fetchmail_server_id=1).message_process(
model=None,
message=raw_email,
save_original=True,
strip_attachments=False,
)
_logger.info("Email processing completed successfully")
except Exception as e:
_logger.error(f"Error during email processing: {str(e)}")
import traceback
_logger.error(f"Traceback: {traceback.format_exc()}")
raise
self.env["mail.thread"].with_context(fetchmail_server_id=1).message_process(
model=None,
message=raw_email,
save_original=True,
strip_attachments=False,
)
# Count account moves after the test
move_count_after = self.env["account.move"].search_count(
@ -328,16 +307,7 @@ Content-Transfer-Encoding: 7bit
# Verify PDF content if possible
if attachment:
_logger.info(f"Found PDF attachment: {attachment.name}")
pdf_data = base64.b64decode(attachment.datas) if attachment.datas else b""
_logger.info(f"PDF data size: {len(pdf_data)} bytes")
if pdf_data:
_logger.info(f"PDF data starts with: {pdf_data[:20]}")
is_valid_pdf = pdf_data.startswith(b"%PDF-")
_logger.info(f"Is valid PDF: {is_valid_pdf}")
else:
_logger.error("PDF data is empty")
self.assertTrue(
pdf_data.startswith(b"%PDF-"),
@ -346,5 +316,4 @@ Content-Transfer-Encoding: 7bit
self.assertTrue(
len(pdf_data) > 100, "PDF from plain text should have reasonable size"
)
else:
_logger.error("No PDF attachment found")