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
hudaifasaleh committed Jun 30, 2023
1 parent 4c6092f commit 75e6f8a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
9 changes: 9 additions & 0 deletions dailyInbox/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class User(AbstractUser):
pass


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


class Account(models.Model):
"""Account holds the users state"""

Expand All @@ -18,8 +25,10 @@ class Status(models.IntegerChoices):
CANCELED = 4
TRIAL_EXPIRED = 5

ACTIVE_STATUS = (Status.TRAILING, Status.ACTIVE, Status.EXEMPT)
user = models.OneToOneField("accounts.User", on_delete=models.CASCADE)
status = models.IntegerField(choices=Status.choices, default=Status.TRAILING, db_index=True)
objects = AccountManager()


@receiver(post_save, sender=User)
Expand Down
3 changes: 3 additions & 0 deletions dailyInbox/accounts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ 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}")


@factory.django.mute_signals(post_save)
class AccountFactory(factory.django.DjangoModelFactory):
Expand Down
13 changes: 12 additions & 1 deletion dailyInbox/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dailyInbox.accounts.models import Account
from dailyInbox.accounts.tests.factories import UserFactory, AccountFactory


Expand All @@ -8,11 +9,21 @@ def test_factory(self):
assert account.user is not None
assert account.status == account.Status.TRAILING

def test_active(self):
"""the active manager method returns active accounts"""
trailing = AccountFactory(status=Account.Status.TRAILING)
active = AccountFactory(status=Account.Status.ACTIVE)
exempt = AccountFactory(status=Account.Status.EXEMPT)
AccountFactory(status=Account.Status.CANCELED)
AccountFactory(status=Account.Status.TRIAL_EXPIRED)

accounts = set(Account.objects.active())
assert accounts == {trailing, active, exempt}


class TestUser:
def test_factory(self):
"""the factory produces a valid instance"""
user = UserFactory()
assert user is not None
assert user.account is not None

8 changes: 0 additions & 8 deletions dailyInbox/entries/jobs.py

This file was deleted.

Empty file.
18 changes: 18 additions & 0 deletions dailyInbox/entries/jobs/send_mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.core import mail
from django_extensions.management.jobs import DailyJob

from dailyInbox.accounts.models import Account


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

def execute(self):
accounts = Account.objects.active()
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],
)
16 changes: 13 additions & 3 deletions dailyInbox/entries/tests/test_jobs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from dailyInbox.entries.jobs import SendMailJob
from dailyInbox.accounts.tests.factories import UserFactory
from dailyInbox.entries.jobs.send_mail import Job as SendMailJob


class TestMailJob:
def test_send_email(self):
class TestSendMailJob:
def test_send_email(self, mailoutbox):
"""An active account receive 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 75e6f8a

Please sign in to comment.