diff options
Diffstat (limited to 'src/notifier/tasks.py')
-rw-r--r-- | src/notifier/tasks.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/notifier/tasks.py b/src/notifier/tasks.py new file mode 100644 index 0000000..b45ab8e --- /dev/null +++ b/src/notifier/tasks.py @@ -0,0 +1,35 @@ +############################################################################## +# Copyright (c) 2016 Max Breitenfeldt and others. +# Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, 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 celery import shared_task +from django.utils import timezone +from django.conf import settings +from booking.models import Booking +from notifier.models import Emailed +from notifier.manager import NotificationHandler + + +@shared_task +def notify_expiring(): + """ + Notify users if their booking is within 48 hours of expiring. + """ + expire_time = timezone.now() + timezone.timedelta(hours=settings.EXPIRE_HOURS) + # Don't email people about bookings that have started recently + start_time = timezone.now() - timezone.timedelta(hours=settings.EXPIRE_LIFETIME) + bookings = Booking.objects.filter(end__lte=expire_time, + end__gte=timezone.now(), + start__lte=start_time) + for booking in bookings: + if Emailed.objects.filter(almost_end_booking=booking).exists(): + continue + NotificationHandler.notify_booking_expiring(booking) + Emailed.objects.create(almost_end_booking=booking) |