Rewrite Notification subsystem
[laas.git] / src / notifier / manager.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import os
10 from notifier.models import Notification
11
12 from django.core.mail import send_mail
13 from django.template.loader import render_to_string
14
15
16 class NotificationHandler(object):
17
18     @classmethod
19     def notify_new_booking(cls, booking):
20         template = "notifier/new_booking.html"
21         titles = ["You have a new Booking", "You have been added to a Booking"]
22         cls.booking_notify(booking, template, titles)
23
24     @classmethod
25     def notify_booking_end(cls, booking):
26         template = "notifier/end_booking.html"
27         titles = ["Your booking has ended", "A booking you collaborate on has ended"]
28         cls.booking_notify(booking, template, titles)
29
30     @classmethod
31     def booking_notify(cls, booking, template, titles):
32         """
33         Creates a notification for a booking owner and collaborators
34         using the template.
35         titles is a list - the first is the title for the owner's notification,
36             the last is the title for the collaborators'
37         """
38         owner_notif = Notification.objects.create(
39                 title=titles[0],
40                 content=render_to_string(template, context={
41                     "booking": booking,
42                     "owner": True
43                     })
44                 )
45         owner_notif.recipients.add(booking.owner)
46         if not booking.collaborators.all().exists():
47             return  # no collaborators - were done
48
49         collab_notif = Notification.objects.create(
50                 title=titles[-1],
51                 content=render_to_string(template, context={
52                     "booking": booking,
53                     "owner": False
54                     })
55                 )
56         for c in booking.collaborators.all():
57             collab_notif.recipients.add(c)
58
59     @classmethod
60     def email_job_fulfilled(cls, job):
61         template_name = "notifier/email_fulfilled.txt"
62         all_tasks = job.get_tasklist()
63         users = list(job.booking.collaborators.all())
64         users.append(job.booking.owner)
65         for user in users:
66             user_tasklist = []
67             # gather up all the relevant messages from the lab
68             for task in all_tasks:
69                 if (not hasattr(task, "user")) or task.user == user:
70                     user_tasklist.append({
71                         "title": task.type_str + " Message: ",
72                         "content": task.message
73                         })
74             # gather up all the other needed info
75             context = {
76                     "user_name": user.userprofile.full_name,
77                     "messages": user_tasklist,
78                     "booking_url": os.environ.get("DASHBOARD_URL", "<Dashboard url>") + "/booking/detail/" + str(job.booking.id) + "/"
79                     }
80
81             # render email template
82             message = render_to_string(template_name, context)
83
84             # finally, send the email
85             send_mail(
86                     "Your Booking is Ready",
87                     message,
88                     os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
89                     user.userprofile.email_addr,
90                     fail_silently=False
91                     )
92
93     @classmethod
94     def email_booking_over(cls, booking):
95         template_name = "notifier/email_ended.txt"
96         hostnames = [host.template.resource.name for host in booking.resource.hosts.all()]
97         users = list(booking.collaborators.all())
98         users.append(booking.owner)
99         for user in users:
100             context = {
101                     "user_name": user.userprofile.full_name,
102                     "booking": booking,
103                     "hosts": hostnames,
104                     "booking_url": os.environ.get("DASHBOARD_URL", "<Dashboard url>") + "/booking/detail/" + str(booking.id) + "/"
105                     }
106
107             message = render_to_string(template_name, context)
108
109             send_mail(
110                     "Your Booking has Expired",
111                     message,
112                     os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
113                     user.userprofile.email_addr,
114                     fail_silently=False
115                     )
116
117     @classmethod
118     def task_updated(cls, task):
119         """
120         called every time a lab updated info about a task.
121         currently only checks if the job is now done so I can send an email,
122         may add more functionality later
123         """
124         if task.job.is_fulfilled():
125             cls.email_job_fulfilled(task.job)