4 #import cgitb; cgitb.enable()
9 title
= "Please enter a title."
10 text
= "Type something interesting."
12 form
= cgi
.FieldStorage()
14 print 'Content-type: text/html; charset=UTF-8\n'
16 conn
= common
.connect()
18 if form
.has_key("delete"):
19 for postid
in form
.getlist("delete"):
20 conn
.execute("DELETE FROM entries WHERE id = ?", (postid
,))
21 elif form
.has_key("edit"):
22 (title
, text
) = conn
.execute("SELECT title, text FROM entries WHERE id = ?", (form
.getvalue("edit"),)).fetchone()
23 elif form
.has_key("title") and form
.has_key("text"):
24 if form
.has_key("update"):
25 conn
.execute("UPDATE entries SET title = ?, text = ? WHERE id = ?", (form
.getvalue("title"), form
.getvalue("text"), form
.getvalue("update")))
27 conn
.execute("INSERT INTO entries VALUES (NULL, current_timestamp, ?, ?)", (form
.getvalue("title"), form
.getvalue("text")))
29 print '<form action="post.cgi" method="post">'
31 if common
.getnumposts(conn
) == 0:
32 print '<p>Nothing here yet.</p>'
34 print '<table border="1">'
35 print '<tr><th>ID</th><th>Date</th><th>Title</th><th>Delete</th><th>Update</th></tr>'
36 for row
in conn
.execute("SELECT id, date, title FROM entries ORDER BY date DESC"):
38 print '<td>%s</td>' % row
[0]
39 print '<td>%s</td>' % row
[1]
40 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(row
[0]) + '">' + row
[2] + '</a>')
41 print '<td><input type="checkbox" name="delete" value="%s"></td>' % row
[0]
42 print '<td><input type="radio" name="edit" value="%s"></td>' % row
[0]
46 if form
.has_key("edit"):
47 print '<p><b>Editing post %s.</b></p>' % form
.getvalue("edit")
48 print '<input type="hidden" name="update" value="%s">' % form
.getvalue("edit")
50 print '<p><input name="title" type="text" value="%s"></p>' % cgi
.escape(title
, True)
51 print '<textarea name="text" id="text">%s</textarea>' % cgi
.escape(text
, True)
53 print '<p><button type="submit" name="submit">Submit</button></p>'