Overhaul of Todo, somewhat. Made everyone's todos accessible by anyone, either

by id or by username.  Also, made it so todos are never deleted, just set to
inactive.
This commit is contained in:
Daniel DiPaolo 2003-10-28 17:40:41 +00:00
parent 2527f47cb6
commit f9d97c91f5
2 changed files with 75 additions and 36 deletions

View File

@ -82,7 +82,8 @@ class Todo(callbacks.Privmsg):
priority INTEGER, priority INTEGER,
added_at TIMESTAMP, added_at TIMESTAMP,
userid INTEGER, userid INTEGER,
task TEXT task TEXT,
active BOOLEAN
)""") )""")
self.db.commit() self.db.commit()
@ -92,50 +93,78 @@ class Todo(callbacks.Privmsg):
del self.db del self.db
def todo(self, irc, msg, args): def todo(self, irc, msg, args):
"""[<task id>] """[<username>|<task id>]
Retrieves a task for the given task id. If no task id is given, it Retrieves a task for the given task id. If no task id is given, it
will return a list of task ids that that user has added to their todo will return a list of task ids that that user has added to their todo
list. list.
""" """
try: arg = privmsgs.getArgs(args, needed=0, optional=1)
id = ircdb.users.getUserId(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNotRegistered)
return
taskid = privmsgs.getArgs(args, needed=0, optional=1) userid = None
if taskid: taskid = None
# Figure out what userid and taskid need to be set to (if anything)
if not arg:
pass
else:
try: try:
taskid = int(taskid) taskid = int(arg)
except ValueError, e: except ValueError:
irc.error(msg, '%r is an invalid task id' % taskid) try:
userid = ircdb.users.getUserId(arg)
except KeyError:
irc.error('%r is not a valid task id or username' % arg)
return
# Everything needs a cursor, might as well plop it outside
if not userid and not taskid:
try:
id = ircdb.users.getUserId(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNotRegistered)
return return
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""SELECT priority, added_at, task FROM todo
WHERE id = %s AND userid = %s""", taskid, id)
if cursor.rowcount == 0:
irc.reply(msg, 'None of your tasks match that id.')
else:
pri, added_at, task = cursor.fetchone()
if pri == 0:
priority = ""
else:
priority = ", priority: %s" % pri
added_time = time.strftime(conf.humanTimestampFormat,
time.localtime(int(added_at)))
s = "%s%s (Added at %s)" % (task, priority, added_time)
irc.reply(msg, s)
else:
cursor = self.db.cursor()
cursor.execute("""SELECT id, task FROM todo cursor.execute("""SELECT id, task FROM todo
WHERE userid = %s""", id) WHERE userid = %s AND active = 1""", id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, 'You have no tasks in your todo list.') irc.reply(msg, 'You have no tasks in your todo list.')
return return
s = ['#%s: %s' % (item[0], utils.ellipsisify(item[1], 50)) \ s = ['#%s: %s' % (item[0], utils.ellipsisify(item[1], 50)) \
for item in cursor.fetchall()] for item in cursor.fetchall()]
irc.reply(msg, utils.commaAndify(s)) irc.reply(msg, utils.commaAndify(s))
else:
cursor = self.db.cursor()
if userid:
cursor.execute("""SELECT id, task FROM todo
WHERE userid = %s AND active = 1""", userid)
if cursor.rowcount == 0:
irc.reply(msg, 'That user has no todos.')
return
s = ['#%s: %s' % (item[0], utils.ellipsisify(item[1], 50)) \
for item in cursor.fetchall()]
reply = "%s for %s: %s" % \
(utils.pluralize(len(s), 'Todo'), arg,
utils.commaAndify(s))
irc.reply(msg, reply)
return
else:
cursor.execute("""SELECT userid, priority, added_at, task
FROM todo WHERE id = %s
AND active = 1""", taskid)
if cursor.rowcount == 0:
irc.error(msg, '%r is not a valid task id' % taskid)
return
userid, pri, added_at, task = cursor.fetchone()
# Construct and return the reply
username = ircdb.users.getUser(userid).name
if pri == 0:
priority = ""
else:
priority = ", priority: %s" % pri
added_time = time.strftime(conf.humanTimestampFormat,
time.localtime(int(added_at)))
s = "Todo for %s: %s%s (Added at %s)" % \
(username, task, priority, added_time)
irc.reply(msg, s)
def add(self, irc, msg, args): def add(self, irc, msg, args):
"""[--priority=<num>] <text> """[--priority=<num>] <text>
@ -160,8 +189,9 @@ class Todo(callbacks.Privmsg):
return return
text = privmsgs.getArgs(rest, needed=1) text = privmsgs.getArgs(rest, needed=1)
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""INSERT INTO todo VALUES (NULL, %s, %s, %s, %s)""", cursor.execute("""INSERT INTO todo
priority, int(time.time()), id, text) VALUES (NULL, %s, %s, %s, %s, 1)""",
priority, int(time.time()), id, text)
self.db.commit() self.db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
@ -179,11 +209,12 @@ class Todo(callbacks.Privmsg):
taskid = privmsgs.getArgs(args, needed=1) taskid = privmsgs.getArgs(args, needed=1)
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""SELECT * FROM todo cursor.execute("""SELECT * FROM todo
WHERE id = %s AND userid = %s""", taskid, id) WHERE id = %s AND userid = %s
AND active = 1""", taskid, id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'None of your tasks match that id.') irc.error(msg, 'None of your tasks match that id.')
return return
cursor.execute("""DELETE FROM todo WHERE id = %s""", taskid) cursor.execute("""UPDATE todo SET active = 0 WHERE id = %s""", taskid)
self.db.commit() self.db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
@ -205,7 +236,7 @@ class Todo(callbacks.Privmsg):
(optlist, rest) = getopt.getopt(args, '', ['regexp=', 'exact=']) (optlist, rest) = getopt.getopt(args, '', ['regexp=', 'exact='])
if not optlist and not rest: if not optlist and not rest:
raise callbacks.ArgumentError raise callbacks.ArgumentError
criteria = ['userid = %s' % id] criteria = ['userid = %s' % id, 'active = 1']
formats = [] formats = []
predicateName = 'p' predicateName = 'p'
for (option, arg) in optlist: for (option, arg) in optlist:
@ -250,7 +281,7 @@ class Todo(callbacks.Privmsg):
(id, priority) = privmsgs.getArgs(args, needed=2) (id, priority) = privmsgs.getArgs(args, needed=2)
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""SELECT userid, priority FROM todo cursor.execute("""SELECT userid, priority FROM todo
WHERE id = %s""", id) WHERE id = %s AND active = 1""", id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'No note with id %d' % id) irc.error(msg, 'No note with id %d' % id)
return return

View File

@ -53,7 +53,15 @@ if sqlite is not None:
self.assertNotError('todo add wash my car') self.assertNotError('todo add wash my car')
self.assertRegexp('todo', '#1: wash my car') self.assertRegexp('todo', '#1: wash my car')
# Check that task # Check that task
self.assertNotError('todo 1') self.assertRegexp('todo 1',
'Todo for tester: wash my car \(Added .*?\)')
# Check that it lists all my tasks when given my name
self.assertRegexp('todo tester', 'Todo for tester: '
'#1: wash my car')
# Check pluralization
self.assertNotError('todo add moo')
self.assertRegexp('todo tester', 'Todos for tester: '
'#1: wash my car and #2: moo')
def testAddtodo(self): def testAddtodo(self):
self.assertNotError('todo add code a new plugin') self.assertNotError('todo add code a new plugin')