From 62b3ba6efe4ac1de77f24527e844c9a70ed94b1b Mon Sep 17 00:00:00 2001 From: Joachim Filip Ignacy Bartosik Date: Wed, 22 Jun 2011 09:28:20 +0200 Subject: Add support for #timelimit (add|list|remove) commands --- bot/ircmeeting/agenda.py | 46 ++++++++++++++++++++++++++++++++++++-- bot/ircmeeting/meeting.py | 17 +++++++++++++- bot/tests/run_test.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/bot/ircmeeting/agenda.py b/bot/ircmeeting/agenda.py index 928ff5f..220acf9 100644 --- a/bot/ircmeeting/agenda.py +++ b/bot/ircmeeting/agenda.py @@ -1,7 +1,15 @@ import json +import threading import urllib import re +class MessageSender: + def __init__(self, irc, message): + self.irc = irc + self.message = message + def send_message(self): + self.irc.reply(self.message) + class Agenda(object): # Messages @@ -18,6 +26,10 @@ class Agenda(object): not_a_number_msg = "Your vote was not recognized as a number. Please retry." out_of_range_msg = "Your vote was out of range!" vote_confirm_msg = "You voted for #{} - {}" + timelimit_added_msg = 'Added "{}" reminder in {}:{}' + timelimit_list_msg = 'Set reminders: "{}"' + timelimit_removed_msg = 'Reminder "{}" removed' + timelimit_missing_msg = 'No such reminder "{}"' # Internal _voters = [] @@ -28,6 +40,7 @@ class Agenda(object): def __init__(self, conf): self.conf = conf + self.reminders = {} def get_agenda_item(self): if not self.conf.manage_agenda: @@ -37,6 +50,12 @@ class Agenda(object): else: return self.empty_agenda_msg + def _swich_agenda_item_to(self, new_item): + self._current_item = new_item + for reminder in self.reminders.values(): + reminder.cancel() + self.reminders = {} + def next_agenda_item(self): if not self.conf.manage_agenda: return('') @@ -44,7 +63,7 @@ class Agenda(object): return self.voting_open_so_item_not_changed_msg else: if (self._current_item + 1) < len(self._agenda): - self._current_item += 1 + self._swich_agenda_item_to(self._current_item + 1) return(self.get_agenda_item()) def prev_agenda_item(self): @@ -54,7 +73,7 @@ class Agenda(object): return self.voting_open_so_item_not_changed_msg else: if self._current_item > 0: - self._current_item -= 1 + self._swich_agenda_item_to(self._current_item - 1) return(self.get_agenda_item()) def start_vote(self): @@ -144,6 +163,29 @@ class Agenda(object): option = self._agenda[self._current_item][1].pop(opt) return str.format(self.removed_option_msg, str(opt), option) + def add_timelimit(self, minutes, seconds, message, irc): + sender = MessageSender(irc, message) + reminder = (threading.Timer(60*minutes + seconds, sender.send_message)) + self.reminders[message] = reminder + reminder.start() + result = str.format(self.timelimit_added_msg, message, minutes, seconds) + return(result) + + def list_timielimits(self): + keys = self.reminders.keys() + keys_str = '", "'.join(keys) + result = str.format(self.timelimit_list_msg, keys_str) + return(result) + + def remove_timelimit(self, message): + if message in self.reminders: + timer = self.reminders.pop(message) + timer.cancel() + result = str.format(self.timelimit_removed_msg, message) + else: + result = str.format(self.timelimit_missing_msg, message) + return(result) + def post_result(self): if not self.conf.manage_agenda: return('') diff --git a/bot/ircmeeting/meeting.py b/bot/ircmeeting/meeting.py index 84949ed..4bd3221 100644 --- a/bot/ircmeeting/meeting.py +++ b/bot/ircmeeting/meeting.py @@ -33,6 +33,7 @@ import time import os import re import stat +import threading import writers import items @@ -301,7 +302,6 @@ else: # Subclass Config and LocalConfig, new type overrides Config. Config = type('Config', (LocalConfig, Config), {}) - class MeetingCommands(object): # Command Definitions # generic parameters to these functions: @@ -328,6 +328,21 @@ class MeetingCommands(object): def do_previtem(self, nick, time_, line, **kwargs): self.reply(self.config.agenda.prev_agenda_item()) + def do_timelimit(self, nick, time_, line, **kwargs): + reply = 'Usage "#timelimit add : " or ' +\ + '"#timelimit list" or "#timelimit remove "' + match = re.match( ' *?add ([0-9]+):([0-9]+) (.*)', line) + if match: + reply = self.config.agenda.add_timelimit(int(match.group(1)), + int(match.group(2)), match.group(3), self) + elif re.match( ' *?list', line): + reply = self.config.agenda.list_timielimits() + else: + match = re.match( ' *?remove (.*)', line) + if(match): + reply = self.config.agenda.remove_timelimit(match.group(1)) + self.reply(reply) + def do_startvote(self, nick, time_, line, **kwargs): for messageline in self.config.agenda.start_vote().split('\n'): self.reply(messageline) diff --git a/bot/tests/run_test.py b/bot/tests/run_test.py index 9808ee6..e3a9030 100644 --- a/bot/tests/run_test.py +++ b/bot/tests/run_test.py @@ -6,6 +6,8 @@ import re import shutil import sys import tempfile +import time +import threading import unittest os.environ['MEETBOT_RUNNING_TESTS'] = '1' @@ -427,6 +429,60 @@ class MeetBotTest(unittest.TestCase): assert(test.votes() == {'first item': {u'x': 'opt2', u'z': 'opt1'}, 'second item': {}}) + def test_agenda_time_limit_adding(self): + test = self.get_simple_agenda_test() + test.answer_should_match('20:13:50 #timelimit', 'Usage "#timelimit ' +\ + 'add : " or "' +\ + '#timelimit list" or "#timelimit remove ' +\ + '"') + test.answer_should_match('20:13:50 #timelimit add 0:1 some other message', + 'Added "some other message" reminder in 0:1') + test.answer_should_match('20:13:50 #timelimit add 1:0 some message', + 'Added "some message" reminder in 1:0') + time.sleep(2) + last_message = test.log[-1] + assert(last_message == 'some other message') + reminders = test.M.config.agenda.reminders + assert(len(reminders) == 2) + for reminder in reminders.values(): + assert(reminder.__class__ == threading._Timer) + + test.process('20:13:50 #nextitem') + + def test_agenda_time_limit_removing_when_changing_item(self): + test = self.get_simple_agenda_test() + + test.process('20:13:50 #timelimit add 0:1 message') + assert(len(test.M.config.agenda.reminders) == 1) + test.process('20:13:50 #nextitem') + assert(len(test.M.config.agenda.reminders) == 0) + test.process('20:13:50 #timelimit add 0:1 message') + assert(len(test.M.config.agenda.reminders) == 1) + test.process('20:13:50 #previtem') + assert(len(test.M.config.agenda.reminders) == 0) + + def test_agenda_time_limit_manual_removing(self): + test = self.get_simple_agenda_test() + + test.process('20:13:50 #timelimit add 0:1 message') + test.process('20:13:50 #timelimit add 0:1 other message') + keys = test.M.config.agenda.reminders.keys() + keys.sort() + assert(keys == ['message', 'other message']) + + test.answer_should_match('20:13:50 #timelimit remove other message', 'Reminder "other message" removed') + keys = test.M.config.agenda.reminders.keys() + assert(keys == ['message']) + + def test_agenda_time_limit_listing(self): + test = self.get_simple_agenda_test() + test.process('20:13:50 #timelimit add 0:1 message') + test.process('20:13:50 #timelimit add 0:1 other message') + test.process('20:13:50 #timelimit add 0:1 yet another message') + keys = test.M.config.agenda.reminders.keys() + test.answer_should_match('20:13:50 #timelimit list', + 'Set reminders: "' + '", "'.join(keys) + '"') + if __name__ == '__main__': os.chdir(os.path.join(os.path.dirname(__file__), '.')) -- cgit v1.2.3-65-gdbad From 13bef04afeacceb530aa159cfb5ada789b7f4380 Mon Sep 17 00:00:00 2001 From: Joachim Filip Ignacy Bartosik Date: Fri, 24 Jun 2011 15:00:40 +0200 Subject: Send timelimits to bot --- site/app/models/agenda.rb | 2 +- site/app/models/agenda_item.rb | 13 +++++++++++++ site/db/schema.rb | 3 ++- site/spec/models/agenda_item_spec.rb | 19 +++++++++++++++++++ site/spec/models/agenda_spec.rb | 17 +++++++++-------- 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb index baaac89..65f45b4 100644 --- a/site/app/models/agenda.rb +++ b/site/app/models/agenda.rb @@ -91,7 +91,7 @@ class Agenda < ActiveRecord::Base def voting_array agenda_items.collect do |item| - [item.title, item.voting_options.*.description] + [item.title, item.voting_options.*.description, item.timelimits] end end diff --git a/site/app/models/agenda_item.rb b/site/app/models/agenda_item.rb index f590bb1..0ce60ea 100644 --- a/site/app/models/agenda_item.rb +++ b/site/app/models/agenda_item.rb @@ -7,6 +7,7 @@ class AgendaItem < ActiveRecord::Base discussion :string body :text rejected :boolean, :default => false + timelimits :text, :null => false, :default => '' timestamps end @@ -14,6 +15,8 @@ class AgendaItem < ActiveRecord::Base belongs_to :agenda has_many :voting_options + validate :timelimits_entered_properly + # --- Permissions --- # def create_permitted? return false if acting_user.guest? @@ -50,4 +53,14 @@ class AgendaItem < ActiveRecord::Base return false unless agenda.nil? return acting_user == user if [nil, :title, :discussion, :body].include?(field) end + + protected + def timelimits_entered_properly + regexp = /^\d+:\d+( .*)?$/ + for line in timelimits.split("\n") + unless line.match regexp + errors.add(:timelimits, "Line '#{line}' doensn't match ': '") + end + end + end end diff --git a/site/db/schema.rb b/site/db/schema.rb index 071ff84..bc8535a 100644 --- a/site/db/schema.rb +++ b/site/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110606170332) do +ActiveRecord::Schema.define(:version => 20110624141720) do create_table "agenda_items", :force => true do |t| t.string "title" @@ -21,6 +21,7 @@ ActiveRecord::Schema.define(:version => 20110606170332) do t.datetime "updated_at" t.integer "user_id" t.integer "agenda_id" + t.text "timelimits", :default => "", :null => false end add_index "agenda_items", ["agenda_id"], :name => "index_agenda_items_on_agenda_id" diff --git a/site/spec/models/agenda_item_spec.rb b/site/spec/models/agenda_item_spec.rb index 3df2a59..72ee0bb 100644 --- a/site/spec/models/agenda_item_spec.rb +++ b/site/spec/models/agenda_item_spec.rb @@ -100,4 +100,23 @@ describe AgendaItem do a.should_not be_editable_by(u, :agenda) end end + + it 'should make sure timelimits are valid' do + valid_timelimits = ["", "0:0", "1:1 message", "1:2 longer message", + "30:40 a few messages\n5:60 as separate lines"] + invalid_timelimits = ["a:0", "1:", "2:a", ":0", " 1:1 message", + "30:40 a few messages\n\n5:60 and an empty line", + "30:40 a few messages\n5:60 and an wrong line\na:"] + + valid_timelimits.each do |limit| + Factory(:agenda_item, :timelimits => limit).should be_valid + end + + invalid_timelimits.each do |limit| + item = AgendaItem.new :title => 'title', :timelimits => limit + item.should_not be_valid + item.errors.length.should be_equal(1) + item.errors[:timelimits].should_not be_nil + end + end end diff --git a/site/spec/models/agenda_spec.rb b/site/spec/models/agenda_spec.rb index b9fbd36..34db02f 100644 --- a/site/spec/models/agenda_spec.rb +++ b/site/spec/models/agenda_spec.rb @@ -126,9 +126,9 @@ describe Agenda do Vote.count.should be_equal(9) - u[0].votes.*.voting_option.*.description.should == ['Yes', 'Yes', 'Dunno'] - u[1].votes.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno'] - u[2].votes.*.voting_option.*.description.should == ['Yes', 'Dunno', 'No'] + u[0].votes.*.voting_option.*.description.sort.should == ['Dunno', 'Yes', 'Yes'] + u[1].votes.*.voting_option.*.description.sort.should == ['Dunno', 'No', 'Yes'] + u[2].votes.*.voting_option.*.description.sort.should == ['Dunno', 'No', 'Yes'] a1.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'Yes', 'Yes'] a2.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno'] a3.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['No', 'Dunno', 'Dunno'] @@ -236,15 +236,16 @@ describe Agenda do it 'should return proper voting_array' do old_agenda = Factory(:agenda, :state => 'old') current_agenda = Factory(:agenda) - i1 = Factory(:agenda_item, :agenda => old_agenda) - i2 = Factory(:agenda_item, :agenda => current_agenda) - i3 = Factory(:agenda_item, :agenda => current_agenda) + i1 = Factory(:agenda_item, :agenda => old_agenda, :timelimits => '0:0') + i2 = Factory(:agenda_item, :agenda => current_agenda, :timelimits => "10:0 Ten minutes passed") + i3 = Factory(:agenda_item, :agenda => current_agenda, :timelimits => "0:10 Ten seconds passed") v11 = Factory(:voting_option, :agenda_item => i1) v21 = Factory(:voting_option, :agenda_item => i2) v22 = Factory(:voting_option, :agenda_item => i2, :description => 'other') - old_agenda.voting_array.should == [[i1.title, [v11.description]]] - current_agenda.voting_array.should == [[i2.title, [v21.description, v22.description]], [i3.title, []]] + old_agenda.voting_array.should == [[i1.title, [v11.description], i1.timelimits]] + current_agenda.voting_array.should == [[i2.title, [v21.description, v22.description], + i2.timelimits], [i3.title, [], i3.timelimits]] end end -- cgit v1.2.3-65-gdbad From 4430af0e8a05b3424b88ee9803034959b5e46dec Mon Sep 17 00:00:00 2001 From: Joachim Filip Ignacy Bartosik Date: Fri, 24 Jun 2011 19:37:23 +0200 Subject: Bot receives reminders from webapp --- bot/ircmeeting/agenda.py | 18 ++++++++++++------ bot/ircmeeting/meeting.py | 4 ++-- bot/tests/run_test.py | 19 ++++++++++++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/bot/ircmeeting/agenda.py b/bot/ircmeeting/agenda.py index 220acf9..101c73c 100644 --- a/bot/ircmeeting/agenda.py +++ b/bot/ircmeeting/agenda.py @@ -50,30 +50,36 @@ class Agenda(object): else: return self.empty_agenda_msg - def _swich_agenda_item_to(self, new_item): + def _swich_agenda_item_to(self, new_item, irc): self._current_item = new_item for reminder in self.reminders.values(): reminder.cancel() self.reminders = {} - - def next_agenda_item(self): + for line in self._agenda[self._current_item][2].split('\n'): + match = re.match( '([0-9]+):([0-9]+) (.*)', line) + if match: + self.add_timelimit(int(match.group(1)), int(match.group(2)), + match.group(3), irc) + self._agenda[self._current_item][2] = '' + + def next_agenda_item(self, irc): if not self.conf.manage_agenda: return('') if self._vote_open: return self.voting_open_so_item_not_changed_msg else: if (self._current_item + 1) < len(self._agenda): - self._swich_agenda_item_to(self._current_item + 1) + self._swich_agenda_item_to(self._current_item + 1, irc) return(self.get_agenda_item()) - def prev_agenda_item(self): + def prev_agenda_item(self, irc): if not self.conf.manage_agenda: return('') if self._vote_open: return self.voting_open_so_item_not_changed_msg else: if self._current_item > 0: - self._swich_agenda_item_to(self._current_item - 1) + self._swich_agenda_item_to(self._current_item - 1, irc) return(self.get_agenda_item()) def start_vote(self): diff --git a/bot/ircmeeting/meeting.py b/bot/ircmeeting/meeting.py index 4bd3221..e3cf38d 100644 --- a/bot/ircmeeting/meeting.py +++ b/bot/ircmeeting/meeting.py @@ -323,10 +323,10 @@ class MeetingCommands(object): self.reply(self.config.agenda.get_agenda_item()) def do_nextitem(self, nick, time_, line, **kwargs): - self.reply(self.config.agenda.next_agenda_item()) + self.reply(self.config.agenda.next_agenda_item(self)) def do_previtem(self, nick, time_, line, **kwargs): - self.reply(self.config.agenda.prev_agenda_item()) + self.reply(self.config.agenda.prev_agenda_item(self)) def do_timelimit(self, nick, time_, line, **kwargs): reply = 'Usage "#timelimit add : " or ' +\ diff --git a/bot/tests/run_test.py b/bot/tests/run_test.py index e3a9030..e22516b 100644 --- a/bot/tests/run_test.py +++ b/bot/tests/run_test.py @@ -344,7 +344,7 @@ class MeetBotTest(unittest.TestCase): def get_simple_agenda_test(self): test = test_meeting.TestMeeting() test.set_voters(['x', 'z']) - test.set_agenda([['first item', ['opt1', 'opt2']], ['second item', []]]) + test.set_agenda([['first item', ['opt1', 'opt2'], ''], ['second item', [], '']]) test.M.config.manage_agenda = False test.answer_should_match("20:13:50 #startmeeting", @@ -483,6 +483,23 @@ class MeetBotTest(unittest.TestCase): test.answer_should_match('20:13:50 #timelimit list', 'Set reminders: "' + '", "'.join(keys) + '"') + def test_preset_agenda_time_limits(self): + test = self.get_simple_agenda_test() + test.M.config.agenda._agenda[0][2] = '1:0 message' + test.M.config.agenda._agenda[1][2] = '1:0 another message\n0:10 some other message' + + test.process('20:13:50 #nextitem') + keys = test.M.config.agenda.reminders.keys() + keys.sort() + assert(keys == ['another message', 'some other message']) + + test.process('20:13:50 #previtem') + keys = test.M.config.agenda.reminders.keys() + keys.sort() + assert(keys == ['message']) + + test.process('20:13:50 #nextitem') + if __name__ == '__main__': os.chdir(os.path.join(os.path.dirname(__file__), '.')) -- cgit v1.2.3-65-gdbad