From bd2c294b356239e82bf402f4271c151677e60669 Mon Sep 17 00:00:00 2001 From: Matt Layman Date: Fri, 21 Jul 2023 15:02:01 -0400 Subject: [PATCH] Add more of the email sending code. For #4 --- journal/entries/jobs/send_mail.py | 12 ++++++++++-- journal/entries/tests/test_jobs.py | 23 ++++++++++++++++++++--- project/settings.py | 1 + templates/entries/email/prompt.html | 21 +++++++++++++++++++++ templates/entries/email/prompt.txt | 8 ++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 templates/entries/email/prompt.html create mode 100644 templates/entries/email/prompt.txt diff --git a/journal/entries/jobs/send_mail.py b/journal/entries/jobs/send_mail.py index 4ee8a73..c6e075e 100644 --- a/journal/entries/jobs/send_mail.py +++ b/journal/entries/jobs/send_mail.py @@ -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" @@ -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], ) diff --git a/journal/entries/tests/test_jobs.py b/journal/entries/tests/test_jobs.py index c7cc896..db5fbb4 100644 --- a/journal/entries/tests/test_jobs.py +++ b/journal/entries/tests/test_jobs.py @@ -2,6 +2,7 @@ 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: @@ -9,14 +10,30 @@ class TestSendMailJob: 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 "

This is the entry.

\n\n

It has newlines.

" 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 "

You have no entries yet!" in html_message diff --git a/project/settings.py b/project/settings.py index 61816ed..5a478ad 100644 --- a/project/settings.py +++ b/project/settings.py @@ -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", diff --git a/templates/entries/email/prompt.html b/templates/entries/email/prompt.html new file mode 100644 index 0000000..b17e7c0 --- /dev/null +++ b/templates/entries/email/prompt.html @@ -0,0 +1,21 @@ +{% 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|linebreaks}} + {% else %} +

You have no entries yet! As soon as you do, a random previous entry will appear in your prompt.

+ {% endif %} +

+ + + + diff --git a/templates/entries/email/prompt.txt b/templates/entries/email/prompt.txt new file mode 100644 index 0000000..22c2cf1 --- /dev/null +++ b/templates/entries/email/prompt.txt @@ -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 %}