More refactoring. This time post.cgi.
[bloggy.git] / post.cgi
blobe7614bd99d9f7706f46e7dd0579c57ae890a3773
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 time
21 import common
22 import config
24 edit_title = "Please enter a title."
25 edit_text = "Type something interesting."
27 conn = common.connect()
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.print_headers(config.TITLE + " - Post")
38 if form.has_key("delete"):
39 for postid in form.getlist("delete"):
40 common.deletepost(conn, 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) = common.getpost(conn, form.getvalue("edit"))
46 elif form.has_key("title") and form.has_key("body"):
47 if form.has_key("update"):
48 common.updatepost(conn, form.getvalue("update"),
49 form.getvalue("title"), form.getvalue("body"))
50 else:
51 common.addpost(conn, form.getvalue("title"), form.getvalue("body"))
53 print '<form action="post.cgi" method="post">'
55 if common.getnumposts(conn) == 0:
56 common.print_msg("Nothing here yet.")
57 else:
58 print '<table id="postlist">'
59 print '<tr><th>ID</th><th>Date</th><th>Title</th><th>Delete</th></tr>'
60 for (postid, date, title, _) in common.getposts(conn):
61 print '<tr>'
62 print '<td>%s</td>' % postid
63 print '<td>%s</td>' % time.strftime("%y/%m/%d %H:%M:%S", time.gmtime(date))
64 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(postid) + '">' + title + '</a>')
65 print '<td><input type="checkbox" name="delete" value="%s"></td>' % postid
66 print '<td><a href="post.cgi?edit=%s">Edit</a></td>' % postid
67 print '</tr>'
68 print '</table>'
70 if form.has_key("edit"):
71 print '<p><b>Editing post %s.</b></p>' % form.getvalue("edit")
72 print '<input type="hidden" name="update" value="%s">' % form.getvalue("edit")
74 if form.has_key("preview"):
75 # Perpetuate the update key so that when the post is submitted it
76 # correctly replaces an existing post rather than inserting a new
77 # one.
78 if form.has_key("update"):
79 print '<input type="hidden" name="update" value="%s">' % form.getvalue("update")
80 common.print_post(edit_title, edit_text)
82 print '<div id="editing">'
83 print '<input name="title" id="posttitle" type="text" value="%s">' % cgi.escape(edit_title, True)
84 print '<textarea name="body" id="postbody">%s</textarea>' % cgi.escape(edit_text, True)
86 print '<p>'
87 print '<input type="submit" name="preview" value="Preview" class="button">'
88 print '<input type="submit" name="submit" value="Submit" class="button">'
89 print '</p>'
90 print '</div>'
92 print '</form>'
94 print '</html>'
96 conn.commit()
97 conn.close()