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):
|
2022-01-24 23:37:04 -05:00
|
|
|
help = "Sends any notifications for upcoming and overdue tasks"
|
2020-12-03 12:31:39 -05:00
|
|
|
|
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):
|
2022-01-24 23:37:04 -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()
|
2022-01-24 23:37:04 -05:00
|
|
|
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:
|
2022-01-24 23:37:04 -05:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.ERROR(
|
|
|
|
f"Can't send email, user '{user}' is missing an email address"
|
|
|
|
)
|
|
|
|
)
|
2020-12-08 16:03:19 -05:00
|
|
|
continue
|
|
|
|
|
2022-01-24 23:37:04 -05:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS(
|
|
|
|
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(
|
2022-01-24 23:37:04 -05:00
|
|
|
subject=f"[CMS Tool Maintenance] {len(subscriptions)} tasks are upcoming or overdue!",
|
|
|
|
message=template.render({"subscriptions": subscriptions}).strip(),
|
|
|
|
from_email="adam@adamgoldsmith.name",
|
2020-12-08 16:03:19 -05:00
|
|
|
recipient_list=[user.email],
|
2022-01-24 23:37:04 -05:00
|
|
|
fail_silently=False,
|
2020-12-08 16:03:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-01-24 23:37:04 -05:00
|
|
|
self.stdout.write(
|
|
|
|
self.style.ERROR(
|
|
|
|
f"Failed to send mail for user '{user}': {type(e).__name__}: {e}"
|
|
|
|
)
|
|
|
|
)
|