summaryrefslogtreecommitdiffstats
path: root/src/notification
diff options
context:
space:
mode:
authorTrevor Bramwell <tbramwell@linuxfoundation.org>2017-09-22 12:23:36 -0700
committerTrevor Bramwell <tbramwell@linuxfoundation.org>2017-09-22 12:23:36 -0700
commitb07bbdba03fe9b1c3da2f69b8bc28b8071d99ec1 (patch)
treeed2a67f71eb8f3d3e8e3c284e7d53f91ecd6cd96 /src/notification
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 <tbramwell@linuxfoundation.org>
Diffstat (limited to 'src/notification')
-rw-r--r--src/notification/__init__.py11
-rw-r--r--src/notification/admin.py17
-rw-r--r--src/notification/apps.py18
-rw-r--r--src/notification/migrations/0001_initial.py28
-rw-r--r--src/notification/migrations/__init__.py10
-rw-r--r--src/notification/models.py33
-rw-r--r--src/notification/signals.py25
-rw-r--r--src/notification/tasks.py49
-rw-r--r--src/notification/tests.py41
9 files changed, 232 insertions, 0 deletions
diff --git a/src/notification/__init__.py b/src/notification/__init__.py
new file mode 100644
index 0000000..37dcbdd
--- /dev/null
+++ b/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/src/notification/admin.py b/src/notification/admin.py
new file mode 100644
index 0000000..bcaa1ab
--- /dev/null
+++ b/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/src/notification/apps.py b/src/notification/apps.py
new file mode 100644
index 0000000..2de22c4
--- /dev/null
+++ b/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/src/notification/migrations/0001_initial.py b/src/notification/migrations/0001_initial.py
new file mode 100644
index 0000000..8b8414e
--- /dev/null
+++ b/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/src/notification/migrations/__init__.py b/src/notification/migrations/__init__.py
new file mode 100644
index 0000000..b5914ce
--- /dev/null
+++ b/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/src/notification/models.py b/src/notification/models.py
new file mode 100644
index 0000000..89b3023
--- /dev/null
+++ b/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/src/notification/signals.py b/src/notification/signals.py
new file mode 100644
index 0000000..936c25b
--- /dev/null
+++ b/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/src/notification/tasks.py b/src/notification/tasks.py
new file mode 100644
index 0000000..7f73762
--- /dev/null
+++ b/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/src/notification/tests.py b/src/notification/tests.py
new file mode 100644
index 0000000..9df9aa6
--- /dev/null
+++ b/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())