diff --git a/fbsurvivor/core/migrations/0044_emaillogrecord.py b/fbsurvivor/core/migrations/0044_emaillogrecord.py new file mode 100644 index 0000000..a026ef5 --- /dev/null +++ b/fbsurvivor/core/migrations/0044_emaillogrecord.py @@ -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)), + ], + ), + ] diff --git a/fbsurvivor/core/models.py b/fbsurvivor/core/models.py index 049bac0..5a23317 100644 --- a/fbsurvivor/core/models.py +++ b/fbsurvivor/core/models.py @@ -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) @@ -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) @@ -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) diff --git a/fbsurvivor/core/services.py b/fbsurvivor/core/services.py new file mode 100644 index 0000000..6cda638 --- /dev/null +++ b/fbsurvivor/core/services.py @@ -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) diff --git a/fbsurvivor/core/utils/emails.py b/fbsurvivor/core/utils/emails.py index 4df21d3..507f0c6 100644 --- a/fbsurvivor/core/utils/emails.py +++ b/fbsurvivor/core/utils/emails.py @@ -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) @@ -23,4 +30,6 @@ def send_email(subject, recipients, message) -> None: finally: conn.quit() + log_email(subject, recipients) + return None