collapsing all of dr_botzo one directory

This commit is contained in:
2017-02-04 11:48:55 -06:00
parent 38d14bb0d2
commit cd23f062a9
194 changed files with 0 additions and 0 deletions

0
countdown/__init__.py Normal file
View File

8
countdown/admin.py Normal 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)

77
countdown/ircplugin.py Normal file
View File

@@ -0,0 +1,77 @@
"""Access to countdown items through bot commands."""
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 abs(rdelta.years) != 1 else "")
if rdelta.months != 0:
relstr += "{0:s} month{1:s}, ".format(str(rdelta.months), "s" if abs(rdelta.months) != 1 else "")
if rdelta.days != 0:
relstr += "{0:s} day{1:s}, ".format(str(rdelta.days), "s" if abs(rdelta.days) != 1 else "")
if rdelta.hours != 0:
relstr += "{0:s} hour{1:s}, ".format(str(rdelta.hours), "s" if abs(rdelta.hours) != 1 else "")
if rdelta.minutes != 0:
relstr += "{0:s} minute{1:s}, ".format(str(rdelta.minutes), "s" if abs(rdelta.minutes) != 1 else "")
if rdelta.seconds != 0:
relstr += "{0:s} second{1:s}, ".format(str(rdelta.seconds), "s" if abs(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,22 @@
# -*- coding: utf-8 -*-
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,17 @@
# -*- coding: utf-8 -*-
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('countdown', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='countdownitem',
name='source',
),
]

View File

24
countdown/models.py Normal file
View File

@@ -0,0 +1,24 @@
"""Countdown item models."""
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 __str__(self):
"""String representation."""
return "{0:s} @ {1:s}".format(self.name, timezone.localtime(self.at_time).strftime('%Y-%m-%d %H:%M:%S %Z'))