Skip to content

Commit

Permalink
Revert email changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ericli18 committed Nov 25, 2024
1 parent c4bea7e commit b0b1494
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 98 deletions.
62 changes: 22 additions & 40 deletions hiss/application/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,6 @@
from application.models import Application
from application.apple_wallet import get_apple_wallet_pass_url

import threading
from django.core.mail import EmailMessage

#create separate threading class for confirmation email since it has a QR code

class EmailQRThread(threading.Thread):
def __init__(self, subject, msg, html_msg, recipient_email, qr_stream):
self.subject = subject
self.msg = msg
self.html_msg = html_msg
self.recipient_email = recipient_email
self.qr_stream = qr_stream
threading.Thread.__init__(self)

def run(self):
qr_code = pyqrcode.create(self.qr_content)
qr_stream = BytesIO()
qr_code.png(qr_stream, scale=5)

email = mail.EmailMultiAlternatives(
self.subject, self.msg, from_email=None, to=[self.recipient_email]
)
email.attach_alternative(self.html_msg, "text/html")
email.attach("code.png", self.qr_stream.getvalue(), "text/png")

# if above code is defined directly in function, it will run synchronously
# therefore need to directly define in threading class to run asynchronously

email.send()

def send_creation_email(app: Application) -> None:
"""
Expand All @@ -56,11 +27,8 @@ def send_creation_email(app: Application) -> None:
"organizer_email": settings.ORGANIZER_EMAIL,
}

# send_html_email is threaded from the User class
# see user/models.py

app.user.send_html_email(template_name, context, subject)


def send_confirmation_email(app: Application) -> None:
"""
Expand All @@ -69,19 +37,30 @@ def send_confirmation_email(app: Application) -> None:
:type app: Application
:return: None
"""
subject = f"TAMUhack: Important Day-Of Information"

subject = f"HowdyHack: Important Day-of Information!"
email_template = "application/emails/confirmed.html"

if app.status == "E":
subject = f"HowdyHack Waitlist: Important Day-of Information!"
email_template = "application/emails/confirmed-waitlist.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,
"apple_wallet_url": get_apple_wallet_pass_url(app.user.email),
"event_date_text": settings.EVENT_DATE_TEXT,
# "apple_wallet_url": get_apple_wallet_pass_url(app.user.email),
"apple_wallet_url": "ASDFASDF",
"meal_group": app.meal_group,
}
html_msg = render_to_string(email_template, context)
plain_msg = html.strip_tags(html_msg)
msg = html.strip_tags(html_msg)
email = mail.EmailMultiAlternatives(
subject, msg, from_email=None, to=[app.user.email]
)
email.attach_alternative(html_msg, "text/html")

qr_content = json.dumps(
{
Expand All @@ -91,6 +70,9 @@ def send_confirmation_email(app: Application) -> None:
"university": app.school.name,
}
)

email_thread = EmailQRThread(subject, plain_msg, html_msg, app.user.email, qr_content)
email_thread.start()
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()
18 changes: 18 additions & 0 deletions hiss/application/migrations/0027_auto_20241125_1435.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.13 on 2024-11-25 20:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('application', '0026_auto_20241119_1945'),
]

operations = [
migrations.AlterField(
model_name='application',
name='wants_team',
field=models.CharField(choices=[('Friend', 'From a friend'), ('Tabling', 'Tabling outside Zachry'), ('Howdy Week', 'From Howdy Week'), ('Yard Sign', 'Yard sign'), ('Social Media', 'Social media'), ('Student Orgs', 'Though another student org'), ('TH Organizer', 'From a TAMUhack organizer'), ('ENGR Newsletter', 'From the TAMU Engineering Newsletter'), ('MLH', 'Major League Hacking (MLH)'), ('Attended Before', "I've attended TAMUhack before")], max_length=16, verbose_name='How did you hear about TAMUhack?'),
),
]
55 changes: 18 additions & 37 deletions hiss/shared/admin_functions.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
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):
def send_mass_html_mail(
datatuple, fail_silently=False, user=None, password=None, connection=None
):
"""
Sends each message in datatuple (subject, text_content, html_content, from_email, recipient_list).
Returns the number of emails sent.
Given a datatuple of (subject, text_content, html_content, from_email,
recipient_list), sends each message to each recipient list. Returns the
number of emails sent.
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
If auth_user and auth_password are set, they're used to log in.
If auth_user is None, the EMAIL_HOST_USER setting is used.
If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
(from <a href="https://stackoverflow.com/a/10215091">this StackOverflow answer</a>
"""
connection = connection or get_connection(
username=user, password=password, fail_silently=fail_silently
)

threads = []

messages = []
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)

for thread in threads:
thread.join()

total_sent = sum(thread.result for thread in threads if thread.result)

return total_sent
message = EmailMultiAlternatives(subject, text, from_email, recipient)
message.attach_alternative(html, "text/html")
messages.append(message)
return connection.send_messages(messages)
25 changes: 4 additions & 21 deletions hiss/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.template.loader import render_to_string
from django.utils import html
from rest_framework.authtoken.models import Token
import threading


class EmailUserManager(auth_models.UserManager):
"""
Expand Down Expand Up @@ -36,22 +36,6 @@ def create_superuser(
extra_fields.setdefault("is_active", True)
return self._create_user(email, password, **extra_fields)

class EmailThread(threading.Thread):
def __init__(self, subject, plain_message, recipient_email, html_message):
self.subject = subject
self.plain_message = plain_message
self.recipient_email = recipient_email
self.html_message = html_message
threading.Thread.__init__(self)

def run(self):
mail.send_mail(
subject=self.subject,
message=self.plain_message,
from_email= settings.ORGANIZER_EMAIL,
recipient_list=[self.recipient_email],
html_message=self.html_message,
)

class User(auth_models.AbstractUser):
"""
Expand Down Expand Up @@ -90,9 +74,8 @@ class User(auth_models.AbstractUser):
def send_html_email(self, template_name, context, subject):
"""Send an HTML email to the user."""
html_msg = render_to_string(template_name, context)
plain_msg = html.strip_tags(html_msg)
email_thread = EmailThread(subject, plain_msg, self.email, html_msg)
email_thread.start()
msg = html.strip_tags(html_msg)
self.email_user(subject, msg, None, html_message=html_msg)


@receiver(post_save, sender=settings.AUTH_USER_MODEL)
Expand All @@ -111,4 +94,4 @@ def create_auth_token(
"""
if created:
# This user was just created, they need a new Token!
Token.objects.create(user=instance)
Token.objects.create(user=instance)

0 comments on commit b0b1494

Please sign in to comment.