Some small fixes to Poll.

This commit is contained in:
Stéphan Kochen 2003-11-22 00:31:55 +00:00
parent e7fd4cafbb
commit 7b26501f15

View File

@ -42,6 +42,7 @@ import ircutils
import privmsgs import privmsgs
import callbacks import callbacks
import conf import conf
import debug
import os.path import os.path
import time import time
@ -71,13 +72,14 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
question TEXT, question TEXT,
started_by INTEGER, started_by INTEGER,
expires TIMESTAMP)""") expires INTEGER)""")
cursor.execute("""CREATE TABLE options ( cursor.execute("""CREATE TABLE options (
poll_id INTEGER, poll_id INTEGER,
option_id INTEGER, option_id INTEGER,
option TEXT, option TEXT,
votes INTEGER, votes INTEGER,
UNIQUE (poll_id, option_id) ON CONFLICT IGNORE)""") PRIMARY KEY (poll_id, option_id)
ON CONFLICT IGNORE)""")
cursor.execute("""CREATE TABLE votes ( cursor.execute("""CREATE TABLE votes (
user_id INTEGER, user_id INTEGER,
poll_id INTEGER, poll_id INTEGER,
@ -95,14 +97,14 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
(lifespan, question) = privmsgs.getArgs(args, optional=1) (lifespan, question) = privmsgs.getArgs(args, optional=1)
try: try:
lifespan = float(lifespan) lifespan = int(lifespan)
except ValueError: except ValueError:
if question: if question:
question = '%s %s' % (lifespan, question) question = '%s %s' % (lifespan, question)
else: else:
question = lifespan question = lifespan
lifespan = 0.0 lifespan = 0
if lifespan != 0.0: if lifespan:
lifespan += time.time() lifespan += time.time()
if not question: if not question:
raise callbacks.ArgumentError raise callbacks.ArgumentError
@ -115,8 +117,8 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""INSERT INTO polls VALUES cursor.execute("""INSERT INTO polls VALUES
(NULL, %s, %s, %s)""", question, (NULL, %%s, %s, %s)""" % (userId, lifespan),
userId, lifespan) question)
db.commit() db.commit()
cursor.execute("""SELECT id FROM polls WHERE question=%s""", question) cursor.execute("""SELECT id FROM polls WHERE question=%s""", question)
id = cursor.fetchone()[0] id = cursor.fetchone()[0]
@ -136,7 +138,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
lifespan = 0 lifespan = 0
else: else:
try: try:
lifespan = float(lifespan) lifespan = int(lifespan)
except ValueError: except ValueError:
irc.error(msg, 'The <lifespan> argument must be an integer.') irc.error(msg, 'The <lifespan> argument must be an integer.')
return return
@ -145,13 +147,13 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
except ValueError: except ValueError:
irc.error(msg, 'The <id> argument must be an integer.') irc.error(msg, 'The <id> argument must be an integer.')
return return
if lifespan != 0.0: if lifespan:
lifespan += time.time() lifespan += time.time()
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""", cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""" % \
lifespan, id) (lifespan, id))
db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
@ -170,8 +172,8 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""", cursor.execute("""UPDATE polls SET expires=%s WHERE id=%s""" % \
int(time.time()), id) (int(time.time()), id))
db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
@ -211,44 +213,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
WHERE poll_id=%s WHERE poll_id=%s
AND votes=0 AND votes=0
AND option=%%s""" % id, option) AND option=%%s""" % id, option)
id = cursor.fetchone()[0] irc.reply(msg, '%s (option #%s)' % (conf.replySuccess, cursor.fetchone()[0]))
irc.reply(msg, '%s (option #%s)' % (conf.replySuccess, id))
def remove(self, irc, msg, args):
"""[<channel>] <poll id> <option id>
Remove option <option id> from poll <poll id>.
"""
channel = privmsgs.getChannel(msg, args)
(pollId, optionId) = privmsgs.getArgs(args, required=2)
try:
pollId = int(pollId)
optionId = int(optionId)
except ValueError:
irc.error(msg, 'The <poll id> and <option id> '
'arguments must be integers.')
return
try:
userId = ircdb.users.getUserId(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNotRegistered)
return
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT started_by FROM polls WHERE id=%s""" % pollId)
if cursor.rowcount == 0:
irc.error(msg, 'There is no such poll.')
return
elif userId != cursor.fetchone()[0]:
irc.error(msg, 'That poll isn\'t yours.')
return
cursor.execute("""DELETE FROM options
WHERE poll_id=%s
AND option_id=%s""" % (pollId, optionId))
irc.reply(msg, conf.replySuccess)
def vote(self, irc, msg, args): def vote(self, irc, msg, args):
"""[<channel>] <poll id> <option id> """[<channel>] <poll id> <option id>
@ -274,12 +239,12 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT expires cursor.execute("""SELECT expires
FROM polls WHERE id=%s""", id) FROM polls WHERE id=%s""" % id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'There is no such poll.') irc.error(msg, 'There is no such poll.')
return return
expires = float(cursor.fetchone()[0]) expires = cursor.fetchone()[0]
if expires != 0.0 and time.time() >= expires: if expires and time.time() >= expires:
irc.error(msg, 'That poll is closed.') irc.error(msg, 'That poll is closed.')
return return
@ -291,10 +256,10 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
return return
cursor.execute("""SELECT vote FROM votes WHERE user_id=%s cursor.execute("""SELECT vote FROM votes WHERE user_id=%s
AND poll_id=%s""", userId, id) AND poll_id=%s""" % (userId, id))
if cursor.rowcount == 0: if cursor.rowcount == 0:
cursor.execute("""INSERT INTO votes VALUES (%s, %s, %s)""", cursor.execute("""INSERT INTO votes VALUES (%s, %s, %s)""" % \
userId, id, option) (userId, id, option))
db.commit() db.commit()
irc.reply(msg, 'You voted option #%s on poll #%s.' % (option, id)) irc.reply(msg, 'You voted option #%s on poll #%s.' % (option, id))
else: else:
@ -310,7 +275,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
WHERE poll_id=%s AND option_id=%s""" \ WHERE poll_id=%s AND option_id=%s""" \
% (id, option)) % (id, option))
cursor.execute("""UPDATE votes SET option_id=%s WHERE user_id=%s cursor.execute("""UPDATE votes SET option_id=%s WHERE user_id=%s
AND poll_id=%s""", option, userId, id) AND poll_id=%s""" % (option, userId, id))
db.commit() db.commit()
irc.reply(msg, 'Your vote on poll #%s has been updated to option ' irc.reply(msg, 'Your vote on poll #%s has been updated to option '
'#%s.' % (id, option)) '#%s.' % (id, option))
@ -330,7 +295,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT * FROM polls WHERE id=%s""", id) cursor.execute("""SELECT * FROM polls WHERE id=%s""" % id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'There is no such poll.') irc.error(msg, 'There is no such poll.')
return return
@ -340,7 +305,8 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
except KeyError: except KeyError:
startedBy = 'an unknown user' startedBy = 'an unknown user'
return return
reply = 'Results for poll #%s: "%s" by %s' % (id, question, startedBy) reply = 'Results for poll #%s: "%s" by %s' % \
(ircutils.bold(id), question, ircutils.bold(startedBy))
cursor.execute("""SELECT option_id, option, votes FROM options cursor.execute("""SELECT option_id, option, votes FROM options
WHERE poll_id=%s ORDER BY option_id""" % id) WHERE poll_id=%s ORDER BY option_id""" % id)
totalVotes = 0 totalVotes = 0
@ -348,29 +314,23 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
if cursor.rowcount == 0: if cursor.rowcount == 0:
reply = '%s - This poll has no options yet.' % reply reply = '%s - This poll has no options yet.' % reply
else: else:
L = cursor.fetchall() for (optionId, option, votes) in cursor.fetchall():
for (optionId, option, votes) in L: if votes == 0:
totalVotes += votes percent = 0
if totalVotes == 0: else:
reply = '%s - There have been no votes on this poll yet.' % reply percent = int(float(votes) / float(totalVotes) * 100.0)
else: results.append('%s. %s: %s (%s%%)'\
for (optionId, option, votes) in L: % (ircutils.bold(option_id), option,
if votes == 0: ircutils.bold(votes), percent))
percent = 0 reply = '%s - %s' % (reply, utils.commaAndify(results))
else: expires = int(expires)
percent = int(float(votes) / float(totalVotes) * 100.0) if expires:
results.append('%s. %s: %s (%s%%)'\
% (ircutils.bold(option_id), option,
ircutils.bold(votes), percent))
reply = '%s - %s' % (reply, utils.commaAndify(results))
expires = float(expires)
if expires != 0.0:
if time.time() >= expires: if time.time() >= expires:
reply = '%s - Poll is closed.' % reply reply = '%s - Poll is closed.' % reply
else: else:
expires -= time.time() expires -= time.time()
reply = '%s - Poll expires in %s' % (reply, reply = '%s - Poll expires in %s' % (reply,
utils.timeElapsed(int(expires))) utils.timeElapsed(expires))
irc.reply(msg, reply) irc.reply(msg, reply)