aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSawyer Bergeron <sbergeron@iol.unh.edu>2020-10-05 16:13:03 -0400
committerSawyer Bergeron <sbergeron@iol.unh.edu>2020-10-05 16:13:54 -0400
commit0d20968698aa4a5fc58bad9ae30857df504e170c (patch)
tree615e00ef5090accde08e913e431057e11cf706ea /src
parent836ba6a22ece7590c203c837669b73259f02540a (diff)
Make emails send asynchronously (using celery job)
Change-Id: I9f4d5d05a0b72c883d667cf3910b3b318cbe82fa Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
Diffstat (limited to 'src')
-rw-r--r--src/notifier/manager.py29
-rw-r--r--src/notifier/models.py7
-rw-r--r--src/notifier/tasks.py18
3 files changed, 29 insertions, 25 deletions
diff --git a/src/notifier/manager.py b/src/notifier/manager.py
index 8ea8021..e2afdec 100644
--- a/src/notifier/manager.py
+++ b/src/notifier/manager.py
@@ -8,9 +8,8 @@
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import os
-from notifier.models import Notification, Emailed
+from notifier.models import Notification, Emailed, Email
-from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils import timezone
@@ -99,14 +98,8 @@ class NotificationHandler(object):
# render email template
message = render_to_string(template_name, context)
- # finally, send the email
- send_mail(
- "Your Booking is Ready",
- message,
- os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@laas-dashboard"),
- [user.userprofile.email_addr],
- fail_silently=False
- )
+ # finally, queue email for sending
+ Email.objects.create(title="Your Booking is Ready", message=message, recipient=user.userprofile.email_addr)
@classmethod
def email_booking_over(cls, booking):
@@ -124,13 +117,7 @@ class NotificationHandler(object):
message = render_to_string(template_name, context)
- send_mail(
- "Your Booking has Expired",
- message,
- os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@laas-dashboard"),
- [user.userprofile.email_addr],
- fail_silently=False
- )
+ Email.objects.create(title="Your Booking has Expired", message=message, recipient=user.userprofile.email_addr)
@classmethod
def email_booking_expiring(cls, booking):
@@ -148,13 +135,7 @@ class NotificationHandler(object):
message = render_to_string(template_name, context)
- send_mail(
- "Your Booking is Expiring",
- message,
- os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@laas-dashboard"),
- [user.userprofile.email_addr],
- fail_silently=False
- )
+ Email.objects.create(title="Your Booking is Expiring", message=message, recipient=user.userprofile.email_addr)
@classmethod
def task_updated(cls, task):
diff --git a/src/notifier/models.py b/src/notifier/models.py
index 0af748b..03e23b3 100644
--- a/src/notifier/models.py
+++ b/src/notifier/models.py
@@ -47,3 +47,10 @@ class Emailed(models.Model):
on_delete=models.CASCADE,
related_name="over_mail"
)
+
+
+class Email(models.Model):
+ sent = models.BooleanField(default=False)
+ title = models.CharField(max_length=150)
+ message = models.TextField()
+ recipient = models.CharField(max_length=150)
diff --git a/src/notifier/tasks.py b/src/notifier/tasks.py
index 474d64d..389750a 100644
--- a/src/notifier/tasks.py
+++ b/src/notifier/tasks.py
@@ -13,8 +13,11 @@ from celery import shared_task
from django.utils import timezone
from django.conf import settings
from booking.models import Booking
-from notifier.models import Emailed
+from notifier.models import Emailed, Email
from notifier.manager import NotificationHandler
+from django.core.mail import send_mail
+
+import os
@shared_task
@@ -33,3 +36,16 @@ def notify_expiring():
continue
NotificationHandler.notify_booking_expiring(booking)
Emailed.objects.create(almost_end_booking=booking)
+
+
+@shared_task
+def dispatch_emails():
+ for email in Email.objects.filter(sent=False):
+ email.sent = True
+ email.save()
+ send_mail(
+ email.title,
+ email.message,
+ os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@laas-dashboard"),
+ email.recipient,
+ fail_silently=False)