Skip to content

Commit

Permalink
Add records to db when email is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
dansahagian committed Sep 21, 2024
1 parent 46d1fd1 commit 4d07471
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
26 changes: 26 additions & 0 deletions fbsurvivor/core/migrations/0044_emaillogrecord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1.1 on 2024-09-21 20:52

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0043_delete_cachedboard"),
]

operations = [
migrations.CreateModel(
name="EmailLogRecord",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("subject", models.CharField(max_length=128)),
("email", models.CharField(max_length=128)),
],
),
]
22 changes: 6 additions & 16 deletions fbsurvivor/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
from django.db.models import Sum
from django.db.models.functions import Lower

from fbsurvivor import settings
from fbsurvivor.core.utils.emails import send_email


class Player(models.Model):
username = models.CharField(max_length=20, unique=True)
Expand All @@ -25,19 +22,6 @@ class Meta:
def __str__(self):
return f"{self.username}"

def save(self, *args, **kwargs):
pk = self.pk
super().save(*args, **kwargs)

if not pk:
ps = f"If you didn't sign up, please email {settings.CONTACT}"
login = f"{settings.DOMAIN}"
subject = "Survivor User Account"
recipients = [self.email]
message = f"You can login here:\n\n{login}\n\n{ps}"

send_email(subject, recipients, message)


class TokenHash(models.Model):
hash = models.CharField(max_length=128)
Expand Down Expand Up @@ -330,3 +314,9 @@ class Board(models.Model):
result_16 = models.CharField(max_length=1, null=True)
result_17 = models.CharField(max_length=1, null=True)
result_18 = models.CharField(max_length=1, null=True)


class EmailLogRecord(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
subject = models.CharField(max_length=128)
email = models.CharField(max_length=128)
18 changes: 18 additions & 0 deletions fbsurvivor/core/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from fbsurvivor import settings
from fbsurvivor.core.models import Player
from fbsurvivor.core.utils.emails import send_email


class PlayerService:
@staticmethod
def create(username: str, email: str):
player, created = Player.objects.get_or_create(username=username, email=email)

if created:
ps = f"If you didn't sign up, please email Dan at {settings.CONTACT}"
login = f"{settings.DOMAIN}"
subject = "Survivor User Account"
recipients = [email]
message = f"You can login here:\n\n{login}\n\n{ps}"

send_email(subject, recipients, message)
9 changes: 9 additions & 0 deletions fbsurvivor/core/utils/emails.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import smtplib
from email.mime.text import MIMEText

from fbsurvivor.core.models import EmailLogRecord
from fbsurvivor.settings import ENV, SMTP_PASSWORD, SMTP_SENDER, SMTP_SERVER, SMTP_USER


def log_email(subject: str, recipients: list[str]):
for recipient in recipients:
EmailLogRecord.objects.create(subject=subject, email=recipient)


def send_email(subject, recipients, message) -> None:
subject = f"🏈 {subject} 🏈"

if ENV == "dev":
print(f"\n\nSending Email to {len(recipients)} players...\n{subject}\n\n{message}\n\n")
log_email(subject, recipients)
return None

msg = MIMEText(message)
Expand All @@ -23,4 +30,6 @@ def send_email(subject, recipients, message) -> None:
finally:
conn.quit()

log_email(subject, recipients)

return None

0 comments on commit 4d07471

Please sign in to comment.