Skip to content

Commit

Permalink
Add the email sending core.
Browse files Browse the repository at this point in the history
For #4
  • Loading branch information
mblayman committed Jun 29, 2023
1 parent aae9f39 commit 9f1c891
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions journal/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
from django.dispatch import receiver


class AccountManager(models.Manager):
def active(self):
"""Get all the active accounts."""
qs = self.get_queryset()
return qs.filter(status__in=self.model.ACTIVE_STATUSES)


class Account(models.Model):
"""Account holds the user's state"""

Expand All @@ -14,6 +21,8 @@ class Status(models.IntegerChoices):
CANCELED = 4
TRIAL_EXPIRED = 5

ACTIVE_STATUSES = (Status.TRIALING, Status.ACTIVE, Status.EXEMPT)

user = models.OneToOneField(
"accounts.User",
on_delete=models.CASCADE,
Expand All @@ -24,6 +33,8 @@ class Status(models.IntegerChoices):
db_index=True,
)

objects = AccountManager()


class User(AbstractUser):
pass
Expand Down
3 changes: 3 additions & 0 deletions journal/accounts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ class Meta:
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = "accounts.User"

email = factory.Sequence(lambda n: f"user_{n}@testing.com")
username = factory.Sequence(lambda n: f"user_{n}")
14 changes: 14 additions & 0 deletions journal/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
from journal.accounts.models import Account
from journal.accounts.tests.factories import AccountFactory, UserFactory


class TestAccount:
def test_factory(self):
"""The factory produces a valid instance."""
account = AccountFactory()

assert account is not None
assert account.user is not None
assert account.status == account.Status.TRIALING

def test_active(self):
"""The active manager method returns active accounts."""
trialing = AccountFactory(status=Account.Status.TRIALING)
active = AccountFactory(status=Account.Status.ACTIVE)
exempt = AccountFactory(status=Account.Status.EXEMPT)
AccountFactory(status=Account.Status.CANCELED)
AccountFactory(status=Account.Status.TRIAL_EXPIRED)

accounts = Account.objects.active()

assert set(accounts) == {trialing, active, exempt}


class TestUser:
def test_factory(self):
Expand Down
Empty file.
19 changes: 19 additions & 0 deletions journal/entries/jobs/send_mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.core import mail
from django_extensions.management.jobs import DailyJob

from journal.accounts.models import Account


class Job(DailyJob):
help = "Sent mail to active accounts"

def execute(self):
accounts = Account.objects.active().select_related("user")
for account in accounts:
mail.send_mail(
subject="Replace this subject",
message="Replace this message",
html_message="Replace this HTML message",
from_email="who is this from email",
recipient_list=[account.user.email],
)
19 changes: 19 additions & 0 deletions journal/entries/tests/test_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from journal.accounts.tests.factories import UserFactory
from journal.entries.jobs.send_mail import Job as SendMailJob


class TestSendMailJob:
def test_send_email(self, mailoutbox):
"""An active account receives an email prompt."""
user = UserFactory()
job = SendMailJob()

job.execute()

assert len(mailoutbox) == 1
mail = mailoutbox[0]
assert mail.to == [user.email]
# TODO: assert subject
# TODO: assert message
# TODO: assert html_message
# TODO: assert from_email

0 comments on commit 9f1c891

Please sign in to comment.