mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-30 14:59:34 +01:00
Style cleanups, some refactoring.
This commit is contained in:
parent
523204f080
commit
e9142dc880
122
plugins/Poll.py
122
plugins/Poll.py
@ -84,59 +84,70 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
db.commit()
|
db.commit()
|
||||||
return db
|
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):
|
def poll(self, irc, msg, args):
|
||||||
"""<id>
|
"""[<channel>] <id>
|
||||||
|
|
||||||
Displays the poll question and options for the given poll id.
|
Displays the poll question and options for the given poll id.
|
||||||
|
<channel> is only necessary if the message isn't sent in the channel
|
||||||
|
itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
poll_id = privmsgs.getArgs(args)
|
poll_id = privmsgs.getArgs(args)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("""SELECT id, question, started_by, open
|
cursor.execute("""SELECT question, started_by, open
|
||||||
FROM polls WHERE id=%s""",
|
FROM polls WHERE id=%s""", poll_id)
|
||||||
poll_id)
|
|
||||||
if cursor.rowcount == 0:
|
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
|
return
|
||||||
_, question, started_by, open = cursor.fetchone()
|
question, started_by, open = cursor.fetchone()
|
||||||
starter = ircdb.users.getUser(started_by).name
|
starter = ircdb.users.getUser(started_by).name
|
||||||
if open:
|
if open:
|
||||||
statusstr = 'open'
|
statusstr = 'open'
|
||||||
else:
|
else:
|
||||||
statusstr = 'closed'
|
statusstr = 'closed'
|
||||||
cursor.execute("""SELECT id, option FROM options
|
cursor.execute("""SELECT id, option FROM options WHERE poll_id=%s""",
|
||||||
WHERE poll_id=%s""",
|
poll_id)
|
||||||
poll_id)
|
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
optionstr = 'This poll has no options yet'
|
optionstr = 'This poll has no options yet'
|
||||||
else:
|
else:
|
||||||
options = cursor.fetchall()
|
options = cursor.fetchall()
|
||||||
optionstr = 'Options:'
|
optionstr = 'Options: ' + ' '.join(['%s: %r' % t for t in options])
|
||||||
optionstr += ''.join([' %s: %r' % (id, option)
|
pollstr = 'Poll #%s: %r started by %s. %s. Poll is %s.' % \
|
||||||
for id, option in options])
|
|
||||||
pollstr = 'Poll #%s: %r started by %s. %s. Poll is %s.' % \
|
|
||||||
(poll_id, question, starter, optionstr, statusstr)
|
(poll_id, question, starter, optionstr, statusstr)
|
||||||
irc.reply(pollstr)
|
irc.reply(pollstr)
|
||||||
|
|
||||||
def open(self, irc, msg, args):
|
def open(self, irc, msg, args):
|
||||||
"""[<channel>] <question>
|
"""[<channel>] <question>
|
||||||
|
|
||||||
Creates a new poll with the given question.
|
Creates a new poll with the given question. <channel> is only
|
||||||
|
necessary if the message isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
question = privmsgs.getArgs(args)
|
question = privmsgs.getArgs(args)
|
||||||
# Must be registered to create a poll
|
userId = self._getUserId(msg.prefix)
|
||||||
try:
|
|
||||||
userId = ircdb.users.getUserId(msg.prefix)
|
|
||||||
except KeyError:
|
|
||||||
irc.errorNotRegistered()
|
|
||||||
return
|
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("""INSERT INTO polls
|
cursor.execute("""INSERT INTO polls VALUES (NULL, %s, %s, 1)""",
|
||||||
VALUES (NULL, %s, %s, 1)""",
|
question, userId)
|
||||||
question, userId)
|
|
||||||
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]
|
||||||
@ -146,14 +157,12 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
"""[<channel>] <id>
|
"""[<channel>] <id>
|
||||||
|
|
||||||
Closes the poll with the given <id>; further votes will not be allowed.
|
Closes the poll with the given <id>; further votes will not be allowed.
|
||||||
|
<channel> is only necessary if the message isn't sent in the channel
|
||||||
|
itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
id = privmsgs.getArgs(args)
|
id = privmsgs.getArgs(args)
|
||||||
try:
|
id = self._getId(id)
|
||||||
id = int(id)
|
|
||||||
except ValueError:
|
|
||||||
irc.error('The id must be an integer.')
|
|
||||||
return
|
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
# Check to make sure that the poll exists
|
# Check to make sure that the poll exists
|
||||||
@ -168,19 +177,13 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
"""[<channel>] <id> <option text>
|
"""[<channel>] <id> <option text>
|
||||||
|
|
||||||
Add an option with the given text to the poll with the given id.
|
Add an option with the given text to the poll with the given id.
|
||||||
|
<channel> is only necessary if the message isn't sent in the channel
|
||||||
|
itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
(poll_id, option) = privmsgs.getArgs(args, required=2)
|
(poll_id, option) = privmsgs.getArgs(args, required=2)
|
||||||
try:
|
poll_id = self._getId(poll_id)
|
||||||
poll_id = int(poll_id)
|
userId = self._getUserId(msg.prefix)
|
||||||
except ValueError:
|
|
||||||
irc.error('The id must be an integer.')
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
userId = ircdb.users.getUserId(msg.prefix)
|
|
||||||
except KeyError:
|
|
||||||
irc.errorNotRegistered()
|
|
||||||
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
|
# Only the poll starter or an admin can add options
|
||||||
@ -216,21 +219,14 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
|
|
||||||
Vote for the option with the given id on the poll with the given poll
|
Vote for the option with the given id on the poll with the given poll
|
||||||
id. This command can also be used to override any previous vote.
|
id. This command can also be used to override any previous vote.
|
||||||
|
<channel> is only necesssary if the message isn't sent in the channel
|
||||||
|
itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
(poll_id, option_id) = privmsgs.getArgs(args, required=2)
|
(poll_id, option_id) = privmsgs.getArgs(args, required=2)
|
||||||
try:
|
poll_id = self._getId(poll_id)
|
||||||
poll_id = int(poll_id)
|
option_id = self._getId(option_id)
|
||||||
option_id = int(option_id)
|
userId = self._getUserId(msg.prefix)
|
||||||
except ValueError:
|
|
||||||
irc.error('The poll id and option id '
|
|
||||||
'arguments must be an integers.')
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
userId = ircdb.users.getUserId(msg.prefix)
|
|
||||||
except KeyError:
|
|
||||||
irc.errorNotRegistered()
|
|
||||||
return
|
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("""SELECT open
|
cursor.execute("""SELECT open
|
||||||
@ -264,15 +260,12 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
def results(self, irc, msg, args):
|
def results(self, irc, msg, args):
|
||||||
"""[<channel>] <id>
|
"""[<channel>] <id>
|
||||||
|
|
||||||
Shows the results for the poll with the given id.
|
Shows the results for the poll with the given id. <channel> is only
|
||||||
|
necessary if the message is not sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
poll_id = privmsgs.getArgs(args)
|
poll_id = privmsgs.getArgs(args)
|
||||||
try:
|
poll_id = self._getId(poll_id)
|
||||||
poll_id = int(poll_id)
|
|
||||||
except ValueError:
|
|
||||||
irc.error('The id argument must be an integer.')
|
|
||||||
return
|
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("""SELECT id, question, started_by, open
|
cursor.execute("""SELECT id, question, started_by, open
|
||||||
@ -282,11 +275,7 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
irc.error('There is no such poll.')
|
irc.error('There is no such poll.')
|
||||||
return
|
return
|
||||||
(id, question, startedBy, open) = cursor.fetchone()
|
(id, question, startedBy, open) = cursor.fetchone()
|
||||||
try:
|
startedBy = self._getUserName(startedBy)
|
||||||
startedBy = ircdb.users.getUser(startedBy).name
|
|
||||||
except KeyError:
|
|
||||||
startedBy = 'an unknown user'
|
|
||||||
return
|
|
||||||
reply = 'Results for poll #%s: "%s" by %s' % \
|
reply = 'Results for poll #%s: "%s" by %s' % \
|
||||||
(id, question, startedBy)
|
(id, question, startedBy)
|
||||||
cursor.execute("""SELECT count(user_id), option_id
|
cursor.execute("""SELECT count(user_id), option_id
|
||||||
@ -318,9 +307,10 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
irc.reply(reply)
|
irc.reply(reply)
|
||||||
|
|
||||||
def list(self, irc, msg, args):
|
def list(self, irc, msg, args):
|
||||||
"""takes no arguments.
|
"""[<channel>]
|
||||||
|
|
||||||
Lists the currently open polls for the channel and their ids.
|
Lists the currently open polls for <channel>. <channel> is only
|
||||||
|
necessary if the message isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
@ -328,9 +318,9 @@ class Poll(callbacks.Privmsg, plugins.ChannelDBHandler):
|
|||||||
cursor.execute("""SELECT id, question FROM polls WHERE open=1""")
|
cursor.execute("""SELECT id, question FROM polls WHERE open=1""")
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.reply('This channel currently has no open polls.')
|
irc.reply('This channel currently has no open polls.')
|
||||||
return
|
else:
|
||||||
polls = ['#%s: %r' % (id, q) for id, q in cursor.fetchall()]
|
polls = ['#%s: %r' % t for t in cursor.fetchall()]
|
||||||
irc.reply(utils.commaAndify(polls))
|
irc.reply(utils.commaAndify(polls))
|
||||||
|
|
||||||
Class = Poll
|
Class = Poll
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user