Lab as a Service 2.0
[laas.git] / src / notifier / manager.py
1 ##############################################################################
2 # Copyright (c) 2018 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
10 from booking.models import *
11 from notifier.models import Notifier, MetaBooking, LabMessage
12 from django.utils import timezone
13 from datetime import timedelta
14 from django.template import Template, Context
15 from account.models import UserProfile
16
17 from django.db import models
18
19 class NotifyPeriodic(object):
20     def task():
21         bookings_new = Booking.objects.filter(metabooking__isnull=True)
22         bookings_old = Booking.objects.filter(end__lte=timezone.now() + timedelta(hours=24)).filter(metabooking__ended_notified=False)
23
24         for booking in bookings_old:
25             metabooking = booking.metabooking
26             if booking.end <= timezone.now() + timedelta(hours=24):
27                 if not metabooking.ending_notified:
28                     Notify().notify(Notify.TOCLEAN, booking)
29                     metabooking.ending_notified = True
30                     metabooking.save()
31             if booking.end <= timezone.now():
32                 metabooking = booking.metabooking
33                 if not metabooking.ended_notified:
34                     Notify().notify(Notify.CLEANED, booking)
35                     metabooking.ended_notified = True
36                     metabooking.save()
37
38         for booking in bookings_new:
39             metabooking = MetaBooking()
40             metabooking.booking = booking
41             metabooking.created_notified = True
42             metabooking.save()
43
44             Notify().notify(Notify.CREATED, booking)
45
46
47 class Notify(object):
48
49     CREATED = "created"
50     TOCLEAN = "toclean"
51     CLEANED = "cleaned"
52
53     TITLES = {}
54     TITLES["created"] = "Your booking has been confirmed"
55     TITLES["toclean"] = "Your booking is ending soon"
56     TITLES["cleaned"] = "Your booking has ended"
57
58     """
59     Lab message is provided with the following context elements:
60     * if is for owner or for collaborator (if owner)
61     * recipient username (<owner, collaborator>.username)
62     * recipient full name (<owner, collaborator>.userprofile.full_name)
63     * booking it pertains to (booking)
64     * status message should convey (currently "created", "toclean" and "cleaned" as strings)
65     It should be a django template that can be rendered with these context elements
66     and should generally use all of them in one way or another.
67     It should be applicable to email, the web based general view, and should be scalable for
68     all device formats across those mediums.
69     """
70     def notify(self, notifier_type, booking):
71         template = Template(LabMessage.objects.filter(lab=booking.lab).first().msg)
72
73         context = {}
74         context["owner"] = booking.owner
75         context["notify_type"] = notifier_type
76         context["booking"] = booking
77         message = template.render(Context(context))
78         notifier = Notifier()
79         notifier.title = self.TITLES[notifier_type]
80         notifier.content = message
81         notifier.user = booking.owner.userprofile
82         notifier.sender = str(booking.lab)
83         notifier.save()
84         notifier.send()
85
86
87         context["owner"] = False
88
89         for user in booking.collaborators.all():
90             context["collaborator"] = user
91             message = template.render(Context(context))
92             notifier = Notifier()
93             notifier.title = self.TITLES[notifier_type]
94             notifier.content = message
95             notifier.user = UserProfile.objects.get(user=user)
96             notifier.sender = str(booking.lab)
97             notifier.save()
98             notifier.send()