2020-12-29 16:26:48 +00:00
|
|
|
from django.dispatch import receiver
|
2020-12-31 21:15:52 +00:00
|
|
|
from django.utils.timezone import localtime, now
|
2020-12-29 16:26:48 +00:00
|
|
|
|
|
|
|
from cronhandler.signals import cron
|
|
|
|
|
|
|
|
from .models import Notification
|
|
|
|
|
|
|
|
@receiver(cron)
|
|
|
|
def send_notifications(sender, **kwargs):
|
|
|
|
returns = []
|
|
|
|
for notification in Notification.objects.all():
|
|
|
|
for datetime in notification.notificationdatetimeschedule_set.all():
|
2020-12-31 21:15:52 +00:00
|
|
|
if not datetime.sent and datetime.datetime <= localtime(now()):
|
2020-12-29 16:26:48 +00:00
|
|
|
try:
|
|
|
|
returns.append(notification.send())
|
|
|
|
datetime.sent = True
|
|
|
|
datetime.save()
|
|
|
|
except:
|
|
|
|
pass # TODO: Implement some sort of error logging / admin notification
|
|
|
|
for daily in notification.notificationdailyschedule_set.all():
|
2020-12-31 21:15:52 +00:00
|
|
|
if ((not daily.last_sent) or daily.last_sent < localtime(now()).date()) and daily.time <= localtime(now()).time():
|
2020-12-29 16:26:48 +00:00
|
|
|
try:
|
|
|
|
returns.append(notification.send())
|
2020-12-31 21:15:52 +00:00
|
|
|
daily.last_sent = localtime(now()).date()
|
2020-12-29 16:26:48 +00:00
|
|
|
daily.save()
|
|
|
|
except:
|
|
|
|
pass # TODO: See above
|
|
|
|
|
2021-01-02 09:13:07 +00:00
|
|
|
return returns
|
|
|
|
|