Skip to content

Commit

Permalink
[test] confimation email threading
Browse files Browse the repository at this point in the history
  • Loading branch information
skandrigi committed Nov 23, 2024
1 parent 6e795dc commit ae238de
Showing 1 changed file with 39 additions and 70 deletions.
109 changes: 39 additions & 70 deletions hiss/shared/admin_functions.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,46 @@

import json
from io import BytesIO

import pyqrcode
from django.conf import settings
from django.core import mail
from django.template.loader import render_to_string
from django.utils import html

from application.models import Application
from application.apple_wallet import get_apple_wallet_pass_url

def send_creation_email(app: Application) -> None:
"""
Sends an email to the user informing them of their newly-created app.
:param app: The user's newly-created application
:return: None
from django.core.mail import get_connection, EmailMultiAlternatives
import threading

#create separate Threading class for mass emails
class MassEmailThread(threading.Thread):
def __init__(self, subject, text_content, html_content, from_email, recipient_list, connection):
threading.Thread.__init__(self)
self.subject = subject
self.text_content = text_content
self.html_content = html_content
self.from_email = from_email
self.recipient_list = recipient_list
self.connection = connection
self.result = 0

def run(self):
email = EmailMultiAlternatives(self.subject, self.text_content, self.from_email, self.recipient_list)
email.attach_alternative(self.html_content, "text/html")
try:
self.result = email.send(fail_silently=False, connection=self.connection)
except Exception as e:
print("Error sending email: ", e)
self.result = 0

def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None, connection=None):
"""
subject = f"We've received your application for {settings.EVENT_NAME}!"
template_name = "application/emails/created.html"
context = {
"first_name": app.first_name,
"event_name": settings.EVENT_NAME,
"organizer_name": settings.ORGANIZER_NAME,
"event_year": settings.EVENT_YEAR,
"organizer_email": settings.ORGANIZER_EMAIL,
}
app.user.send_html_email(template_name, context, subject)

def send_confirmation_email(app: Application) -> None:
"""
Sends a confirmation email to a user, which contains their QR code as well as additional event information.
:param app: The user's application
:type app: Application
:return: None
Sends each message in datatuple (subject, text_content, html_content, from_email, recipient_list).
Returns the number of emails sent.
"""
subject = f"HowdyHack: Important Day-of Information!"
email_template = "application/emails/confirmed.html"
connection = connection or get_connection(
username=user, password=password, fail_silently=fail_silently
)

if app.status == "E":
subject = f"HowdyHack Waitlist: Important Day-of Information!"
email_template = "application/emails/confirmed-waitlist.html"
threads = []

context = {
"first_name": app.first_name,
"event_name": settings.EVENT_NAME,
"organizer_name": settings.ORGANIZER_NAME,
"event_year": settings.EVENT_YEAR,
"organizer_email": settings.ORGANIZER_EMAIL,
"apple_wallet_url": get_apple_wallet_pass_url(app.user.email),
"meal_group": app.meal_group,
}
for subject, text, html, from_email, recipient in datatuple:
email_thread = MassEmailThread(subject, text, html, from_email, recipient, connection)
email_thread.start()
threads.append(email_thread)

html_msg = render_to_string(email_template, context)
msg = html.strip_tags(html_msg)
email = mail.EmailMultiAlternatives(
subject, msg, from_email=None, to=[app.user.email]
)
for thread in threads:
thread.join()

email.attach_alternative(html_msg, "text/html")
qr_content = json.dumps(
{
"first_name": app.first_name,
"last_name": app.last_name,
"email": app.user.email,
"university": app.school.name,
}
)
total_sent = sum(thread.result for thread in threads if thread.result)

qr_code = pyqrcode.create(qr_content)
qr_stream = BytesIO()
qr_code.png(qr_stream, scale=5)
email.attach("code.png", qr_stream.getvalue(), "text/png")

print(f"sending confirmation email to {app.user.email}")
email.send()
return total_sent

0 comments on commit ae238de

Please sign in to comment.