From 4b269fba0ca273dfa3acf44c9f5490f01e0c3d87 Mon Sep 17 00:00:00 2001 From: Trevor Bramwell Date: Fri, 22 Sep 2017 12:23:36 -0700 Subject: Rename pharos-dashboard and pharos-validator As subdirectories of the pharos-tools repo, there is little need to keep the pharos prefix. Change-Id: Ica3d79411f409df638647300036c0664183c2725 Signed-off-by: Trevor Bramwell --- dashboard/src/notification/__init__.py | 11 +++++ dashboard/src/notification/admin.py | 17 ++++++++ dashboard/src/notification/apps.py | 18 ++++++++ .../src/notification/migrations/0001_initial.py | 28 +++++++++++++ dashboard/src/notification/migrations/__init__.py | 10 +++++ dashboard/src/notification/models.py | 33 +++++++++++++++ dashboard/src/notification/signals.py | 25 +++++++++++ dashboard/src/notification/tasks.py | 49 ++++++++++++++++++++++ dashboard/src/notification/tests.py | 41 ++++++++++++++++++ 9 files changed, 232 insertions(+) create mode 100644 dashboard/src/notification/__init__.py create mode 100644 dashboard/src/notification/admin.py create mode 100644 dashboard/src/notification/apps.py create mode 100644 dashboard/src/notification/migrations/0001_initial.py create mode 100644 dashboard/src/notification/migrations/__init__.py create mode 100644 dashboard/src/notification/models.py create mode 100644 dashboard/src/notification/signals.py create mode 100644 dashboard/src/notification/tasks.py create mode 100644 dashboard/src/notification/tests.py (limited to 'dashboard/src/notification') diff --git a/dashboard/src/notification/__init__.py b/dashboard/src/notification/__init__.py new file mode 100644 index 0000000..37dcbdd --- /dev/null +++ b/dashboard/src/notification/__init__.py @@ -0,0 +1,11 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 +############################################################################## + + +default_app_config = 'notification.apps.NotificationConfig' \ No newline at end of file diff --git a/dashboard/src/notification/admin.py b/dashboard/src/notification/admin.py new file mode 100644 index 0000000..bcaa1ab --- /dev/null +++ b/dashboard/src/notification/admin.py @@ -0,0 +1,17 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 django.conf import settings +from django.contrib import admin + +from notification.models import BookingNotification + +if settings.DEBUG: + admin.site.register(BookingNotification) \ No newline at end of file diff --git a/dashboard/src/notification/apps.py b/dashboard/src/notification/apps.py new file mode 100644 index 0000000..2de22c4 --- /dev/null +++ b/dashboard/src/notification/apps.py @@ -0,0 +1,18 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 django.apps import AppConfig + + +class NotificationConfig(AppConfig): + name = 'notification' + + def ready(self): + import notification.signals #noqa \ No newline at end of file diff --git a/dashboard/src/notification/migrations/0001_initial.py b/dashboard/src/notification/migrations/0001_initial.py new file mode 100644 index 0000000..8b8414e --- /dev/null +++ b/dashboard/src/notification/migrations/0001_initial.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-11-03 13:33 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('booking', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='BookingNotification', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('type', models.CharField(max_length=100)), + ('submit_time', models.DateTimeField()), + ('submitted', models.BooleanField(default=False)), + ('booking', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='booking.Booking')), + ], + ), + ] diff --git a/dashboard/src/notification/migrations/__init__.py b/dashboard/src/notification/migrations/__init__.py new file mode 100644 index 0000000..b5914ce --- /dev/null +++ b/dashboard/src/notification/migrations/__init__.py @@ -0,0 +1,10 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 +############################################################################## + + diff --git a/dashboard/src/notification/models.py b/dashboard/src/notification/models.py new file mode 100644 index 0000000..89b3023 --- /dev/null +++ b/dashboard/src/notification/models.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 django.db import models + +class BookingNotification(models.Model): + id = models.AutoField(primary_key=True) + type = models.CharField(max_length=100) + booking = models.ForeignKey('booking.Booking', on_delete=models.CASCADE) + submit_time = models.DateTimeField() + submitted = models.BooleanField(default=False) + + def get_content(self): + return { + 'resource_id': self.booking.resource.id, + 'booking_id': self.booking.id, + 'user': self.booking.user.username, + 'user_id': self.booking.user.id, + } + + def save(self, *args, **kwargs): + notifications = self.booking.bookingnotification_set.filter(type=self.type).exclude( + id=self.id) + #if notifications.count() > 0: + # raise ValueError('Doubled Notification') + return super(BookingNotification, self).save(*args, **kwargs) diff --git a/dashboard/src/notification/signals.py b/dashboard/src/notification/signals.py new file mode 100644 index 0000000..936c25b --- /dev/null +++ b/dashboard/src/notification/signals.py @@ -0,0 +1,25 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 django.db.models.signals import post_save +from django.dispatch import receiver + +from booking.models import Booking +from notification.models import BookingNotification + + +@receiver(post_save, sender=Booking) +def booking_notification_handler(sender, instance, **kwargs): + BookingNotification.objects.update_or_create( + booking=instance, type='booking_start', defaults={'submit_time': instance.start} + ) + BookingNotification.objects.update_or_create( + booking=instance, type='booking_end', defaults={'submit_time': instance.end} + ) \ No newline at end of file diff --git a/dashboard/src/notification/tasks.py b/dashboard/src/notification/tasks.py new file mode 100644 index 0000000..7f73762 --- /dev/null +++ b/dashboard/src/notification/tasks.py @@ -0,0 +1,49 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 +############################################################################## + + +import os +import sys +from datetime import timedelta + +from celery import shared_task +from django.conf import settings +from django.utils import timezone + +from notification.models import BookingNotification + +# this adds the top level directory to the python path, this is needed so that we can access the +# notification library +sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) + +from dashboard_notification.notification import Notification, Message + + +@shared_task +def send_booking_notifications(): + with Notification(dashboard_url=settings.RABBITMQ_URL, user=settings.RABBITMQ_USER, password=settings.RABBITMQ_PASSWORD) as messaging: + now = timezone.now() + notifications = BookingNotification.objects.filter(submitted=False, + submit_time__gt=now - timedelta(minutes=1), + submit_time__lt=now + timedelta(minutes=5)) + for notification in notifications: + message = Message(type=notification.type, topic=notification.booking.resource.name, + content=notification.get_content()) + messaging.send(message) + notification.submitted = True + notification.save() + +@shared_task +def notification_debug(): + with Notification(dashboard_url=settings.RABBITMQ_URL) as messaging: + notifications = BookingNotification.objects.all() + for notification in notifications: + message = Message(type=notification.type, topic=notification.booking.resource.name, + content=notification.get_content()) + messaging.send(message) diff --git a/dashboard/src/notification/tests.py b/dashboard/src/notification/tests.py new file mode 100644 index 0000000..9df9aa6 --- /dev/null +++ b/dashboard/src/notification/tests.py @@ -0,0 +1,41 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt 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 datetime import timedelta +from unittest import TestCase + +from django.contrib.auth.models import User +from django.utils import timezone + +from booking.models import Booking +from dashboard.models import Resource +from jenkins.models import JenkinsSlave +from notification.models import * + + +class JenkinsModelTestCase(TestCase): + def setUp(self): + self.slave = JenkinsSlave.objects.create(name='test1', url='test') + self.res1 = Resource.objects.create(name='res1', slave=self.slave, description='x', + url='x') + self.user1 = User.objects.create(username='user1') + + start = timezone.now() + end = start + timedelta(days=1) + self.booking = Booking.objects.create(start=start, end=end, purpose='test', + resource=self.res1, user=self.user1) + + def test_booking_notification(self): + BookingNotification.objects.create(type='test', booking=self.booking, + submit_time=timezone.now()) + + self.assertRaises(ValueError, BookingNotification.objects.create, type='test', + booking=self.booking, + submit_time=timezone.now()) -- cgit 1.2.3-korg