3 # Copyright (c) 2008, 2009, Simon Morgan <sjm@spamcop.net>
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #import cgitb; cgitb.enable()
24 title
= "Please enter a title."
25 text
= "Type something interesting."
27 form
= cgi
.FieldStorage()
29 print 'Content-type: text/html; charset=UTF-8\n'
31 print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
34 common
.printheaders(config
.TITLE
+ " - Post")
36 conn
= common
.connect()
38 if form
.has_key("delete"):
39 for postid
in form
.getlist("delete"):
40 conn
.execute("DELETE FROM entries WHERE id = ?", (postid
,))
41 elif form
.has_key("edit"):
42 (title
, text
) = conn
.execute("SELECT title, text FROM entries WHERE id = ?", (form
.getvalue("edit"),)).fetchone()
43 elif form
.has_key("title") and form
.has_key("body"):
44 if form
.has_key("update"):
45 conn
.execute("UPDATE entries SET title = ?, text = ? WHERE id = ?", (form
.getvalue("title"), form
.getvalue("body"), form
.getvalue("update")))
47 conn
.execute("INSERT INTO entries VALUES (NULL, current_timestamp, ?, ?)", (form
.getvalue("title"), form
.getvalue("body")))
49 print '<form action="post.cgi" method="post">'
51 if common
.getnumposts(conn
) == 0:
52 print '<p>Nothing here yet.</p>'
54 print '<table id="postlist" border="1">'
55 print '<tr><th>ID</th><th>Date</th><th>Title</th><th>Delete</th><th>Update</th></tr>'
56 for row
in conn
.execute("SELECT id, date, title FROM entries ORDER BY date DESC"):
58 print '<td>%s</td>' % row
[0]
59 print '<td>%s</td>' % row
[1]
60 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(row
[0]) + '">' + row
[2] + '</a>')
61 print '<td><input type="checkbox" name="delete" value="%s"></td>' % row
[0]
62 print '<td><input type="radio" name="edit" value="%s"></td>' % row
[0]
66 if form
.has_key("edit"):
67 print '<p><b>Editing post %s.</b></p>' % form
.getvalue("edit")
68 print '<input type="hidden" name="update" value="%s">' % form
.getvalue("edit")
70 print '<p><input name="title" id="posttitle" type="text" value="%s"></p>' % cgi
.escape(title
, True)
71 print '<textarea name="body" id="postbody">%s</textarea>' % cgi
.escape(text
, True)
73 print '<p><button type="submit" name="submit">Submit</button></p>'