ircbot and notification worker basic functionality works

This commit is contained in:
Thomas Steen Rasmussen 2017-02-02 14:37:17 +01:00
parent 3715d00701
commit 5bc728a2e8
6 changed files with 99 additions and 2 deletions

View File

@ -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

9
src/camps/utils.py Normal file
View File

@ -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

View File

@ -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)

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -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']