From ebaef085b2e7a1889c545225e368329d5656e9bb Mon Sep 17 00:00:00 2001 From: lucasmndc Date: Thu, 15 Aug 2024 12:10:32 -0300 Subject: [PATCH] =?UTF-8?q?Adi=C3=A7=C3=A3o=20de=20html=20no=20email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/utils/models.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sigi/apps/utils/models.py b/sigi/apps/utils/models.py index 59d720e..519a7ee 100644 --- a/sigi/apps/utils/models.py +++ b/sigi/apps/utils/models.py @@ -12,6 +12,8 @@ from tinymce.models import HTMLField from django.core.mail import send_mail from django.conf import settings from datetime import timedelta +from docutils.core import publish_string +from django.utils.html import format_html class SigiAlert(models.Model): @@ -224,6 +226,12 @@ class JobSchedule(models.Model): now = timezone.localtime() + # Converte o resultado para HTML usando docutils + try: + html_result = publish_string(self.resultado, writer_name="html") + except Exception as e: + html_result = f"

Erro ao converter o log para HTML: {str(e)}

" + if self.job.digest == "N": # Envia imediatamente sem acumular send_mail( @@ -232,7 +240,7 @@ class JobSchedule(models.Model): from_email=settings.SERVER_EMAIL, recipient_list=self.job.get_emails_list(), fail_silently=True, - html_message=self.resultado, + html_message=html_result.decode("utf-8"), ) self.job.last_digest = now self.job.save() @@ -279,11 +287,24 @@ class JobSchedule(models.Model): if job_schedules.exists(): message_lines = [] + html_message_lines = [] for js in job_schedules: message_lines.append( f"{localize(js.iniciado)}: {js.resultado}" ) + try: + html_message_lines.append( + publish_string( + js.resultado, writer_name="html" + ).decode("utf-8") + ) + except Exception as e: + html_message_lines.append( + f"

Erro ao converter o log para HTML: {str(e)}

" + ) + message = "\n\n".join(message_lines) + html_message = "

".join(html_message_lines) send_mail( subject=f"Digest JOB: {self.job.job_name} ({frequency})", @@ -291,7 +312,7 @@ class JobSchedule(models.Model): from_email=settings.SERVER_EMAIL, recipient_list=self.job.get_emails_list(), fail_silently=True, - html_message=message, + html_message=html_message, )