Hides expired bookings in the "My Bookings" Page
[pharos-tools.git] / dashboard / 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(
41                 template,
42                 context={
43                     "booking": booking,
44                     "owner": True
45                 }
46             )
47         )
48         owner_notif.recipients.add(booking.owner.userprofile)
49         if not booking.collaborators.all().exists():
50             return  # no collaborators - were done
51
52         collab_notif = Notification.objects.create(
53             title=titles[-1],
54             content=render_to_string(
55                 template,
56                 context={
57                     "booking": booking,
58                     "owner": False
59                 }
60             )
61         )
62         for c in booking.collaborators.all():
63             collab_notif.recipients.add(c.userprofile)
64
65     @classmethod
66     def email_job_fulfilled(cls, job):
67         template_name = "notifier/email_fulfilled.txt"
68         all_tasks = job.get_tasklist()
69         users = list(job.booking.collaborators.all())
70         users.append(job.booking.owner)
71         for user in users:
72             user_tasklist = []
73             # gather up all the relevant messages from the lab
74             for task in all_tasks:
75                 if (not hasattr(task, "user")) or task.user == user:
76                     user_tasklist.append(
77                         {
78                             "title": task.type_str() + " Message: ",
79                             "content": task.message
80                         }
81                     )
82             # gather up all the other needed info
83             context = {
84                 "user_name": user.userprofile.full_name,
85                 "messages": user_tasklist,
86                 "booking_url": os.environ.get("DASHBOARD_URL", "<Dashboard url>") + "/booking/detail/" + str(job.booking.id) + "/"
87             }
88
89             # render email template
90             message = render_to_string(template_name, context)
91
92             # finally, send the email
93             send_mail(
94                 "Your Booking is Ready",
95                 message,
96                 os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
97                 [user.userprofile.email_addr],
98                 fail_silently=False
99             )
100
101     @classmethod
102     def email_booking_over(cls, booking):
103         template_name = "notifier/email_ended.txt"
104         hostnames = [host.template.resource.name for host in booking.resource.hosts.all()]
105         users = list(booking.collaborators.all())
106         users.append(booking.owner)
107         for user in users:
108             context = {
109                 "user_name": user.userprofile.full_name,
110                 "booking": booking,
111                 "hosts": hostnames,
112                 "booking_url": os.environ.get("DASHBOARD_URL", "<Dashboard url>") + "/booking/detail/" + str(booking.id) + "/"
113             }
114
115             message = render_to_string(template_name, context)
116
117             send_mail(
118                 "Your Booking has Expired",
119                 message,
120                 os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
121                 user.userprofile.email_addr,
122                 fail_silently=False
123             )
124
125     @classmethod
126     def task_updated(cls, task):
127         """
128         called every time a lab updated info about a task.
129         currently only checks if the job is now done so I can send an email,
130         may add more functionality later
131         """
132         if task.job.is_fulfilled():
133             cls.email_job_fulfilled(task.job)