sendNotifications: Use a template for the email message
This commit is contained in:
parent
927094ee41
commit
dc82bfc58a
@ -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())
|
||||
|
4
tasks/templates/tasks/notificationEmail.txt
Normal file
4
tasks/templates/tasks/notificationEmail.txt
Normal 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 %}
|
Loading…
Reference in New Issue
Block a user