From 5321ade8a586dc010f76b4ac48951b41e856239d Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Fri, 13 Sep 2024 10:14:05 +0200 Subject: [PATCH] wip send email with link to doc --- src/backend/core/api/viewsets.py | 11 ++++++++++- src/backend/core/models.py | 32 +++++++++++++++++++++++++------- src/mail/mjml/summary.mjml | 17 +++++------------ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 9facf659..6bc44eeb 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -2,6 +2,7 @@ import smtplib import uuid +import requests from django.conf import settings from django.db.models import Q @@ -270,9 +271,17 @@ class RoomViewSet( transcript = self.request.data.get("transcript", None) instance = self.get_object() + owners = instance.get_owners() + email = owners[0].email + + url = f"https://dinum-pad-dev.osc-fr1.scalingo.io/summary?email={email}" + headers = { + "Content-Type": "text/plain" + } + response = requests.post(url, headers=headers, data=summary, allow_redirects=False) try: - instance.email_summary(summary=summary, transcript=transcript) + instance.email_summary(summary=summary, transcript=transcript, owners=owners, link=response.headers['Location']) except smtplib.SMTPException: return drf_response.Response({"message": "Error"}, status=500) diff --git a/src/backend/core/models.py b/src/backend/core/models.py index f726c6f7..ef1f100c 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -16,6 +16,9 @@ from django.utils.functional import lazy from django.utils.text import capfirst, slugify from django.utils.translation import gettext_lazy as _ +from django.core.mail import EmailMessage +from django.core.files.base import ContentFile + from timezone_field import TimeZoneField logger = getLogger(__name__) @@ -151,11 +154,28 @@ class User(AbstractBaseUser, BaseModel, auth_models.PermissionsMixin): def __str__(self): return self.email or self.admin_email or str(self.id) - def email_user(self, subject, message, from_email=None, **kwargs): + def email_user(self, subject, transcript, from_email=None, **kwargs): """Email this user.""" if not self.email: raise ValueError("User has no email address.") - mail.send_mail(subject, message, from_email, [self.email], **kwargs) +# mail.send_mail(subject, message, from_email, [self.email], **kwargs) + + email = EmailMessage( + subject, + kwargs['html_message'], + from_email, + [self.email], + ) + +# email.attach_alternative(kwargs['html_message'], "text/html") + + transcript = transcript or '' + + wip_file = ContentFile(transcript, 'transcript.txt') + + email.content_subtype = "html" + email.attach(wip_file.name, wip_file.read(), 'text/plain') + email.send() def get_teams(self): """ @@ -332,16 +352,14 @@ class Room(Resource): owner_accesses = self.accesses.filter(role=RoleChoices.OWNER) return [access.user for access in owner_accesses] - def email_summary(self, summary, transcript): + def email_summary(self, owners, summary, transcript, link): """Wip""" - owners = self.get_owners() - template_vars = { "title": _("A new summary is ready"), "room": self.slug, "summary": summary, - "transcript": transcript, + "link": link, } msg_plain = render_to_string("mail/text/summary.txt", template_vars) @@ -350,7 +368,7 @@ class Room(Resource): for owner in owners: owner.email_user( subject=_("A new summary is ready"), - message=msg_plain, + transcript=transcript, from_email=settings.EMAIL_FROM, html_message=msg_html, fail_silently=False, diff --git a/src/mail/mjml/summary.mjml b/src/mail/mjml/summary.mjml index 462d42d2..61709ed3 100644 --- a/src/mail/mjml/summary.mjml +++ b/src/mail/mjml/summary.mjml @@ -2,25 +2,18 @@ - - - - - -

Summary:

-

{{summary}}

-
- -

Transcript:

-

{{transcript}}

+

Dear user,

+

The meeting {{room}} has been successfully transcribed and summarized.

+ + Get your summary +
-