From 25275685e9a735e51fae8b1a936ba5733f6fb770 Mon Sep 17 00:00:00 2001 From: Parker Berberian Date: Wed, 10 Oct 2018 16:06:47 -0400 Subject: Lab as a Service 2.0 See changes here: https://wiki.opnfv.org/display/INF/Pharos+Laas Change-Id: I59ada5f98e70a28d7f8c14eab3239597e236ca26 Signed-off-by: Sawyer Bergeron Signed-off-by: Parker Berberian --- dashboard/src/notifier/manager.py | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 dashboard/src/notifier/manager.py (limited to 'dashboard/src/notifier/manager.py') diff --git a/dashboard/src/notifier/manager.py b/dashboard/src/notifier/manager.py new file mode 100644 index 0000000..a705d00 --- /dev/null +++ b/dashboard/src/notifier/manager.py @@ -0,0 +1,98 @@ +############################################################################## +# Copyright (c) 2018 Sawyer Bergeron and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from booking.models import * +from notifier.models import Notifier, MetaBooking, LabMessage +from django.utils import timezone +from datetime import timedelta +from django.template import Template, Context +from account.models import UserProfile + +from django.db import models + +class NotifyPeriodic(object): + def task(): + bookings_new = Booking.objects.filter(metabooking__isnull=True) + bookings_old = Booking.objects.filter(end__lte=timezone.now() + timedelta(hours=24)).filter(metabooking__ended_notified=False) + + for booking in bookings_old: + metabooking = booking.metabooking + if booking.end <= timezone.now() + timedelta(hours=24): + if not metabooking.ending_notified: + Notify().notify(Notify.TOCLEAN, booking) + metabooking.ending_notified = True + metabooking.save() + if booking.end <= timezone.now(): + metabooking = booking.metabooking + if not metabooking.ended_notified: + Notify().notify(Notify.CLEANED, booking) + metabooking.ended_notified = True + metabooking.save() + + for booking in bookings_new: + metabooking = MetaBooking() + metabooking.booking = booking + metabooking.created_notified = True + metabooking.save() + + Notify().notify(Notify.CREATED, booking) + + +class Notify(object): + + CREATED = "created" + TOCLEAN = "toclean" + CLEANED = "cleaned" + + TITLES = {} + TITLES["created"] = "Your booking has been confirmed" + TITLES["toclean"] = "Your booking is ending soon" + TITLES["cleaned"] = "Your booking has ended" + + """ + Lab message is provided with the following context elements: + * if is for owner or for collaborator (if owner) + * recipient username (.username) + * recipient full name (.userprofile.full_name) + * booking it pertains to (booking) + * status message should convey (currently "created", "toclean" and "cleaned" as strings) + It should be a django template that can be rendered with these context elements + and should generally use all of them in one way or another. + It should be applicable to email, the web based general view, and should be scalable for + all device formats across those mediums. + """ + def notify(self, notifier_type, booking): + template = Template(LabMessage.objects.filter(lab=booking.lab).first().msg) + + context = {} + context["owner"] = booking.owner + context["notify_type"] = notifier_type + context["booking"] = booking + message = template.render(Context(context)) + notifier = Notifier() + notifier.title = self.TITLES[notifier_type] + notifier.content = message + notifier.user = booking.owner.userprofile + notifier.sender = str(booking.lab) + notifier.save() + notifier.send() + + + context["owner"] = False + + for user in booking.collaborators.all(): + context["collaborator"] = user + message = template.render(Context(context)) + notifier = Notifier() + notifier.title = self.TITLES[notifier_type] + notifier.content = message + notifier.user = UserProfile.objects.get(user=user) + notifier.sender = str(booking.lab) + notifier.save() + notifier.send() -- cgit 1.2.3-korg