mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Added some poll enhancements
This commit is contained in:
parent
9873981fd8
commit
c7f50eb31c
@ -79,7 +79,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
started_by INTEGER,
|
started_by INTEGER,
|
||||||
open INTEGER)""")
|
open INTEGER)""")
|
||||||
cursor.execute("""CREATE TABLE options (
|
cursor.execute("""CREATE TABLE options (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER,
|
||||||
poll_id INTEGER,
|
poll_id INTEGER,
|
||||||
option TEXT,
|
option TEXT,
|
||||||
UNIQUE (poll_id, id) ON CONFLICT IGNORE)""")
|
UNIQUE (poll_id, id) ON CONFLICT IGNORE)""")
|
||||||
@ -92,6 +92,41 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
db.commit()
|
db.commit()
|
||||||
return db
|
return db
|
||||||
|
|
||||||
|
def poll(self, irc, msg, args):
|
||||||
|
"""<id>
|
||||||
|
|
||||||
|
Displays the poll question and options for the given poll id.
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.error(msg, 'There is no poll with id %s' % poll_id)
|
||||||
|
return
|
||||||
|
_, 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)
|
||||||
|
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.' % \
|
||||||
|
(poll_id, question, starter, optionstr, statusstr)
|
||||||
|
irc.reply(msg, pollstr)
|
||||||
|
|
||||||
def open(self, irc, msg, args):
|
def open(self, irc, msg, args):
|
||||||
"""[<channel>] <question>
|
"""[<channel>] <question>
|
||||||
|
|
||||||
@ -156,6 +191,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
return
|
return
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
# Only the poll starter or an admin can add options
|
||||||
cursor.execute("""SELECT started_by FROM polls
|
cursor.execute("""SELECT started_by FROM polls
|
||||||
WHERE id=%s""",
|
WHERE id=%s""",
|
||||||
poll_id)
|
poll_id)
|
||||||
@ -166,9 +202,24 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
(ircdb.checkCapability(userId, 'admin'))):
|
(ircdb.checkCapability(userId, 'admin'))):
|
||||||
irc.error(msg, 'That poll isn\'t yours and you aren\'t an admin.')
|
irc.error(msg, 'That poll isn\'t yours and you aren\'t an admin.')
|
||||||
return
|
return
|
||||||
|
# and NOBODY can add options once a poll has votes
|
||||||
|
cursor.execute("""SELECT COUNT(user_id) FROM votes
|
||||||
|
WHERE poll_id=%s""",
|
||||||
|
poll_id)
|
||||||
|
if cursor.fetchone()[0] != 0:
|
||||||
|
irc.error(msg, 'Cannot add options to a poll with votes.')
|
||||||
|
return
|
||||||
|
# Get the next highest id
|
||||||
|
cursor.execute("""SELECT MAX(id)+1 FROM options
|
||||||
|
WHERE poll_id=%s""",
|
||||||
|
poll_id)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
option_id = 1
|
||||||
|
else:
|
||||||
|
option_id = cursor.fetchone()[0]
|
||||||
cursor.execute("""INSERT INTO options VALUES
|
cursor.execute("""INSERT INTO options VALUES
|
||||||
(NULL, %s, %s)""",
|
(%s, %s, %s)""",
|
||||||
poll_id, option)
|
option_id, poll_id, option)
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
def vote(self, irc, msg, args):
|
def vote(self, irc, msg, args):
|
||||||
@ -243,7 +294,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
return
|
return
|
||||||
(id, question, startedBy, open) = cursor.fetchone()
|
(id, question, startedBy, open) = cursor.fetchone()
|
||||||
try:
|
try:
|
||||||
startedBy = ircdb.users.getUser(msg.prefix).name
|
startedBy = ircdb.users.getUser(startedBy).name
|
||||||
except KeyError:
|
except KeyError:
|
||||||
startedBy = 'an unknown user'
|
startedBy = 'an unknown user'
|
||||||
return
|
return
|
||||||
@ -253,17 +304,26 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
FROM votes
|
FROM votes
|
||||||
WHERE poll_id=%s
|
WHERE poll_id=%s
|
||||||
GROUP BY option_id
|
GROUP BY option_id
|
||||||
|
UNION
|
||||||
|
SELECT 0, id AS option_id
|
||||||
|
FROM options
|
||||||
|
WHERE poll_id=%s
|
||||||
|
AND id NOT IN (
|
||||||
|
SELECT option_id FROM votes
|
||||||
|
WHERE poll_id=%s)
|
||||||
|
GROUP BY option_id
|
||||||
ORDER BY count(user_id) DESC""",
|
ORDER BY count(user_id) DESC""",
|
||||||
poll_id)
|
poll_id, poll_id, poll_id)
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
s = 'This poll has no votes yet.'
|
s = 'This poll has no votes yet.'
|
||||||
else:
|
else:
|
||||||
results = []
|
results = []
|
||||||
for count, option_id in cursor.fetchall():
|
for count, option_id in cursor.fetchall():
|
||||||
cursor.execute("""SELECT option FROM options
|
cursor.execute("""SELECT option FROM options
|
||||||
WHERE id=%s""", option_id)
|
WHERE id=%s AND poll_id=%s""",
|
||||||
|
option_id, poll_id)
|
||||||
option = cursor.fetchone()[0]
|
option = cursor.fetchone()[0]
|
||||||
results.append('%s: %s' % (option, count))
|
results.append('%r: %s' % (option, int(count)))
|
||||||
s = utils.commaAndify(results)
|
s = utils.commaAndify(results)
|
||||||
reply += ' - %s' % s
|
reply += ' - %s' % s
|
||||||
irc.reply(msg, reply)
|
irc.reply(msg, reply)
|
||||||
@ -283,26 +343,6 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
polls = ['#%s: %r' % (id, q) for id, q in cursor.fetchall()]
|
polls = ['#%s: %r' % (id, q) for id, q in cursor.fetchall()]
|
||||||
irc.reply(msg, utils.commaAndify(polls))
|
irc.reply(msg, utils.commaAndify(polls))
|
||||||
|
|
||||||
def options(self, irc, msg, args):
|
|
||||||
"""<id>
|
|
||||||
|
|
||||||
Lists the options for the poll with the given id.
|
|
||||||
"""
|
|
||||||
channel = privmsgs.getChannel(msg, args)
|
|
||||||
poll_id = privmsgs.getArgs(args)
|
|
||||||
db = self.getDb(channel)
|
|
||||||
cursor = db.cursor()
|
|
||||||
cursor.execute("""SELECT id, option FROM options
|
|
||||||
WHERE poll_id=%s""",
|
|
||||||
poll_id)
|
|
||||||
if cursor.rowcount == 0:
|
|
||||||
irc.reply(msg, 'This poll has no options yet '
|
|
||||||
'or no such poll exists.')
|
|
||||||
return
|
|
||||||
options = ['%s: %r' % (id, o) for id, o in cursor.fetchall()]
|
|
||||||
irc.reply(msg, utils.commaAndify(options))
|
|
||||||
|
|
||||||
|
|
||||||
Class = Poll
|
Class = Poll
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
Reference in New Issue
Block a user