Merge branch 'countdown-reminders' into 'master'

Add recurrence, and recurrence ceilings, to reminders

Closes #31 and #30

See merge request !17
This commit is contained in:
Brian S. Stephan 2017-02-23 19:38:27 -06:00
commit b58b208f29
4 changed files with 73 additions and 6 deletions

View File

@ -20,7 +20,10 @@ log = logging.getLogger('countdown.ircplugin')
class Countdown(Plugin):
"""Report on countdown items."""
new_reminder_regex = r'remind\s+([^\s]+)\s+(at|in|on)\s+(.*?)\s+(to|that|about)\s+(.*)'
new_reminder_regex = (r'remind\s+(?P<who>[^\s]+)\s+(?P<when_type>at|in|on)\s+(?P<when>.*?)\s+'
r'(and\s+every\s+(?P<recurring_period>.*?)\s+)?'
r'(until\s+(?P<recurring_until>.*?)\s+)?'
r'(to|that|about)\s+(?P<text>.*)')
def __init__(self, bot, connection, event):
"""Initialize some stuff."""
@ -74,7 +77,20 @@ class Countdown(Plugin):
if reminder.at_time <= timezone.now():
log.info("sending %s to %s", reminder.reminder_message, reminder.reminder_target)
self.bot.reply(None, reminder.reminder_message, explicit_target=reminder.reminder_target)
reminder.sent_reminder = True
# if recurring and not hit until, set a new at time, otherwise stop reminding
if reminder.recurring_until is not None and timezone.now() >= reminder.recurring_until:
reminder.sent_reminder = True
elif reminder.recurring_period != '':
calendar = pdt.Calendar()
when_t = calendar.parseDT(reminder.recurring_period, reminder.at_time,
tzinfo=reminder.at_time.tzinfo)[0]
if reminder.recurring_until is None or when_t <= reminder.recurring_until:
reminder.at_time = when_t
else:
reminder.sent_reminder = True
else:
reminder.sent_reminder = True
reminder.save()
time.sleep(1)
@ -96,10 +112,12 @@ class Countdown(Plugin):
match = re.match(self.new_reminder_regex, trimmed_what, re.IGNORECASE)
if match:
log.debug("%s is a new reminder request", trimmed_what)
who = match.group(1)
when_type = match.group(2)
when = match.group(3)
text = match.group(5)
who = match.group('who')
when_type = match.group('when_type')
when = match.group('when')
recurring_period = match.group('recurring_period')
recurring_until = match.group('recurring_until')
text = match.group('text')
log.debug("%s / %s / %s", who, when, text)
item_name = '{0:s}-{1:s}'.format(sender_nick, timezone.now().strftime('%s'))
@ -125,6 +143,12 @@ class Countdown(Plugin):
countdown_item = CountdownItem.objects.create(name=item_name, at_time=when_t, is_reminder=True,
reminder_message=message, reminder_target=sent_location)
if recurring_period:
countdown_item.recurring_period = recurring_period
if recurring_until:
recurring_until_t = timezone.make_aware(parse(recurring_until))
countdown_item.recurring_until = recurring_until_t
countdown_item.save()
log.info("created countdown item %s", str(countdown_item))
if in_privmsg:

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-24 01:06
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('countdown', '0003_auto_20170222_2025'),
]
operations = [
migrations.AddField(
model_name='countdownitem',
name='recurring_period',
field=models.CharField(default='', max_length=64),
),
]

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-24 01:12
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('countdown', '0004_countdownitem_recurring_period'),
]
operations = [
migrations.AddField(
model_name='countdownitem',
name='recurring_until',
field=models.DateTimeField(blank=True, default=None, null=True),
),
]

View File

@ -21,6 +21,9 @@ class CountdownItem(models.Model):
reminder_message = models.TextField(default="")
reminder_target = models.CharField(max_length=64, default='')
recurring_period = models.CharField(max_length=64, default='')
recurring_until = models.DateTimeField(null=True, blank=True, default=None)
created_time = models.DateTimeField(auto_now_add=True)
def __str__(self):