update send email uses

This commit is contained in:
Aarni Halinen
2022-01-13 21:53:55 +02:00
parent 6f316401f7
commit 7382c4e4bf
7 changed files with 263 additions and 252 deletions
+44 -28
View File
@@ -1,18 +1,30 @@
"""Webapp utils."""
from django.utils import timezone
import sendgrid
from sendgrid.helpers.mail import *
from datetime import timedelta
import logging
from django.template.loader import render_to_string
from django.core.files.base import ContentFile
import base64
import uuid
from sikweb.settings import FRONTEND_URL, EMAIL_API_KEY, DEFAULT_EMAIL_FROM_ADDR, ENABLE_AUTOMATIC_EMAILS
import imghdr
import markdown
import sendgrid
from django.utils import timezone
from sendgrid.helpers.mail import (
Email,
To,
Subject,
Mail,
HtmlContent,
PlainTextContent,
)
from django.template.loader import render_to_string
from django.core.files.base import ContentFile
from sikweb.settings import (
FRONTEND_URL,
EMAIL_API_KEY,
DEFAULT_EMAIL_FROM,
DEFAULT_EMAIL_FROM_ADDR,
ENABLE_AUTOMATIC_EMAILS,
)
from datetime import timedelta
def get_file_extension(file_name, decoded_file):
@@ -25,15 +37,15 @@ def decode_base64_file(data):
# Check if this is a base64 string
if isinstance(data, str):
# Check if the base64 string is in the "data:" format
if 'data:' in data and ';base64,' in data:
if "data:" in data and ";base64," in data:
# Break out the header from the base64 content
header, data = data.split(';base64,')
header, data = data.split(";base64,")
# Try to decode the file. Return validation error if it fails.
try:
decoded_file = base64.b64decode(data)
except TypeError:
TypeError('invalid_image')
TypeError("invalid_image")
# Generate file name:
file_name = str(uuid.uuid4())
@@ -41,7 +53,10 @@ def decode_base64_file(data):
# Get the file name extension:
file_extension = get_file_extension(file_name, decoded_file)
complete_file_name = "%s.%s" % (file_name, file_extension, )
complete_file_name = "%s.%s" % (
file_name,
file_extension,
)
return ContentFile(decoded_file, name=complete_file_name)
@@ -51,50 +66,51 @@ def month_from_now():
return timezone.now() + timedelta(days=30)
def send_email(to, subject, body, html=False):
if not ENABLE_AUTOMATIC_EMAILS:
def send_email(to: str, subject: str, body: str, html: bool = False):
if ENABLE_AUTOMATIC_EMAILS is False:
logging.debug("Skipping email")
logging.debug(f"to: {to}")
logging.debug(f"subject: {subject}")
logging.debug(f"body: {body}")
return
from_email = DEFAULT_EMAIL_FROM_ADDR
to_email = to
sub = subject
from_email = Email(email=DEFAULT_EMAIL_FROM_ADDR, name=DEFAULT_EMAIL_FROM)
to_email = To(email=to)
sub = Subject(subject=subject)
html_content = None
plain_text_content = None
if (html):
html_content = HtmlContent(body)
if html:
html_content = HtmlContent(content=body)
else:
plain_text_content = PlainTextContent(body)
plain_text_content = PlainTextContent(content=body)
mail = Mail(
from_email=from_email,
to_emails=to_email,
subject=sub,
html_content=html_content,
plain_text_content=plain_text_content
plain_text_content=plain_text_content,
)
try:
sg = sendgrid.SendGridAPIClient(EMAIL_API_KEY)
sg = sendgrid.SendGridAPIClient(api_key=EMAIL_API_KEY)
response = sg.send(mail)
if response.status_code != 202:
raise Exception(f'Failed to send email: {response.body}')
raise Exception(f"Failed to send email: {response.body}")
except Exception as ex:
logging.exception('Failed to send email.')
logging.exception("Failed to send email.")
def send_signup_email(to, subject, id, uuid, content):
message = render_to_string(
'webapp:signup_email.html', {
'url': f"https://{FRONTEND_URL}/signup/edit/{id}/{uuid}",
'content': markdown.markdown(content),
}
"webapp:signup_email.html",
{
"url": f"https://{FRONTEND_URL}/signup/edit/{id}/{uuid}",
"content": markdown.markdown(content),
},
)
return send_email(to, subject, message, True)