Convert another print call to print_msg.
[bloggy.git] / post.cgi
blob80c1f0ee810ffd4e8c344ed47f063238e8f12f76
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."
26 edit_date = int(time.time())
28 conn = common.connect()
30 form = cgi.FieldStorage()
32 print 'Content-type: text/html; charset=UTF-8\n'
34 print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
35 print '<html>'
37 common.print_headers(config.TITLE + " - Post")
39 if form.has_key("delete"):
40 common.deletepost(conn, form.getvalue("delete"))
42 if 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) = common.getpost(conn, form.getvalue("edit"))
47 elif form.has_key("title") and form.has_key("body"):
48 if form.has_key("update"):
49 common.updatepost(conn, form.getvalue("update"),
50 form.getvalue("title"), form.getvalue("body"))
51 else:
52 common.addpost(conn, form.getvalue("title"), form.getvalue("body"))
54 print '<form action="post.cgi" method="post">'
56 if common.getnumposts(conn) == 0:
57 common.print_msg("Nothing here yet.")
58 else:
59 print '<table id="postlist">'
60 print '<tr><th>ID</th><th>Date</th><th>Title</th></tr>'
61 for (postid, date, title, _) in common.getposts(conn):
62 print '<tr>'
63 print '<td>%s</td>' % postid
64 print '<td>%s</td>' % time.strftime("%y/%m/%d %H:%M:%S", time.gmtime(date))
65 print '<td>%s</td>' % ('<a href="index.cgi?id=' + str(postid) + '">' + title + '</a>')
66 print '<td><a href="post.cgi?delete=%s">Delete</a></td>' % postid
67 print '<td><a href="post.cgi?edit=%s">Edit</a></td>' % postid
68 print '</tr>'
69 print '</table>'
71 if form.has_key("edit"):
72 common.print_msg("Editing post %s." % 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 (edit_date, _, _) = common.getpost(conn,
81 form.getvalue("update"))
82 print '<input type="hidden" name="update" value="%s">' % form.getvalue("update")
83 common.print_post(edit_title, edit_text, edit_date)
85 print '<div id="editing">'
86 print '<input name="title" id="posttitle" type="text" value="%s">' % cgi.escape(edit_title, True)
87 print '<textarea name="body" id="postbody">%s</textarea>' % cgi.escape(edit_text, True)
89 print '<p>'
90 print '<input type="submit" name="preview" value="Preview" class="button">'
91 print '<input type="submit" name="submit" value="Submit" class="button">'
92 print '</p>'
93 print '</div>'
95 print '</form>'
97 print '</html>'
99 conn.commit()
100 conn.close()