From 6560327984c74602b03c014cbf0c8e34976de94d Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 18 Jun 2015 09:19:53 -0500 Subject: [PATCH] countdown: app for noting points in time --- dr_botzo/countdown/__init__.py | 0 dr_botzo/countdown/admin.py | 8 ++ dr_botzo/countdown/ircplugin.py | 79 +++++++++++++++++++ dr_botzo/countdown/migrations/0001_initial.py | 23 ++++++ .../0002_remove_countdownitem_source.py | 18 +++++ dr_botzo/countdown/migrations/__init__.py | 0 dr_botzo/countdown/models.py | 26 ++++++ dr_botzo/dr_botzo/settings.py | 1 + 8 files changed, 155 insertions(+) create mode 100644 dr_botzo/countdown/__init__.py create mode 100644 dr_botzo/countdown/admin.py create mode 100644 dr_botzo/countdown/ircplugin.py create mode 100644 dr_botzo/countdown/migrations/0001_initial.py create mode 100644 dr_botzo/countdown/migrations/0002_remove_countdownitem_source.py create mode 100644 dr_botzo/countdown/migrations/__init__.py create mode 100644 dr_botzo/countdown/models.py diff --git a/dr_botzo/countdown/__init__.py b/dr_botzo/countdown/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dr_botzo/countdown/admin.py b/dr_botzo/countdown/admin.py new file mode 100644 index 0000000..6000255 --- /dev/null +++ b/dr_botzo/countdown/admin.py @@ -0,0 +1,8 @@ +"""Manage countdown models in the admin interface.""" + +from django.contrib import admin + +from countdown.models import CountdownItem + + +admin.site.register(CountdownItem) diff --git a/dr_botzo/countdown/ircplugin.py b/dr_botzo/countdown/ircplugin.py new file mode 100644 index 0000000..7890209 --- /dev/null +++ b/dr_botzo/countdown/ircplugin.py @@ -0,0 +1,79 @@ +"""Access to countdown items through bot commands.""" + +from __future__ import unicode_literals + +import logging + +from dateutil.relativedelta import relativedelta + +from django.utils import timezone + +from ircbot.lib import Plugin +from countdown.models import CountdownItem + + +log = logging.getLogger('countdown.ircplugin') + + +class Countdown(Plugin): + + """Report on countdown items.""" + + def start(self): + """Set up handlers.""" + + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+list$', + self.handle_item_list, -20) + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!countdown\s+(\S+)$', + self.handle_item_detail, -20) + + super(Countdown, self).start() + + def stop(self): + """Tear down handlers.""" + + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_list) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_item_detail) + + super(Countdown, self).stop() + + def handle_item_detail(self, connection, event, match): + """Provide the details of one countdown item.""" + + name = match.group(1) + + if name != 'list': + try: + item = CountdownItem.objects.get(name=name) + rdelta = relativedelta(item.at_time, timezone.now()) + relstr = "{0:s} will occur in ".format(name) + if rdelta.years != 0: + relstr += "{0:s} year{1:s} ".format(str(rdelta.years), "s" if rdelta.years != 1 else "") + if rdelta.months != 0: + relstr += "{0:s} month{1:s}, ".format(str(rdelta.months), "s" if rdelta.months != 1 else "") + if rdelta.days != 0: + relstr += "{0:s} day{1:s}, ".format(str(rdelta.days), "s" if rdelta.days != 1 else "") + if rdelta.hours != 0: + relstr += "{0:s} hour{1:s}, ".format(str(rdelta.hours), "s" if rdelta.hours != 1 else "") + if rdelta.minutes != 0: + relstr += "{0:s} minute{1:s}, ".format(str(rdelta.minutes), "s" if rdelta.minutes != 1 else "") + if rdelta.seconds != 0: + relstr += "{0:s} second{1:s}, ".format(str(rdelta.seconds), "s" if rdelta.seconds != 1 else "") + # remove trailing comma from output + reply = relstr[0:-2] + return self.bot.reply(event, reply) + except CountdownItem.DoesNotExist: + return self.bot.reply(event, "countdown item '{0:s}' not found".format(name)) + + def handle_item_list(self, connection, event, match): + """List all countdown items.""" + + items = CountdownItem.objects.all() + if len(items) > 0: + reply = "countdown items: {0:s}".format(", ".join([x.name for x in items])) + return self.bot.reply(event, reply) + else: + return self.bot.reply(event, "no countdown items are configured") + + +plugin = Countdown diff --git a/dr_botzo/countdown/migrations/0001_initial.py b/dr_botzo/countdown/migrations/0001_initial.py new file mode 100644 index 0000000..9461e6b --- /dev/null +++ b/dr_botzo/countdown/migrations/0001_initial.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='CountdownItem', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('name', models.CharField(default='', max_length=64)), + ('source', models.CharField(default='', max_length=200)), + ('at_time', models.DateTimeField()), + ('created_time', models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/dr_botzo/countdown/migrations/0002_remove_countdownitem_source.py b/dr_botzo/countdown/migrations/0002_remove_countdownitem_source.py new file mode 100644 index 0000000..2f2f221 --- /dev/null +++ b/dr_botzo/countdown/migrations/0002_remove_countdownitem_source.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('countdown', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='countdownitem', + name='source', + ), + ] diff --git a/dr_botzo/countdown/migrations/__init__.py b/dr_botzo/countdown/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dr_botzo/countdown/models.py b/dr_botzo/countdown/models.py new file mode 100644 index 0000000..fea628e --- /dev/null +++ b/dr_botzo/countdown/models.py @@ -0,0 +1,26 @@ +"""Countdown item models.""" + +from __future__ import unicode_literals + +import logging + +from django.db import models +from django.utils import timezone + + +log = logging.getLogger('countdown.models') + + +class CountdownItem(models.Model): + + """Track points in time.""" + + name = models.CharField(max_length=64, default='') + at_time = models.DateTimeField() + + created_time = models.DateTimeField(auto_now_add=True) + + def __unicode__(self): + """String representation.""" + + return "{0:s} @ {1:s}".format(self.name, timezone.localtime(self.at_time).strftime('%Y-%m-%d %H:%M:%S %Z')) diff --git a/dr_botzo/dr_botzo/settings.py b/dr_botzo/dr_botzo/settings.py index 51c3e28..d857502 100644 --- a/dr_botzo/dr_botzo/settings.py +++ b/dr_botzo/dr_botzo/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'django_extensions', 'adminplus', + 'countdown', 'facts', 'ircbot', 'karma',