From dc82bfc58ac2887e88d3cb2b21e29c7b59f07543 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 8 Dec 2020 16:02:08 -0500 Subject: [PATCH] sendNotifications: Use a template for the email message --- tasks/management/commands/sendNotifications.py | 15 ++++++--------- tasks/templates/tasks/notificationEmail.txt | 4 ++++ 2 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 tasks/templates/tasks/notificationEmail.txt diff --git a/tasks/management/commands/sendNotifications.py b/tasks/management/commands/sendNotifications.py index 69cf37c..d5648bc 100644 --- a/tasks/management/commands/sendNotifications.py +++ b/tasks/management/commands/sendNotifications.py @@ -3,7 +3,10 @@ import smtplib from email.message import EmailMessage from itertools import groupby +from django.core.mail import send_mail from django.core.management.base import BaseCommand, CommandError +from django.template import loader + from tasks.models import Tool, Task, Event, Reminder @@ -25,12 +28,9 @@ class Command(BaseCommand): if reminder.should_remind: 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): + template = loader.get_template('tasks/notificationEmail.txt') + reminders_per_user = { user: sorted(reminders, key=lambda r: r.task.next_recurrence) 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( 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( user.email, f'[CMS Tool Maintenance] {len(reminders)} tasks are upcoming or overdue!', - contents) + template.render({'reminders': reminders}).strip()) diff --git a/tasks/templates/tasks/notificationEmail.txt b/tasks/templates/tasks/notificationEmail.txt new file mode 100644 index 0000000..85ec367 --- /dev/null +++ b/tasks/templates/tasks/notificationEmail.txt @@ -0,0 +1,4 @@ +The following tasks are upcoming or overdue: +{% for reminder in reminders %} + - {{ reminder.task.name }} {{ reminder.task.next_recurrence|date }} +{% endfor %}