diff --git a/plugins/Poll.py b/plugins/Poll.py index 4cd80e32e..1224305d5 100644 --- a/plugins/Poll.py +++ b/plugins/Poll.py @@ -72,93 +72,46 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler): cursor = db.cursor() cursor.execute("""CREATE TABLE polls ( id INTEGER PRIMARY KEY, - question TEXT, + question TEXT UNIQUE ON CONFLICT IGNORE, started_by INTEGER, - expires INTEGER)""") + open INTEGER)""") cursor.execute("""CREATE TABLE options ( + id INTEGER PRIMARY KEY, poll_id INTEGER, - option_id INTEGER, option TEXT, - votes INTEGER, - PRIMARY KEY (poll_id, option_id) - ON CONFLICT IGNORE)""") + UNIQUE (poll_id, id) ON CONFLICT IGNORE)""") cursor.execute("""CREATE TABLE votes ( user_id INTEGER, poll_id INTEGER, - option_id INTERER)""") + option_id INTEGER, + UNIQUE (user_id, poll_id) + ON CONFLICT IGNORE)""") db.commit() return db - def new(self, irc, msg, args): - """[] [] + def open(self, irc, msg, args): + """[] - Creates a new poll with the given question and optional lifespan. - Without a lifespan the poll will never expire and accept voting - until it is closed. + Creates a new poll with the given question. """ channel = privmsgs.getChannel(msg, args) - (lifespan, question) = privmsgs.getArgs(args, optional=1) - try: - lifespan = int(lifespan) - except ValueError: - if question: - question = '%s %s' % (lifespan, question) - else: - question = lifespan - lifespan = 0 - if lifespan: - lifespan += time.time() - if not question: - raise callbacks.ArgumentError + question = privmsgs.getArgs(args) + # Must be registered to create a poll try: userId = ircdb.users.getUserId(msg.prefix) except KeyError: irc.error(msg, conf.replyNotRegistered) return - db = self.getDb(channel) cursor = db.cursor() - cursor.execute("""INSERT INTO polls VALUES - (NULL, %%s, %s, %s)""" % (userId, lifespan), - question) + 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] irc.reply(msg, '%s (poll #%s)' % (conf.replySuccess, id)) - def open(self, irc, msg, args): - """[] [] - - Reopens a closed poll with the given and optional lifespan. - Without a lifespan the poll will never expire and accept voting - until it is closed. - """ - channel = privmsgs.getChannel(msg, args) - (lifespan, id) = privmsgs.getArgs(args, optional=1) - if not id: - id = lifespan - lifespan = 0 - else: - try: - lifespan = int(lifespan) - except ValueError: - irc.error(msg, 'The argument must be an integer.') - return - try: - id = int(id) - except ValueError: - irc.error(msg, 'The argument must be an integer.') - return - if lifespan: - lifespan += time.time() - - db = self.getDb(channel) - cursor = db.cursor() - cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""" % \ - (lifespan, id)) - db.commit() - irc.reply(msg, conf.replySuccess) - def close(self, irc, msg, args): """[] @@ -169,67 +122,65 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler): try: id = int(id) except ValueError: - irc.error(msg, 'The argument must be an integer.') + irc.error(msg, 'The id must be an integer.') return - db = self.getDb(channel) cursor = db.cursor() - cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""" % \ - (int(time.time()), id)) - db.commit() + # Check to make sure that the poll exists + cursor.execute("""SELECT id FROM polls WHERE id=%s""", id) + if cursor.rowcount == 0: + irc.error(msg, 'Id #%s is not an existing poll.') + return + cursor.execute("""UPDATE polls SET open=0 WHERE id=%s""", id) irc.reply(msg, conf.replySuccess) def add(self, irc, msg, args): - """[]