diff --git a/plugins/Poll.py b/plugins/Poll.py index 5a13454a9..3cc1a6327 100644 --- a/plugins/Poll.py +++ b/plugins/Poll.py @@ -84,59 +84,70 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler): db.commit() return db + def _getUserId(self, prefix): + try: + return ircdb.users.getUserId(prefix) + except KeyError: + irc.errorNotRegistered(Raise=True) + + def _getUserName(self, id): + try: + return ircdb.users.getUser(int(id)).name + except KeyError: + return 'an unknown user' + + def _getId(self, idStr): + try: + return int(idStr) + except ValueError: + irc.error('The id must be a valid integer.', Raise=True) + def poll(self, irc, msg, args): - """ + """[] Displays the poll question and options for the given poll id. + is only necessary if the message isn't sent in the channel + itself. """ channel = privmsgs.getChannel(msg, args) poll_id = privmsgs.getArgs(args) db = self.getDb(channel) cursor = db.cursor() - cursor.execute("""SELECT id, question, started_by, open - FROM polls WHERE id=%s""", - poll_id) + cursor.execute("""SELECT question, started_by, open + FROM polls WHERE id=%s""", poll_id) if cursor.rowcount == 0: - irc.error('There is no poll with id %s' % poll_id) + irc.error('There is no poll with id %s.' % poll_id) return - _, question, started_by, open = cursor.fetchone() + question, started_by, open = cursor.fetchone() starter = ircdb.users.getUser(started_by).name if open: statusstr = 'open' else: statusstr = 'closed' - cursor.execute("""SELECT id, option FROM options - WHERE poll_id=%s""", - poll_id) + cursor.execute("""SELECT id, option FROM options WHERE poll_id=%s""", + poll_id) if cursor.rowcount == 0: optionstr = 'This poll has no options yet' else: options = cursor.fetchall() - optionstr = 'Options:' - optionstr += ''.join([' %s: %r' % (id, option) - for id, option in options]) - pollstr = 'Poll #%s: %r started by %s. %s. Poll is %s.' % \ + optionstr = 'Options: ' + ' '.join(['%s: %r' % t for t in options]) + pollstr = 'Poll #%s: %r started by %s. %s. Poll is %s.' % \ (poll_id, question, starter, optionstr, statusstr) irc.reply(pollstr) def open(self, irc, msg, args): """[] - Creates a new poll with the given question. + Creates a new poll with the given question. is only + necessary if the message isn't sent in the channel itself. """ channel = privmsgs.getChannel(msg, args) question = privmsgs.getArgs(args) - # Must be registered to create a poll - try: - userId = ircdb.users.getUserId(msg.prefix) - except KeyError: - irc.errorNotRegistered() - return + userId = self._getUserId(msg.prefix) db = self.getDb(channel) cursor = db.cursor() - cursor.execute("""INSERT INTO polls - VALUES (NULL, %s, %s, 1)""", - question, userId) + cursor.execute("""INSERT INTO polls VALUES (NULL, %s, %s, 1)""", + question, userId) db.commit() cursor.execute("""SELECT id FROM polls WHERE question=%s""", question) id = cursor.fetchone()[0] @@ -146,14 +157,12 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler): """[] Closes the poll with the given ; further votes will not be allowed. + is only necessary if the message isn't sent in the channel + itself. """ channel = privmsgs.getChannel(msg, args) id = privmsgs.getArgs(args) - try: - id = int(id) - except ValueError: - irc.error('The id must be an integer.') - return + id = self._getId(id) db = self.getDb(channel) cursor = db.cursor() # Check to make sure that the poll exists @@ -168,19 +177,13 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler): """[]