Only print the navigation bar if the number of posts exceeds the number to be display...
[bloggy.git] / post.cgi
blob3ac5dfd01a2cff5058a93b0d61e9bb041cfadc3a
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 import markdown2
26 title = "Please enter a title."
27 text = "Type something interesting."
29 form = cgi.FieldStorage()
31 print 'Content-type: text/html; charset=UTF-8\n'
33 print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
34 print '<html>'
36 common.printheaders(config.TITLE + " - Post")
38 conn = common.connect()
40 if form.has_key("delete"):
41 for postid in form.getlist("delete"):
42 conn.execute("DELETE FROM entries WHERE id = ?", (postid,))
43 elif form.has_key("preview"):
44 title = form.getvalue("title")
45 text = form.getvalue("body")
46 elif form.has_key("edit"):
47 (title, text) = conn.execute("SELECT title, text FROM entries WHERE id = ?", (form.getvalue("edit"),)).fetchone()
48 elif form.has_key("title") and form.has_key("body"):
49 if form.has_key("update"):
50 conn.execute("UPDATE entries SET title = ?, text = ? WHERE id = ?", (form.getvalue("title"), form.getvalue("body"), form.getvalue("update")))
51 else:
52 conn.execute("INSERT INTO entries VALUES (NULL, current_timestamp, ?, ?)", (form.getvalue("title"), form.getvalue("body")))
54 print '<form action="post.cgi" method="post">'
56 if common.getnumposts(conn) == 0:
57 print '<p>Nothing here yet.</p>'
58 else:
59 print '<table id="postlist">'
60 print '<tr><th>ID</th><th>Date</th><th>Title</th><th>Delete</th><th>Update</th></tr>'
61 for row in conn.execute("SELECT id, date, title FROM entries ORDER BY date DESC"):
62 print '<tr>'
63 print '<td>%s</td>' % row[0]
64 print '<td>%s</td>' % row[1]
65 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(row[0]) + '">' + row[2] + '</a>')
66 print '<td><input type="checkbox" name="delete" value="%s"></td>' % row[0]
67 print '<td><input type="radio" name="edit" value="%s"></td>' % row[0]
68 print '</tr>'
69 print '</table>'
71 if form.has_key("edit"):
72 print '<p><b>Editing post %s.</b></p>' % form.getvalue("edit")
73 print '<input type="hidden" name="update" value="%s">' % form.getvalue("edit")
75 if form.has_key("preview"):
76 # Perpetuate the update key so that when the post is submitted it
77 # correctly replaces an existing post rather than inserting a new
78 # one.
79 if form.has_key("update"):
80 print '<input type="hidden" name="update" value="%s">' % form.getvalue("update")
81 print '<div id="preview">'
82 print markdown2.markdown(text)
83 print '</div>'
85 print '<div id="editingControls">'
86 print '<input name="title" id="posttitle" type="text" value="%s">' % cgi.escape(title, True)
87 print '<textarea name="body" id="postbody">%s</textarea>' % cgi.escape(text, True)
89 print '<p>'
90 print '<input type="submit" name="preview" value="Preview">'
91 print '<input type="submit" name="submit" value="Submit">'
92 print '</p>'
93 print '</div>'
95 print '</form>'
97 print '</html>'
99 conn.commit()
100 conn.close()