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 edit_title
= "Please enter a title."
25 edit_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("preview"):
42 edit_title
= form
.getvalue("title")
43 edit_text
= form
.getvalue("body")
44 elif form
.has_key("edit"):
45 (edit_title
, edit_text
) = conn
.execute("SELECT title, text FROM entries WHERE id = ?", (form
.getvalue("edit"),)).fetchone()
46 elif form
.has_key("title") and form
.has_key("body"):
47 if form
.has_key("update"):
48 conn
.execute("UPDATE entries SET title = ?, text = ? WHERE id = ?", (form
.getvalue("title"), form
.getvalue("body"), form
.getvalue("update")))
50 conn
.execute("INSERT INTO entries VALUES (NULL, current_timestamp, ?, ?)", (form
.getvalue("title"), form
.getvalue("body")))
52 print '<form action="post.cgi" method="post">'
54 if common
.getnumposts(conn
) == 0:
55 print '<p>Nothing here yet.</p>'
57 print '<table id="postlist">'
58 print '<tr><th>ID</th><th>Date</th><th>Title</th><th>Delete</th></tr>'
59 for (postid
, date
, title
) in conn
.execute("SELECT id, date, title FROM entries ORDER BY date DESC"):
61 print '<td>%s</td>' % postid
62 print '<td>%s</td>' % date
63 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(postid
) + '">' + title
+ '</a>')
64 print '<td><input type="checkbox" name="delete" value="%s"></td>' % postid
65 print '<td><a href="post.cgi?edit=%s">Edit</a></td>' % postid
69 if form
.has_key("edit"):
70 print '<p><b>Editing post %s.</b></p>' % form
.getvalue("edit")
71 print '<input type="hidden" name="update" value="%s">' % form
.getvalue("edit")
73 if form
.has_key("preview"):
74 # Perpetuate the update key so that when the post is submitted it
75 # correctly replaces an existing post rather than inserting a new
77 if form
.has_key("update"):
78 print '<input type="hidden" name="update" value="%s">' % form
.getvalue("update")
79 common
.displaypost(edit_title
, edit_text
)
81 print '<div id="editingControls">'
82 print '<input name="title" id="posttitle" type="text" value="%s">' % cgi
.escape(edit_title
, True)
83 print '<textarea name="body" id="postbody">%s</textarea>' % cgi
.escape(edit_text
, True)
86 print '<input type="submit" name="preview" value="Preview">'
87 print '<input type="submit" name="submit" value="Submit">'