diff --git a/plugins/Poll.py b/plugins/Poll.py index 000af2d7d..3c0966e40 100644 --- a/plugins/Poll.py +++ b/plugins/Poll.py @@ -57,35 +57,58 @@ def configure(onStart, afterConnect, advanced): onStart.append('load Poll') example = utils.wrapLines(""" -mooooo + @load Poll + G-LiTe: The operation succeeded. + @list Poll + G-LiTe: close, new, open, poll, results, vote + @poll new Got milk? + G-LiTe: The operation succeeded. (poll #1) + @poll results 1 + G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - There have been no votes on this poll yet. + @poll vote 1 yes + G-LiTe: You voted Yes on poll #1. + @poll results 1 + G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - Yes: 1 (100%), No: 0 (0%), Total votes: 1. + @poll close 1 + G-LiTe: The operation succeeded. + @poll results 1 + G-LiTe: Results for poll #1: "Got milk?" by G-LiTe - Yes: 1 (100%), No: 0 (0%), Total votes: 1. Poll is closed. + @poll vote 1 no + G-LiTe: Error: That poll is closed. + @poll open 384 1 + G-LiTe: The operation succeeded. + @poll vote 1 no + G-LiTe: Your vote on poll #1 has been updated to No. + @poll results 1 + 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') -def makeDb(dbfilename, replace=False): +def makeDb(dbfilename): if os.path.exists(dbfilename): - if replace: - os.remove(dbfilename) - db = sqlite.connect(dbfilename) - cursor = db.cursor() - try: - cursor.execute("""SELECT * FROM polls LIMIT 1""") - except sqlite.DatabaseError: - cursor.execute("""CREATE TABLE polls ( - id INTEGER PRIMARY KEY, - question TEXT, - started_by INTEGER, - yes INTEGER, - no INTEGER, - expires TIMESTAMP)""") - try: - cursor.execute("""SELECT * FROM votes LIMIT 1""") - except sqlite.DatabaseError: - cursor.execute("""CREATE TABLE votes ( - user_id INTEGER, - poll_id INTEGER, - vote BOOLEAN)""") - db.commit() + db = sqlite.connect(dbfilename) + else: + db = sqlite.connect(dbfilename) + cursor = db.cursor() + try: + cursor.execute("""SELECT * FROM polls LIMIT 1""") + except sqlite.DatabaseError: + cursor.execute("""CREATE TABLE polls ( + id INTEGER PRIMARY KEY, + question TEXT, + started_by INTEGER, + yes INTEGER, + no INTEGER, + expires TIMESTAMP)""") + try: + cursor.execute("""SELECT * FROM votes LIMIT 1""") + except sqlite.DatabaseError: + cursor.execute("""CREATE TABLE votes ( + user_id INTEGER, + poll_id INTEGER, + vote BOOLEAN)""") + db.commit() return db class Poll(callbacks.Privmsg): @@ -98,7 +121,7 @@ class Poll(callbacks.Privmsg): 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 or deleted. + until it is closed or removed. """ (lifespan, question) = privmsgs.getArgs(args, optional=1) try: @@ -133,7 +156,7 @@ class Poll(callbacks.Privmsg): 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 or deleted. + until it is closed or removed. """ (lifespan, id) = privmsgs.getArgs(args, optional=1) if not id: @@ -177,25 +200,6 @@ class Poll(callbacks.Privmsg): self.db.commit() irc.reply(msg, conf.replySuccess) - def delete(self, irc, msg, args): - """ - - Deletes the poll with the given 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 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): """ @@ -221,7 +225,8 @@ class Poll(callbacks.Privmsg): return 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: irc.error(msg, 'There is no such poll.') return @@ -304,9 +309,9 @@ class Poll(callbacks.Privmsg): if time.time() >= expires: reply = '%s Poll is closed.' % reply else: - reply = '%s Poll expires on %s %s' % (reply, - time.asctime(time.localtime(expires)), - time.tzname[0]) + expires -= time.time() + reply = '%s Poll expires in %s' % (reply, + utils.timeElapsed(expires)) irc.reply(msg, reply) diff --git a/test/test_Poll.py b/test/test_Poll.py index ac5a6e6c7..0b1248877 100644 --- a/test/test_Poll.py +++ b/test/test_Poll.py @@ -59,8 +59,6 @@ if sqlite is not None: self.assertError('poll vote 1 Yes') self.assertNotError('poll open 1') self.assertNotError('poll vote 1 Yes') - self.assertNotError('poll delete 1') - self.assertError('poll vote 1 No') def testOpen(self): self.assertNotError('poll open 1') @@ -70,10 +68,6 @@ if sqlite is not None: self.assertNotError('poll close 1') self.assertError('poll close blah') - def testDelete(self): - self.assertNotError('poll delete 1') - self.assertError('poll delete blah') - def testVote(self): self.assertHelp('poll vote 1 blah') self.assertError('poll vote blah Yes')