Adding Labels
[pharos.git] / tools / pharos-dashboard / src / notification / tasks.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
11 import os
12 import sys
13 from datetime import timedelta
14
15 from celery import shared_task
16 from django.conf import settings
17 from django.utils import timezone
18
19 from notification.models import BookingNotification
20
21 # this adds the top level directory to the python path, this is needed so that we can access the
22 # notification library
23 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
24
25 from dashboard_notification.notification import Notification, Message
26
27
28 @shared_task
29 def send_booking_notifications():
30     with Notification(dashboard_url=settings.RABBITMQ_URL) as messaging:
31         now = timezone.now()
32         notifications = BookingNotification.objects.filter(submitted=False,
33                                                            submit_time__gt=now - timedelta(minutes=1),
34                                                            submit_time__lt=now + timedelta(minutes=5))
35         for notification in notifications:
36             message = Message(type=notification.type, topic=notification.booking.resource.name,
37                               content=notification.get_content())
38             messaging.send(message)
39             notification.submitted = True
40             notification.save()
41
42 @shared_task
43 def notification_debug():
44     with Notification(dashboard_url=settings.RABBITMQ_URL) as messaging:
45         notifications = BookingNotification.objects.all()
46         for notification in notifications:
47             message = Message(type=notification.type, topic=notification.booking.resource.name,
48                               content=notification.get_content())
49             messaging.send(message)