Browse Source

Updates and bugfixes.

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
2a09064767
1 changed files with 21 additions and 15 deletions
  1. +21
    -15
      earwigbot/commands/notes.py

+ 21
- 15
earwigbot/commands/notes.py View File

@@ -101,6 +101,7 @@ class Notes(Command):
entries = [] entries = []


if entries: if entries:
entries = [entry[0] for entry in entries]
self.reply(data, "Entries: {0}".format(", ".join(entries))) self.reply(data, "Entries: {0}".format(", ".join(entries)))
else: else:
self.reply(data, "No entries in the database.") self.reply(data, "No entries in the database.")
@@ -111,7 +112,7 @@ class Notes(Command):
INNER JOIN revisions ON entry_revision = rev_id INNER JOIN revisions ON entry_revision = rev_id
WHERE entry_slug = ?""" WHERE entry_slug = ?"""
try: try:
slug = data.args[1].lower().replace("_", "").replace("-", "")
slug = self.slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to read from.") self.reply(data, "Please specify an entry to read from.")
return return
@@ -137,7 +138,7 @@ class Notes(Command):
query3 = "INSERT INTO entries VALUES (?, ?, ?, ?)" query3 = "INSERT INTO entries VALUES (?, ?, ?, ?)"
query4 = "UPDATE entries SET entry_revision = ? WHERE entry_id = ?" query4 = "UPDATE entries SET entry_revision = ? WHERE entry_id = ?"
try: try:
slug = data.args[1].lower().replace("_", "").replace("-", "")
slug = self.slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to edit.") self.reply(data, "Please specify an entry to edit.")
return return
@@ -164,7 +165,7 @@ class Notes(Command):
return return
revid = self.get_next_revision(conn) revid = self.get_next_revision(conn)
userid = self.get_user(conn, data.host) userid = self.get_user(conn, data.host)
now = datetime.utcnow()
now = datetime.utcnow().strftime("%b %d, %Y %H:%M:%S")
conn.execute(query2, (revid, id_, userid, now, content)) conn.execute(query2, (revid, id_, userid, now, content))
if create: if create:
conn.execute(query3, (id_, slug, title, revid)) conn.execute(query3, (id_, slug, title, revid))
@@ -180,7 +181,7 @@ class Notes(Command):
INNER JOIN users ON rev_user = user_id INNER JOIN users ON rev_user = user_id
WHERE entry_slug = ?""" WHERE entry_slug = ?"""
try: try:
slug = data.args[1].lower().replace("_", "").replace("-", "")
slug = self.slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to get info on.") self.reply(data, "Please specify an entry to get info on.")
return return
@@ -194,11 +195,11 @@ class Notes(Command):
if info: if info:
title = info[0][0] title = info[0][0]
times = [datum[1] for datum in info] times = [datum[1] for datum in info]
earliest = min(times).strftime("%b %d, %Y %H:%M:%S")
earliest = min(times)
msg = "\x0302{0}\x0F: {1} edits since {2}" msg = "\x0302{0}\x0F: {1} edits since {2}"
msg = msg.format(title, len(info), earliest) msg = msg.format(title, len(info), earliest)
if len(times) > 1: if len(times) > 1:
latest = max(times).strftime("%b %d, %Y %H:%M:%S")
latest = max(times)
msg += "; last edit on {0}".format(latest) msg += "; last edit on {0}".format(latest)
names = [datum[2] for datum in info] names = [datum[2] for datum in info]
msg += "; authors: {0}.".format(", ".join(list(set(names)))) msg += "; authors: {0}.".format(", ".join(list(set(names))))
@@ -213,9 +214,10 @@ class Notes(Command):
INNER JOIN revisions ON entry_revision = rev_id INNER JOIN revisions ON entry_revision = rev_id
INNER JOIN users ON rev_user = user_id INNER JOIN users ON rev_user = user_id
WHERE entry_slug = ?""" WHERE entry_slug = ?"""
query2 = "UPDATE entries SET entry_title = ? WHERE entry_id = ?"
query2 = """UPDATE entries SET entry_slug = ?, entry_title = ?
WHERE entry_id = ?"""
try: try:
slug = data.args[1].lower().replace("_", "").replace("-", "")
slug = self.slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to rename.") self.reply(data, "Please specify an entry to rename.")
return return
@@ -240,7 +242,7 @@ class Notes(Command):
msg = "You must be an author or a bot admin to rename this entry." msg = "You must be an author or a bot admin to rename this entry."
self.reply(data, msg) self.reply(data, msg)
return return
conn.execute(query2, (newtitle, id_))
conn.execute(query2, (self.slugify(newtitle), newtitle, id_))


msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F." msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F."
self.reply(data, msg.format(data.args[1], newtitle)) self.reply(data, msg.format(data.args[1], newtitle))
@@ -254,7 +256,7 @@ class Notes(Command):
query2 = "DELETE FROM entries WHERE entry_id = ?" query2 = "DELETE FROM entries WHERE entry_id = ?"
query3 = "DELETE FROM revisions WHERE rev_entry = ?" query3 = "DELETE FROM revisions WHERE rev_entry = ?"
try: try:
slug = data.args[1].lower().replace("_", "").replace("-", "")
slug = self.slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to delete.") self.reply(data, "Please specify an entry to delete.")
return return
@@ -271,11 +273,15 @@ class Notes(Command):
msg = "You must be an author or a bot admin to delete this entry." msg = "You must be an author or a bot admin to delete this entry."
self.reply(data, msg) self.reply(data, msg)
return return
conn.execute(query2, (id_))
conn.execute(query3, (id_))
conn.execute(query2, (id_,))
conn.execute(query3, (id_,))


self.reply(data, "Entry \x0302{0}\x0F deleted.".format(data.args[1])) self.reply(data, "Entry \x0302{0}\x0F deleted.".format(data.args[1]))


def slugify(self, name):
"""Convert *name* into an identifier for storing in the database."""
return name.lower().replace("_", "").replace("-", "")

def create_db(self, conn): def create_db(self, conn):
"""Initialize the notes database with its necessary tables.""" """Initialize the notes database with its necessary tables."""
script = """ script = """
@@ -301,12 +307,12 @@ class Notes(Command):


def get_user(self, conn, host): def get_user(self, conn, host):
"""Get the user ID corresponding to a hostname, or make one.""" """Get the user ID corresponding to a hostname, or make one."""
query1 = "SELECT user_host FROM users WHERE user_id = ?"
query1 = "SELECT user_id FROM users WHERE user_host = ?"
query2 = "SELECT MAX(user_id) FROM users" query2 = "SELECT MAX(user_id) FROM users"
query3 = "INSERT INTO users VALUES (?, ?)" query3 = "INSERT INTO users VALUES (?, ?)"
user = conn.execute(query1).fetchone()[0]
user = conn.execute(query1, (host,)).fetchone()
if user: if user:
return user
return user[0]
last = conn.execute(query2).fetchone()[0] last = conn.execute(query2).fetchone()[0]
later = last + 1 if last else 1 later = last + 1 if last else 1
conn.execute(query3, (later, host)) conn.execute(query3, (later, host))


Loading…
Cancel
Save