1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron and others.
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 ##############################################################################
10 from notifier.models import Notification
12 from django.core.mail import send_mail
13 from django.template.loader import render_to_string
16 class NotificationHandler(object):
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)
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)
31 def booking_notify(cls, booking, template, titles):
33 Creates a notification for a booking owner and collaborators
35 titles is a list - the first is the title for the owner's notification,
36 the last is the title for the collaborators'
38 owner_notif = Notification.objects.create(
40 content=render_to_string(
48 owner_notif.recipients.add(booking.owner.userprofile)
49 if not booking.collaborators.all().exists():
50 return # no collaborators - were done
52 collab_notif = Notification.objects.create(
54 content=render_to_string(
62 for c in booking.collaborators.all():
63 collab_notif.recipients.add(c.userprofile)
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)
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:
78 "title": task.type_str + " Message: ",
79 "content": task.message
82 # gather up all the other needed info
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) + "/"
89 # render email template
90 message = render_to_string(template_name, context)
92 # finally, send the email
94 "Your Booking is Ready",
96 os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
97 user.userprofile.email_addr,
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)
109 "user_name": user.userprofile.full_name,
112 "booking_url": os.environ.get("DASHBOARD_URL", "<Dashboard url>") + "/booking/detail/" + str(booking.id) + "/"
115 message = render_to_string(template_name, context)
118 "Your Booking has Expired",
120 os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
121 user.userprofile.email_addr,
126 def task_updated(cls, task):
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
132 if task.job.is_fulfilled():
133 cls.email_job_fulfilled(task.job)