From 76b70ec784c43c834bea5c307f0eece0c17a08c9 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 23 Feb 2017 18:45:06 -0600 Subject: [PATCH 1/3] countdown: use match labels in new reminder regex this thing is going to get hairy soon, so might as well do this now --- countdown/ircplugin.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/countdown/ircplugin.py b/countdown/ircplugin.py index f15c65c..c135234 100644 --- a/countdown/ircplugin.py +++ b/countdown/ircplugin.py @@ -20,7 +20,8 @@ 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[^\s]+)\s+(?Pat|in|on)\s+(?P.*?)\s+' + r'(to|that|about)\s+(?P.*)') def __init__(self, bot, connection, event): """Initialize some stuff.""" @@ -96,10 +97,10 @@ 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') + 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')) From bdbba249cd0510d89e8b7668d573cfe2e3ba1b9d Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 23 Feb 2017 19:10:24 -0600 Subject: [PATCH 2/3] countdown: let reminders be recurring when creating a reminder, describe it as "and every X" and it'll refire over that period, rather than only firing once closes bss/dr.botzo#30 --- countdown/ircplugin.py | 15 +++++++++++++- .../0004_countdownitem_recurring_period.py | 20 +++++++++++++++++++ countdown/models.py | 2 ++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 countdown/migrations/0004_countdownitem_recurring_period.py diff --git a/countdown/ircplugin.py b/countdown/ircplugin.py index c135234..d7c2f6c 100644 --- a/countdown/ircplugin.py +++ b/countdown/ircplugin.py @@ -21,6 +21,7 @@ class Countdown(Plugin): """Report on countdown items.""" new_reminder_regex = (r'remind\s+(?P[^\s]+)\s+(?Pat|in|on)\s+(?P.*?)\s+' + r'(and\s+every\s+(?P.*?)\s+)?' r'(to|that|about)\s+(?P.*)') def __init__(self, bot, connection, event): @@ -75,7 +76,15 @@ 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, set a new at time, otherwise stop reminding + if reminder.recurring_period != '': + calendar = pdt.Calendar() + when_t = calendar.parseDT(reminder.recurring_period, reminder.at_time, + tzinfo=reminder.at_time.tzinfo)[0] + reminder.at_time = when_t + else: + reminder.sent_reminder = True reminder.save() time.sleep(1) @@ -100,6 +109,7 @@ class Countdown(Plugin): who = match.group('who') when_type = match.group('when_type') when = match.group('when') + recurring_period = match.group('recurring_period') text = match.group('text') log.debug("%s / %s / %s", who, when, text) @@ -126,6 +136,9 @@ 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 + countdown_item.save() log.info("created countdown item %s", str(countdown_item)) if in_privmsg: diff --git a/countdown/migrations/0004_countdownitem_recurring_period.py b/countdown/migrations/0004_countdownitem_recurring_period.py new file mode 100644 index 0000000..608859f --- /dev/null +++ b/countdown/migrations/0004_countdownitem_recurring_period.py @@ -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), + ), + ] diff --git a/countdown/models.py b/countdown/models.py index 1336657..6437ef1 100644 --- a/countdown/models.py +++ b/countdown/models.py @@ -21,6 +21,8 @@ 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='') + created_time = models.DateTimeField(auto_now_add=True) def __str__(self): From 1b1ae9638dcf456bee3febc9da5372b597e9c36c Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 23 Feb 2017 19:35:47 -0600 Subject: [PATCH 3/3] countdown: add "recurring until" support tacking onto the recurring option, this allows for having the recurringness end after a while. closes bss/dr.botzo#31 --- countdown/ircplugin.py | 16 ++++++++++++--- .../0005_countdownitem_recurring_until.py | 20 +++++++++++++++++++ countdown/models.py | 1 + 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 countdown/migrations/0005_countdownitem_recurring_until.py diff --git a/countdown/ircplugin.py b/countdown/ircplugin.py index d7c2f6c..a25108b 100644 --- a/countdown/ircplugin.py +++ b/countdown/ircplugin.py @@ -22,6 +22,7 @@ class Countdown(Plugin): new_reminder_regex = (r'remind\s+(?P[^\s]+)\s+(?Pat|in|on)\s+(?P.*?)\s+' r'(and\s+every\s+(?P.*?)\s+)?' + r'(until\s+(?P.*?)\s+)?' r'(to|that|about)\s+(?P.*)') def __init__(self, bot, connection, event): @@ -77,12 +78,17 @@ class Countdown(Plugin): log.info("sending %s to %s", reminder.reminder_message, reminder.reminder_target) self.bot.reply(None, reminder.reminder_message, explicit_target=reminder.reminder_target) - # if recurring, set a new at time, otherwise stop reminding - if reminder.recurring_period != '': + # 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] - reminder.at_time = when_t + 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() @@ -110,6 +116,7 @@ class Countdown(Plugin): 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) @@ -138,6 +145,9 @@ class Countdown(Plugin): 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)) diff --git a/countdown/migrations/0005_countdownitem_recurring_until.py b/countdown/migrations/0005_countdownitem_recurring_until.py new file mode 100644 index 0000000..9825ea6 --- /dev/null +++ b/countdown/migrations/0005_countdownitem_recurring_until.py @@ -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), + ), + ] diff --git a/countdown/models.py b/countdown/models.py index 6437ef1..fc2c89c 100644 --- a/countdown/models.py +++ b/countdown/models.py @@ -22,6 +22,7 @@ class CountdownItem(models.Model): 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)