2020-12-07 18:02:46 -05:00
|
|
|
from itertools import groupby
|
2020-12-03 12:31:39 -05:00
|
|
|
|
2020-12-08 16:02:08 -05:00
|
|
|
from django.core.mail import send_mail
|
2020-12-03 12:31:39 -05:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2020-12-08 16:02:08 -05:00
|
|
|
from django.template import loader
|
|
|
|
|
2020-12-17 11:47:22 -05:00
|
|
|
from tasks.models import Tool, Task, Event, GroupToolSubscription, GroupTaskSubscription
|
2020-12-03 12:31:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Sends any notifications for upcoming and overdue tasks'
|
|
|
|
|
2020-12-17 11:47:22 -05:00
|
|
|
def _active_task_subscriptions(self):
|
|
|
|
for subscription in GroupTaskSubscription.objects.all():
|
|
|
|
if subscription.should_remind:
|
|
|
|
yield subscription
|
|
|
|
|
|
|
|
for tool_subscription in GroupToolSubscription.objects.all():
|
|
|
|
for subscription in tool_subscription.get_task_subscriptions():
|
|
|
|
if subscription.should_remind:
|
|
|
|
yield subscription
|
|
|
|
|
|
|
|
def _expand_group_subscriptions_to_users(self, subscriptions):
|
|
|
|
out = {}
|
|
|
|
for subscription in subscriptions:
|
|
|
|
for user in subscription.group.user_set.all():
|
|
|
|
if user not in out:
|
|
|
|
out[user] = []
|
|
|
|
out[user].append(subscription)
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: sorted(subscriptions, key=lambda r: r.task.next_recurrence)
|
|
|
|
for user, subscriptions in out.items()
|
|
|
|
}
|
2020-12-07 18:02:46 -05:00
|
|
|
|
2020-12-03 12:31:39 -05:00
|
|
|
def handle(self, *args, **options):
|
2020-12-17 11:47:22 -05:00
|
|
|
template = loader.get_template('tasks/notificationEmail.txt.dtl')
|
2020-12-08 16:02:08 -05:00
|
|
|
|
2020-12-17 11:47:22 -05:00
|
|
|
group_subscriptions = self._active_task_subscriptions()
|
|
|
|
subscriptions_per_user = self._expand_group_subscriptions_to_users(group_subscriptions)
|
2020-12-07 18:02:46 -05:00
|
|
|
|
2020-12-17 11:47:22 -05:00
|
|
|
for user, subscriptions in subscriptions_per_user.items():
|
2020-12-08 16:03:19 -05:00
|
|
|
if not user.email:
|
|
|
|
self.stdout.write(self.style.ERROR(
|
|
|
|
f"Can't send email, user '{user}' is missing an email address"))
|
|
|
|
continue
|
|
|
|
|
2020-12-07 18:02:46 -05:00
|
|
|
self.stdout.write(self.style.SUCCESS(
|
2020-12-17 11:47:22 -05:00
|
|
|
f'Sending notification for {len(subscriptions)} task(s) to {user}'))
|
2020-12-07 18:02:46 -05:00
|
|
|
|
2020-12-08 16:03:19 -05:00
|
|
|
try:
|
|
|
|
send_mail(
|
2020-12-17 11:47:22 -05:00
|
|
|
subject=f'[CMS Tool Maintenance] {len(subscriptions)} tasks are upcoming or overdue!',
|
|
|
|
message=template.render({'subscriptions': subscriptions}).strip(),
|
2020-12-08 16:03:19 -05:00
|
|
|
from_email='adam@adamgoldsmith.name',
|
|
|
|
recipient_list=[user.email],
|
|
|
|
fail_silently=False
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
self.stdout.write(self.style.ERROR(
|
|
|
|
f"Failed to send mail for user '{user}': {type(e).__name__}: {e}"))
|