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(template, context={
45 owner_notif.recipients.add(booking.owner)
46 if not booking.collaborators.all().exists():
47 return # no collaborators - were done
49 collab_notif = Notification.objects.create(
51 content=render_to_string(template, context={
56 for c in booking.collaborators.all():
57 collab_notif.recipients.add(c)
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)
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
74 # gather up all the other needed info
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) + "/"
81 # render email template
82 message = render_to_string(template_name, context)
84 # finally, send the email
86 "Your Booking is Ready",
88 os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
89 user.userprofile.email_addr,
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)
101 "user_name": user.userprofile.full_name,
104 "booking_url": os.environ.get("DASHBOARD_URL", "<Dashboard url>") + "/booking/detail/" + str(booking.id) + "/"
107 message = render_to_string(template_name, context)
110 "Your Booking has Expired",
112 os.environ.get("DEFAULT_FROM_EMAIL", "opnfv@pharos-dashboard"),
113 user.userprofile.email_addr,
118 def task_updated(cls, task):
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
124 if task.job.is_fulfilled():
125 cls.email_job_fulfilled(task.job)