Skip to content

Commit

Permalink
Add more of the email sending code.
Browse files Browse the repository at this point in the history
For #4
  • Loading branch information
mblayman committed Jul 21, 2023
1 parent 44e8391 commit bd2c294
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
12 changes: 10 additions & 2 deletions journal/entries/jobs/send_mail.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.core import mail
from django.template.loader import render_to_string
from django.utils import timezone
from django_extensions.management.jobs import DailyJob

from journal.accounts.models import Account

from ..models import Entry


class Job(DailyJob):
help = "Sent mail to active accounts"
Expand All @@ -12,10 +15,15 @@ def execute(self):
accounts = Account.objects.active().select_related("user")
today = timezone.localdate()
for account in accounts:
# TODO: get *random* entry
entry = Entry.objects.filter(user=account.user).last()
context = {"entry": entry}
text_message = render_to_string("entries/email/prompt.txt", context)
html_message = render_to_string("entries/email/prompt.html", context)
mail.send_mail(
subject=f"It's {today:%A}, {today:%b}. {today:%-d}, how are you?",
message="Replace this message",
html_message="Replace this HTML message",
message=text_message,
html_message=html_message,
from_email="who is this from email",
recipient_list=[account.user.email],
)
23 changes: 20 additions & 3 deletions journal/entries/tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@

from journal.accounts.tests.factories import UserFactory
from journal.entries.jobs.send_mail import Job as SendMailJob
from journal.entries.tests.factories import EntryFactory


class TestSendMailJob:
@time_machine.travel("2023-07-19")
def test_send_email(self, mailoutbox):
"""An active account receives an email prompt."""
user = UserFactory()
body = "This is the entry.\n\nIt has newlines."
entry = EntryFactory(user=user, body=body)
job = SendMailJob()

job.execute()

assert len(mailoutbox) == 1
mail = mailoutbox[0]
# TODO: assert from_email
assert mail.to == [user.email]
assert mail.subject == "It's Wednesday, Jul. 19, how are you?"
# TODO: assert message
# TODO: assert html_message
# TODO: assert from_email
assert entry.body in mail.body # Test the text email.
html_message = mail.alternatives[0][0]
assert "<p>This is the entry.</p>\n\n<p>It has newlines.</p>" in html_message

def test_no_available_entries(self, mailoutbox):
"""The message indicates that a previous entry will appear once it exists."""
UserFactory()
job = SendMailJob()

job.execute()

assert len(mailoutbox) == 1
mail = mailoutbox[0]
assert "You have no entries yet!" in mail.body # Test the text email.
html_message = mail.alternatives[0][0]
assert "<p>You have no entries yet!" in html_message
1 change: 1 addition & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.humanize",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.sites",
Expand Down
21 changes: 21 additions & 0 deletions templates/entries/email/prompt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% load humanize %}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<p>How are you? Reply to this prompt to update your journal.</p>
{% if entry %}
<p>On your journey on {{ entry.when|date:"l, M. j, Y" }} ({{ entry.when|naturalday }}), you wrote:</p>

{{ entry.body|linebreaks}}
{% else %}
<p>You have no entries yet! As soon as you do, a random previous entry will appear in your prompt.</p>
{% endif %}
<p></p>
</body>
</html>


8 changes: 8 additions & 0 deletions templates/entries/email/prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% load humanize %}How are you? Reply to this prompt to update your journal.
{% if entry %}
On your journey on {{ entry.when|date:"l, M. j, Y" }} ({{ entry.when|naturalday }}), you wrote:

{{ entry.body }}
{% else %}
You have no entries yet! As soon as you do, a random previous entry will appear in your prompt.
{% endif %}

0 comments on commit bd2c294

Please sign in to comment.