Prettier buttons.
[bloggy.git] / post.cgi
blob33885c4e856093eea86c25356b007640ddef2c63
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
20 import time
22 import common
23 import config
25 edit_title = "Please enter a title."
26 edit_text = "Type something interesting."
28 form = cgi.FieldStorage()
30 print 'Content-type: text/html; charset=UTF-8\n'
32 print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
33 print '<html>'
35 common.print_headers(config.TITLE + " - Post")
37 conn = common.connect()
39 if form.has_key("delete"):
40 for postid in form.getlist("delete"):
41 conn.execute("DELETE FROM entries WHERE id = ?", (postid,))
42 elif form.has_key("preview"):
43 edit_title = form.getvalue("title")
44 edit_text = form.getvalue("body")
45 elif form.has_key("edit"):
46 (edit_title, edit_text) = conn.execute("SELECT title, text FROM entries WHERE id = ?", (form.getvalue("edit"),)).fetchone()
47 elif form.has_key("title") and form.has_key("body"):
48 if form.has_key("update"):
49 conn.execute("UPDATE entries SET title = ?, text = ? WHERE id = ?", (form.getvalue("title"), form.getvalue("body"), form.getvalue("update")))
50 else:
51 curtime = int(time.time())
52 conn.execute("INSERT INTO entries VALUES (NULL, ?, ?, ?)",
53 (curtime, form.getvalue("title"), form.getvalue("body")))
55 print '<form action="post.cgi" method="post">'
57 if common.getnumposts(conn) == 0:
58 common.print_msg("Nothing here yet.")
59 else:
60 print '<table id="postlist">'
61 print '<tr><th>ID</th><th>Date</th><th>Title</th><th>Delete</th></tr>'
62 for (postid, date, title) in conn.execute("SELECT id, date, title FROM entries ORDER BY date DESC"):
63 print '<tr>'
64 print '<td>%s</td>' % postid
65 print '<td>%s</td>' % time.ctime(date)
66 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(postid) + '">' + title + '</a>')
67 print '<td><input type="checkbox" name="delete" value="%s"></td>' % postid
68 print '<td><a href="post.cgi?edit=%s">Edit</a></td>' % postid
69 print '</tr>'
70 print '</table>'
72 if form.has_key("edit"):
73 print '<p><b>Editing post %s.</b></p>' % form.getvalue("edit")
74 print '<input type="hidden" name="update" value="%s">' % form.getvalue("edit")
76 if form.has_key("preview"):
77 # Perpetuate the update key so that when the post is submitted it
78 # correctly replaces an existing post rather than inserting a new
79 # one.
80 if form.has_key("update"):
81 print '<input type="hidden" name="update" value="%s">' % form.getvalue("update")
82 common.print_post(edit_title, edit_text)
84 print '<div id="editing">'
85 print '<input name="title" id="posttitle" type="text" value="%s">' % cgi.escape(edit_title, True)
86 print '<textarea name="body" id="postbody">%s</textarea>' % cgi.escape(edit_text, True)
88 print '<p>'
89 print '<input type="submit" name="preview" value="Preview" class="button">'
90 print '<input type="submit" name="submit" value="Submit" class="button">'
91 print '</p>'
92 print '</div>'
94 print '</form>'
96 print '</html>'
98 conn.commit()
99 conn.close()