diff --git a/src/bornhack/environment_settings.py.dist b/src/bornhack/environment_settings.py.dist index 3af1dbbc..ec1e324c 100644 --- a/src/bornhack/environment_settings.py.dist +++ b/src/bornhack/environment_settings.py.dist @@ -55,12 +55,13 @@ TICKET_CATEGORY_NAME='Tickets' # schedule settings SCHEDULE_MIDNIGHT_OFFSET_HOURS=6 SCHEDULE_TIMESLOT_LENGTH_MINUTES=30 +SCHEDULE_EVENT_NOTIFICATION_MINUTES=10 # irc bot settings IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS=60 IRCBOT_NICK='mybot' -IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL='#test' -IRCBOT_SERVER_HOSTNAME='ircd.example.com' +IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL='#something' +IRCBOT_SERVER_HOSTNAME='irc.example.com' IRCBOT_SERVER_PORT=6697 IRCBOT_SERVER_USETLS=True diff --git a/src/camps/utils.py b/src/camps/utils.py new file mode 100644 index 00000000..81454fb7 --- /dev/null +++ b/src/camps/utils.py @@ -0,0 +1,9 @@ +from camps.models import Camp +from django.utils import timezone + +def get_current_camp(): + try: + return Camp.objects.get(camp__contains=timezone.now()) + except Camp.DoesNotExist: + return False + diff --git a/src/program/management/commands/notification_worker.py b/src/program/management/commands/notification_worker.py new file mode 100644 index 00000000..55124806 --- /dev/null +++ b/src/program/management/commands/notification_worker.py @@ -0,0 +1,45 @@ +from django.core.management.base import BaseCommand +from django.conf import settings +from django.utils import timezone +from time import sleep +import irc3, sys, asyncio +from ircbot.models import OutgoingIrcMessage +from camps.utils import get_current_camp +from django.utils import timezone +from program.models import EventInstance +from datetime import timedelta + + +class Command(BaseCommand): + args = 'none' + help = 'Queue notifications for channels and users for upcoming event instances.' + + def output(self, message): + self.stdout.write('%s: %s' % (timezone.now().strftime("%Y-%m-%d %H:%M:%S"), message)) + + def handle(self, *args, **options): + self.output('Schedule notification worker running...') + while True: + camp = get_current_camp() + if camp: + print("working with camp %s" % camp) + # a camp is currently going on, check if we need to send out any notifications + for ei in EventInstance.objects.filter( + event__camp=camp, + event__event_type__notifications=True, + notifications_sent=False, + when__startswith__lt=timezone.now()+timedelta(minutes=settings.SCHEDULE_EVENT_NOTIFICATION_MINUTES), # start of event is less than X minutes away + when__startswith__gt=timezone.now() # but event has not started yet + ): + # this event is less than settings.SCHEDULE_EVENT_NOTIFICATION_MINUTES minutes from starting, queue an IRC notificatio + OutgoingIrcMessage.objects.create( + target=settings.IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL, + message="starting soon: %s" % ei, + timeout=ei.when.lower + ) + ei.notifications_sent=True + ei.save() + + # check once per minute + sleep(60) + diff --git a/src/program/migrations/0017_eventinstance_notifications_sent.py b/src/program/migrations/0017_eventinstance_notifications_sent.py new file mode 100644 index 00000000..28a07713 --- /dev/null +++ b/src/program/migrations/0017_eventinstance_notifications_sent.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-02-01 23:18 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('program', '0016_auto_20170131_1849'), + ] + + operations = [ + migrations.AddField( + model_name='eventinstance', + name='notifications_sent', + field=models.BooleanField(default=False), + ), + ] diff --git a/src/program/migrations/0018_eventtype_notifications.py b/src/program/migrations/0018_eventtype_notifications.py new file mode 100644 index 00000000..d519d42f --- /dev/null +++ b/src/program/migrations/0018_eventtype_notifications.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-02-02 12:29 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('program', '0017_eventinstance_notifications_sent'), + ] + + operations = [ + migrations.AddField( + model_name='eventtype', + name='notifications', + field=models.BooleanField(default=False), + ), + ] diff --git a/src/program/models.py b/src/program/models.py index 5d4e94f9..1cb0bfcf 100644 --- a/src/program/models.py +++ b/src/program/models.py @@ -14,6 +14,7 @@ class EventType(CreatedUpdatedModel): slug = models.SlugField() color = models.CharField(max_length=50) light_text = models.BooleanField(default=False) + notifications = models.BooleanField(default=False) def __str__(self): return self.name @@ -50,6 +51,7 @@ class EventInstance(CreatedUpdatedModel): """ An instance of an event """ event = models.ForeignKey('program.event', related_name='instances') when = DateTimeRangeField() + notifications_sent = models.BooleanField(default=False) class Meta: ordering = ['when']