What?
[bloggy.git] / post.cgi
blob4a2f1d43e443b055e8c0fea44796338b3847aeb6
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 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">'
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("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")))
46 else:
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>'
53 else:
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"):
57 print '<tr>'
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]
63 print '</tr>'
64 print '</table>'
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>'
75 print '</form>'
77 print '</html>'
79 conn.commit()
80 conn.close()