Some fixes to Poll, removed delete command, added example.

This commit is contained in:
Stéphan Kochen 2003-10-27 15:11:45 +00:00
parent 9caaf8fef9
commit 5ace5a48ad
2 changed files with 54 additions and 55 deletions

View File

@ -57,35 +57,58 @@ def configure(onStart, afterConnect, advanced):
onStart.append('load Poll') onStart.append('load Poll')
example = utils.wrapLines(""" example = utils.wrapLines("""
mooooo <G-LiTe> @load Poll
<supybot> G-LiTe: The operation succeeded.
<G-LiTe> @list Poll
<supybot> G-LiTe: close, new, open, poll, results, vote
<G-LiTe> @poll new Got milk?
<supybot> G-LiTe: The operation succeeded. (poll #1)
<G-LiTe> @poll results 1
<supybot> G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - There have been no votes on this poll yet.
<G-LiTe> @poll vote 1 yes
<supybot> G-LiTe: You voted Yes on poll #1.
<G-LiTe> @poll results 1
<supybot> G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - Yes: 1 (100%), No: 0 (0%), Total votes: 1.
<G-LiTe> @poll close 1
<supybot> G-LiTe: The operation succeeded.
<G-LiTe> @poll results 1
<supybot> G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - Yes: 1 (100%), No: 0 (0%), Total votes: 1. Poll is closed.
<G-LiTe> @poll vote 1 no
<supybot> G-LiTe: Error: That poll is closed.
<G-LiTe> @poll open 384 1
<supybot> G-LiTe: The operation succeeded.
<G-LiTe> @poll vote 1 no
<supybot> G-LiTe: Your vote on poll #1 has been updated to No.
<G-LiTe> @poll results 1
<supybot> G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - Yes: 0 (0%), No: 1 (100%), Total votes: 1. Poll expires in 6 minutes and 17 seconds
""") """)
dbFilename = os.path.join(conf.dataDir, 'Poll.db') dbFilename = os.path.join(conf.dataDir, 'Poll.db')
def makeDb(dbfilename, replace=False): def makeDb(dbfilename):
if os.path.exists(dbfilename): if os.path.exists(dbfilename):
if replace: db = sqlite.connect(dbfilename)
os.remove(dbfilename) else:
db = sqlite.connect(dbfilename) db = sqlite.connect(dbfilename)
cursor = db.cursor() cursor = db.cursor()
try: try:
cursor.execute("""SELECT * FROM polls LIMIT 1""") cursor.execute("""SELECT * FROM polls LIMIT 1""")
except sqlite.DatabaseError: except sqlite.DatabaseError:
cursor.execute("""CREATE TABLE polls ( cursor.execute("""CREATE TABLE polls (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
question TEXT, question TEXT,
started_by INTEGER, started_by INTEGER,
yes INTEGER, yes INTEGER,
no INTEGER, no INTEGER,
expires TIMESTAMP)""") expires TIMESTAMP)""")
try: try:
cursor.execute("""SELECT * FROM votes LIMIT 1""") cursor.execute("""SELECT * FROM votes LIMIT 1""")
except sqlite.DatabaseError: except sqlite.DatabaseError:
cursor.execute("""CREATE TABLE votes ( cursor.execute("""CREATE TABLE votes (
user_id INTEGER, user_id INTEGER,
poll_id INTEGER, poll_id INTEGER,
vote BOOLEAN)""") vote BOOLEAN)""")
db.commit() db.commit()
return db return db
class Poll(callbacks.Privmsg): class Poll(callbacks.Privmsg):
@ -98,7 +121,7 @@ class Poll(callbacks.Privmsg):
Creates a new poll with the given question and optional lifespan. Creates a new poll with the given question and optional lifespan.
Without a lifespan the poll will never expire and accept voting Without a lifespan the poll will never expire and accept voting
until it is closed or deleted. until it is closed or removed.
""" """
(lifespan, question) = privmsgs.getArgs(args, optional=1) (lifespan, question) = privmsgs.getArgs(args, optional=1)
try: try:
@ -133,7 +156,7 @@ class Poll(callbacks.Privmsg):
Reopens a closed poll with the given <id> and optional lifespan. Reopens a closed poll with the given <id> and optional lifespan.
Without a lifespan the poll will never expire and accept voting Without a lifespan the poll will never expire and accept voting
until it is closed or deleted. until it is closed or removed.
""" """
(lifespan, id) = privmsgs.getArgs(args, optional=1) (lifespan, id) = privmsgs.getArgs(args, optional=1)
if not id: if not id:
@ -177,25 +200,6 @@ class Poll(callbacks.Privmsg):
self.db.commit() self.db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
def delete(self, irc, msg, args):
"""<id>
Deletes the poll with the given <id> from the history (thus also
closing it if it's still active).
"""
id = privmsgs.getArgs(args)
try:
id = int(id)
except ValueError:
irc.error(msg, 'The <id> argument must be an integer.')
return
cursor = self.db.cursor()
cursor.execute("""DELETE FROM polls WHERE id=%s""", id)
cursor.execute("""DELETE FROM votes WHERE poll_id=%s""", id)
self.db.commit()
irc.reply(msg, conf.replySuccess)
def vote(self, irc, msg, args): def vote(self, irc, msg, args):
"""<id> <Yes,No> """<id> <Yes,No>
@ -221,7 +225,8 @@ class Poll(callbacks.Privmsg):
return return
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""SELECT yes, no, expires FROM polls WHERE id=%s""", id) cursor.execute("""SELECT yes, no, expires
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
@ -304,9 +309,9 @@ class Poll(callbacks.Privmsg):
if time.time() >= expires: if time.time() >= expires:
reply = '%s Poll is closed.' % reply reply = '%s Poll is closed.' % reply
else: else:
reply = '%s Poll expires on %s %s' % (reply, expires -= time.time()
time.asctime(time.localtime(expires)), reply = '%s Poll expires in %s' % (reply,
time.tzname[0]) utils.timeElapsed(expires))
irc.reply(msg, reply) irc.reply(msg, reply)

View File

@ -59,8 +59,6 @@ if sqlite is not None:
self.assertError('poll vote 1 Yes') self.assertError('poll vote 1 Yes')
self.assertNotError('poll open 1') self.assertNotError('poll open 1')
self.assertNotError('poll vote 1 Yes') self.assertNotError('poll vote 1 Yes')
self.assertNotError('poll delete 1')
self.assertError('poll vote 1 No')
def testOpen(self): def testOpen(self):
self.assertNotError('poll open 1') self.assertNotError('poll open 1')
@ -70,10 +68,6 @@ if sqlite is not None:
self.assertNotError('poll close 1') self.assertNotError('poll close 1')
self.assertError('poll close blah') self.assertError('poll close blah')
def testDelete(self):
self.assertNotError('poll delete 1')
self.assertError('poll delete blah')
def testVote(self): def testVote(self):
self.assertHelp('poll vote 1 blah') self.assertHelp('poll vote 1 blah')
self.assertError('poll vote blah Yes') self.assertError('poll vote blah Yes')