diff options
author | Joachim Filip Ignacy Bartosik <jbartosik@gmail.com> | 2011-05-23 13:17:40 +0200 |
---|---|---|
committer | Joachim Filip Ignacy Bartosik <jbartosik@gmail.com> | 2011-06-03 19:17:48 +0200 |
commit | d93c2e59b3e1b0a077c34ad9f560b79a9f1670df (patch) | |
tree | 1670c7654a56c33ac5b5c4b9688b50d3b7024e0a /bot/ircmeeting | |
parent | Add *.pyc to .gitignore (diff) | |
download | council-webapp-d93c2e59b3e1b0a077c34ad9f560b79a9f1670df.tar.gz council-webapp-d93c2e59b3e1b0a077c34ad9f560b79a9f1670df.tar.bz2 council-webapp-d93c2e59b3e1b0a077c34ad9f560b79a9f1670df.zip |
Bot obtains voters, agenda items and voting options lists from webapp
Diffstat (limited to 'bot/ircmeeting')
-rw-r--r-- | bot/ircmeeting/agenda.py | 91 | ||||
-rw-r--r-- | bot/ircmeeting/meeting.py | 26 |
2 files changed, 115 insertions, 2 deletions
diff --git a/bot/ircmeeting/agenda.py b/bot/ircmeeting/agenda.py new file mode 100644 index 0000000..7ee9beb --- /dev/null +++ b/bot/ircmeeting/agenda.py @@ -0,0 +1,91 @@ +import json +import urllib + +class Agenda(object): + + # Messages + empty_agenda_msg = "Agenda is empty so I can't help you manage meeting (and voting)." + current_item_msg = "Current agenda item is {}." + voting_already_open_msg = "Voting is already open. You can end it with #endvote." + voting_open_msg = "Voting started. Your choices are: {} Vote #vote <option number>.\n End voting with #endvote." + voting_close_msg = "Voting is closed." + voting_already_closed_msg = "Voting is already closed. You can start it with #startvote." + voting_open_so_item_not_changed_msg = "Voting is currently open so I didn't change item. Please #endvote first" + can_not_vote_msg = "You can not vote. Only {} can vote" + 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 #{} - {}" + + # Internal + _voters = [] + _votes = [] + _agenda = [] + _current_item = 0 + _vote_open = False + + def __init__(self, conf): + self.conf = conf + + def get_agenda_item(self): + if self._current_item < len(self._agenda): + return str.format(self.current_item_msg, self._agenda[self._current_item][0]) + else: + return self.empty_agenda_msg + + def next_agenda_item(self): + if self._vote_open: + return voting_open_so_item_not_changed_msg + else: + if (self._current_item + 1) < len(self._agenda): + self._current_item += 1 + return(self.get_agenda_item()) + + def prev_agenda_item(self): + if self._vote_open: + return voting_open_so_item_not_changed_msg + else: + if self._current_item > 0: + self._current_item -= 1 + return(self.get_agenda_item()) + + def start_vote(self): + if self._vote_open: + return self.voting_already_open_msg + self._vote_open = True + options = "\n" + for i in range(len(self._agenda[self._current_item][1])): + options += str.format("{}. {}\n", i, self._agenda[self._current_item][i]) + return str.format(self.voting_open_msg, options) + + def end_vote(self): + if self._vote_open: + self._vote_open = False + return voting_already_closed_msg + return voting_close_msg + + def get_data(self): + self._voters = self._get_json(self.conf.voters_url) + self._agenda = self._get_json(self.conf.agenda_url) + self._votes = { } + for i in self._agenda: + self._votes[i[0]] = { } + + def vote(self, nick, line): + if not nick in self._voters: + return str.format(self.can_not_vote_msg, ", ".join(self._voters)) + if not line.isdigit(): + return self.not_a_number_msg + + opt = int(line) + + if opt < 0 or opt >= len(self._agenda[self._current_item][1]): + return self.out_of_range_msg + + self._votes[self._agenda[self._current_item][0]][nick] = self._agenda[self._current_item][1][opt] + return str.format(self.vote_confirm_msg, opt, self._agenda[self._current_item][opt]) + + def _get_json(self, url): + str = urllib.urlopen(url).read() + str = urllib.unquote(str) + result = json.loads(str) + return result diff --git a/bot/ircmeeting/meeting.py b/bot/ircmeeting/meeting.py index 85880a6..b22dac6 100644 --- a/bot/ircmeeting/meeting.py +++ b/bot/ircmeeting/meeting.py @@ -36,6 +36,7 @@ import stat import writers import items +import agenda reload(writers) reload(items) @@ -96,6 +97,11 @@ class Config(object): input_codec = 'utf-8' output_codec = 'utf-8' # Functions to do the i/o conversion. + + # Meeting management urls + voters_url = 'http://localhost:3000/users/voters' + agenda_url = 'http://localhost:3000/agendas/current_items' + def enc(self, text): return text.encode(self.output_codec, 'replace') def dec(self, text): @@ -118,7 +124,6 @@ class Config(object): #'.rst.html':writers.HTMLfromReST, } - def __init__(self, M, writeRawLog=False, safeMode=False, extraConfig={}): self.M = M @@ -134,6 +139,7 @@ class Config(object): for extension, writer in self.writer_map.iteritems(): self.writers[extension] = writer(self.M) self.safeMode = safeMode + self.agenda = agenda.Agenda(self) def filename(self, url=False): # provide a way to override the filename. If it is # overridden, it must be a full path (and the URL-part may not @@ -308,6 +314,22 @@ class MeetingCommands(object): self.reply(messageline) if line.strip(): self.do_meetingtopic(nick=nick, line=line, time_=time_, **kwargs) + self.config.agenda.get_data() + self.reply(self.config.agenda.get_agenda_item()) + + def do_nextitem(self, nick, time_, line, **kwargs): + self.reply(self.config.agenda.next_agenda_item()) + + def do_previtem(self, nick, time_, line, **kwargs): + self.reply(self.config.agenda.prev_agenda_item()) + + def do_startvote(self, nick, time_, line, **kwargs): + for messageline in self.config.agenda.start_vote().split('\n'): + self.reply(messageline) + + def do_vote(self, nick, time_, line, **kwargs): + self.reply(self.config.agenda.vote(nick, line)) + def do_endmeeting(self, nick, time_, **kwargs): """End the meeting.""" if not self.isChair(nick): return @@ -454,7 +476,7 @@ class MeetingCommands(object): commands = [ "#"+x[3:] for x in dir(self) if x[:3]=="do_" ] commands.sort() self.reply("Available commands: "+(" ".join(commands))) - + class Meeting(MeetingCommands, object): |