Convert the post editing radio button to a hyperlink.
[bloggy.git] / post.cgi
blob73b3e2e84af9d6864d377d85d1f9603f235702d1
1 #!/usr/bin/env python
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.
17 import cgi
18 #import cgitb; cgitb.enable()
19 import sqlite3
21 import common
22 import config
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">'
32 print '<html>'
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")))
49 else:
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>'
56 else:
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"):
60 print '<tr>'
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
66 print '</tr>'
67 print '</table>'
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
76 # one.
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)
85 print '<p>'
86 print '<input type="submit" name="preview" value="Preview">'
87 print '<input type="submit" name="submit" value="Submit">'
88 print '</p>'
89 print '</div>'
91 print '</form>'
93 print '</html>'
95 conn.commit()
96 conn.close()