sendNotifications: Use a template for the email message

This commit is contained in:
Adam Goldsmith 2020-12-08 16:02:08 -05:00
parent 927094ee41
commit dc82bfc58a
2 changed files with 10 additions and 9 deletions

View File

@ -3,7 +3,10 @@ import smtplib
from email.message import EmailMessage from email.message import EmailMessage
from itertools import groupby from itertools import groupby
from django.core.mail import send_mail
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.template import loader
from tasks.models import Tool, Task, Event, Reminder from tasks.models import Tool, Task, Event, Reminder
@ -25,12 +28,9 @@ class Command(BaseCommand):
if reminder.should_remind: if reminder.should_remind:
yield reminder yield reminder
def _format_reminder_lines(self, reminders):
for reminder in reminders:
next_date = reminder.task.next_recurrence.strftime('%Y-%m-%d')
yield f" - {reminder.task.name} - {next_date}"
def handle(self, *args, **options): def handle(self, *args, **options):
template = loader.get_template('tasks/notificationEmail.txt')
reminders_per_user = { reminders_per_user = {
user: sorted(reminders, key=lambda r: r.task.next_recurrence) user: sorted(reminders, key=lambda r: r.task.next_recurrence)
for user, reminders in groupby(self._active_reminders(), lambda r: r.user) for user, reminders in groupby(self._active_reminders(), lambda r: r.user)
@ -40,10 +40,7 @@ class Command(BaseCommand):
self.stdout.write(self.style.SUCCESS( self.stdout.write(self.style.SUCCESS(
f'Sending notification for {len(reminders)} task(s) to {user}')) f'Sending notification for {len(reminders)} task(s) to {user}'))
contents = "The following tasks are upcoming or overdue:\n\n" + \
('\n'.join(self._format_reminder_lines(reminders)))
self._send_email( self._send_email(
user.email, user.email,
f'[CMS Tool Maintenance] {len(reminders)} tasks are upcoming or overdue!', f'[CMS Tool Maintenance] {len(reminders)} tasks are upcoming or overdue!',
contents) template.render({'reminders': reminders}).strip())

View File

@ -0,0 +1,4 @@
The following tasks are upcoming or overdue:
{% for reminder in reminders %}
- {{ reminder.task.name }} {{ reminder.task.next_recurrence|date }}
{% endfor %}