Merge "Adds workflows to deploy and clean hosts"
[pharos-tools.git] / dashboard / src / notifier / dispatchers.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt 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 django.db.models.signals import pre_save
11 from django.dispatch import receiver
12 from django.contrib import messages
13 from django.core.mail import send_mail
14
15 class DispatchHandler():
16
17     @receiver(pre_save, sender='notifier.Notifier')
18     def dispatch(sender, instance, *args, **kwargs):
19         try:
20             msg_type = getattr(DispatchHandler, instance.message_type)
21             msg_type(instance)
22         except AttributeError:
23             instance.msg_sent = 'no dispatcher by given name exists: sending by email'
24             email(instance)
25
26     def email(instance):
27         if instance.msg_sent != 'no dispatcher by given name exists: sending by email':
28             instance.msg_sent = 'by email'
29         send_mail(instance.title,instance.content + 
30             '\n\n This message pertains to the following resource: ' + 
31             instance.resource.name,instance.sender,[instance.user.email_addr], fail_silently=False)
32
33     def webnotification(instance):
34         instance.msg_sent='by web notification'