diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/account/migrations/0002_userprofile_email_addr.py | 20 | ||||
-rw-r--r-- | src/account/models.py | 2 | ||||
-rw-r--r-- | src/api/serializers.py | 6 | ||||
-rw-r--r-- | src/api/urls.py | 3 | ||||
-rw-r--r-- | src/api/views.py | 5 | ||||
-rw-r--r-- | src/notification/__init__.py | 11 | ||||
-rw-r--r-- | src/notification/migrations/0001_initial.py | 28 | ||||
-rw-r--r-- | src/notification/migrations/__init__.py | 10 | ||||
-rw-r--r-- | src/notification/models.py | 33 | ||||
-rw-r--r-- | src/notification/signals.py | 25 | ||||
-rw-r--r-- | src/notification/tasks.py | 49 | ||||
-rw-r--r-- | src/notification/tests.py | 41 | ||||
-rw-r--r-- | src/notifier/__init__.py | 0 | ||||
-rw-r--r-- | src/notifier/admin.py (renamed from src/notification/admin.py) | 7 | ||||
-rw-r--r-- | src/notifier/apps.py (renamed from src/notification/apps.py) | 8 | ||||
-rw-r--r-- | src/notifier/dispatchers.py | 34 | ||||
-rw-r--r-- | src/notifier/migrations/0001_initial.py | 31 | ||||
-rw-r--r-- | src/notifier/migrations/__init__.py | 0 | ||||
-rw-r--r-- | src/notifier/models.py | 38 | ||||
-rw-r--r-- | src/pharos_dashboard/settings.py | 8 |
20 files changed, 147 insertions, 212 deletions
diff --git a/src/account/migrations/0002_userprofile_email_addr.py b/src/account/migrations/0002_userprofile_email_addr.py new file mode 100644 index 0000000..bfbed17 --- /dev/null +++ b/src/account/migrations/0002_userprofile_email_addr.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2017-12-14 20:37 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='email_addr', + field=models.CharField(default='email@mail.com', max_length=300), + ), + ] diff --git a/src/account/models.py b/src/account/models.py index bfc0bbe..47b012b 100644 --- a/src/account/models.py +++ b/src/account/models.py @@ -20,7 +20,7 @@ class UserProfile(models.Model): timezone = models.CharField(max_length=100, blank=False, default='UTC') ssh_public_key = models.FileField(upload_to=upload_to, null=True, blank=True) pgp_public_key = models.FileField(upload_to=upload_to, null=True, blank=True) - email_addr = models.CharField(max_length=300, blank=false, default='email@mail.com') + email_addr = models.CharField(max_length=300, blank=False, default='email@mail.com') company = models.CharField(max_length=200, blank=False) oauth_token = models.CharField(max_length=1024, blank=False) diff --git a/src/api/serializers.py b/src/api/serializers.py index 4478175..dc79d9d 100644 --- a/src/api/serializers.py +++ b/src/api/serializers.py @@ -10,6 +10,7 @@ from rest_framework import serializers +from notifier.models import Notifier from booking.models import Booking from dashboard.models import Server, Resource, ResourceStatus @@ -37,3 +38,8 @@ class ResourceStatusSerializer(serializers.ModelSerializer): class Meta: model = ResourceStatus fields = ('id', 'resource', 'timestamp','type', 'title', 'content') + +class NotifierSerializer(serializers.ModelSerializer): + class Meta: + model = Notifier + fields = ('id', 'title', 'content', 'user', 'sender', 'message_type', 'msg_sent') diff --git a/src/api/urls.py b/src/api/urls.py index a4a4b2f..71cd3ef 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -33,8 +33,9 @@ router.register(r'resources', ResourceViewSet) router.register(r'servers', ServerViewSet) router.register(r'bookings', BookingViewSet) router.register(r'resource_status', ResourceStatusViewSet) +router.register(r'notifier', NotifierViewSet) urlpatterns = [ url(r'^', include(router.urls)), url(r'^token$', GenerateTokenView.as_view(), name='generate_token'), -]
\ No newline at end of file +] diff --git a/src/api/views.py b/src/api/views.py index 84fa1b5..c16d57d 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -8,7 +8,7 @@ ############################################################################## -from django.contrib.auth.decorators import login_required +from django.contrib.auth.decorators import login_required, user_passes_test from django.shortcuts import redirect from django.utils.decorators import method_decorator from django.views import View @@ -41,6 +41,9 @@ class ResourceStatusViewSet(viewsets.ModelViewSet): queryset = ResourceStatus.objects.all() serializer_class = ResourceStatusSerializer +class NotifierViewSet(viewsets.ModelViewSet): + queryset = Notifier.objects.none() + serializer_class = NotifierSerializer @method_decorator(login_required, name='dispatch') class GenerateTokenView(View): diff --git a/src/notification/__init__.py b/src/notification/__init__.py deleted file mode 100644 index 37dcbdd..0000000 --- a/src/notification/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -############################################################################## -# 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/migrations/0001_initial.py b/src/notification/migrations/0001_initial.py deleted file mode 100644 index 8b8414e..0000000 --- a/src/notification/migrations/0001_initial.py +++ /dev/null @@ -1,28 +0,0 @@ -# -*- 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 deleted file mode 100644 index b5914ce..0000000 --- a/src/notification/migrations/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -############################################################################## -# 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 deleted file mode 100644 index 89b3023..0000000 --- a/src/notification/models.py +++ /dev/null @@ -1,33 +0,0 @@ -############################################################################## -# 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 deleted file mode 100644 index 936c25b..0000000 --- a/src/notification/signals.py +++ /dev/null @@ -1,25 +0,0 @@ -############################################################################## -# 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 deleted file mode 100644 index 7f73762..0000000 --- a/src/notification/tasks.py +++ /dev/null @@ -1,49 +0,0 @@ -############################################################################## -# 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 deleted file mode 100644 index 9df9aa6..0000000 --- a/src/notification/tests.py +++ /dev/null @@ -1,41 +0,0 @@ -############################################################################## -# 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()) diff --git a/src/notifier/__init__.py b/src/notifier/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/notifier/__init__.py diff --git a/src/notification/admin.py b/src/notifier/admin.py index bcaa1ab..cfbe778 100644 --- a/src/notification/admin.py +++ b/src/notifier/admin.py @@ -7,11 +7,8 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## - -from django.conf import settings from django.contrib import admin -from notification.models import BookingNotification +from notifier.models import * -if settings.DEBUG: - admin.site.register(BookingNotification)
\ No newline at end of file +admin.site.register(Notifier) diff --git a/src/notification/apps.py b/src/notifier/apps.py index 2de22c4..da5d3b0 100644 --- a/src/notification/apps.py +++ b/src/notifier/apps.py @@ -7,12 +7,8 @@ # 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 +class NotifierConfig(AppConfig): + name = 'notifier' diff --git a/src/notifier/dispatchers.py b/src/notifier/dispatchers.py new file mode 100644 index 0000000..64be2a5 --- /dev/null +++ b/src/notifier/dispatchers.py @@ -0,0 +1,34 @@ +############################################################################## +# 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 pre_save +from django.dispatch import receiver +from django.contrib import messages +from django.core.mail import send_mail + +class DispatchHandler(): + + @receiver(pre_save, sender='notifier.Notifier') + def dispatch(sender, instance, *args, **kwargs): + try: + msg_type = getattr(DispatchHandler, instance.message_type) + msg_type(instance) + except AttributeError: + instance.msg_sent = 'no dispatcher by given name exists: sending by email' + email(instance) + + def email(instance): + if instance.msg_sent != 'no dispatcher by given name exists: sending by email': + instance.msg_sent = 'by email' + send_mail(instance.title,instance.content + + '\n\n This message pertains to the following resource: ' + + instance.resource.name,instance.sender,[instance.user.email_addr], fail_silently=False) + + def webnotification(instance): + instance.msg_sent='by web notification' diff --git a/src/notifier/migrations/0001_initial.py b/src/notifier/migrations/0001_initial.py new file mode 100644 index 0000000..78d2bad --- /dev/null +++ b/src/notifier/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2017-12-14 21:41 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import fernet_fields.fields + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('account', '0002_userprofile_email_addr'), + ('dashboard', '0002_auto_20170505_0815'), + ] + + operations = [ + migrations.CreateModel( + name='Notifier', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('title', models.CharField(max_length=240)), + ('content', fernet_fields.fields.EncryptedTextField()), + ('sender', models.CharField(default='unknown', max_length=240)), + ('message_type', models.CharField(choices=[('email', 'Email'), ('webnotification', 'Web Notification')], default='email', max_length=240)), + ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='account.UserProfile')), + ], + ), + ] diff --git a/src/notifier/migrations/__init__.py b/src/notifier/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/notifier/migrations/__init__.py diff --git a/src/notifier/models.py b/src/notifier/models.py new file mode 100644 index 0000000..9ebc6fc --- /dev/null +++ b/src/notifier/models.py @@ -0,0 +1,38 @@ +############################################################################## +# 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 +from jira import JIRA, JIRAError +from dashboard.models import Resource +from booking.models import Booking +from django.contrib.auth.models import User +from account.models import UserProfile +from django.contrib import messages +from django.db.models.signals import pre_save +from fernet_fields import EncryptedTextField + +class Notifier(models.Model): + id = models.AutoField(primary_key=True) + title = models.CharField(max_length=240) + content = EncryptedTextField() + user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, null=True, blank=True) + sender = models.CharField(max_length=240, default='unknown') + message_type = models.CharField(max_length=240, default='email', choices=( + ('email','Email'), + ('webnotification', 'Web Notification'))) + msg_sent = '' + + import notifier.dispatchers + + def __str__(self): + return self.title + + def getEmail(self): + return self.user.email_addr + diff --git a/src/pharos_dashboard/settings.py b/src/pharos_dashboard/settings.py index 83ad172..240f68e 100644 --- a/src/pharos_dashboard/settings.py +++ b/src/pharos_dashboard/settings.py @@ -14,7 +14,7 @@ INSTALLED_APPS = [ 'booking', 'account', 'jenkins', - 'notification', + 'notifier', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -188,3 +188,9 @@ CI_SLAVES_URL = os.environ['JENKINS_URL'] + '/label/ci-pod/api/json?tree=nodes[n ALL_JOBS_URL = os.environ['JENKINS_URL'] + '/api/json?tree=jobs[displayName,url,lastBuild[fullDisplayName,building,builtOn,timestamp,result]' GET_SLAVE_URL = os.environ['JENKINS_URL'] + '/computer/' +# Notifier Settings +EMAIL_HOST = os.environ['EMAIL_HOST'] +EMAIL_PORT = os.environ['EMAIL_PORT'] +EMAIL_HOST_USER = os.environ['EMAIL_HOST_USER'] +EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD'] +EMAIL_USE_TLS=True |