countdown: app for noting points in time

This commit is contained in:
Brian S. Stephan 2015-06-18 09:19:53 -05:00
parent a9ef68c3a2
commit 6560327984
8 changed files with 155 additions and 0 deletions

View File

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -37,6 +37,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django_extensions', 'django_extensions',
'adminplus', 'adminplus',
'countdown',
'facts', 'facts',
'ircbot', 'ircbot',
'karma', 'karma',