More compliance fixes.
[bloggy.git] / index.cgi
blobbc7730b8387e09d836f8f1611f2c9d491488530c
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
24 import markdown2
26 def displaypost(date, title, body):
27 """Formats and prints a post"""
28 print '<div class="blogpost">'
29 print '<h1>%s</h1>' % title
30 print '<h3>%s</h3>' % date
31 print '%s' % markdown2.markdown(body)
32 print '</div>'
34 starttime = time.time()
36 form = cgi.FieldStorage()
38 print "Content-type: text/html; charset=UTF-8\n"
40 print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
41 print '<html>'
43 common.printheaders(config.TITLE)
45 conn = common.connect()
47 numposts = common.getnumposts(conn)
48 if numposts == 0:
49 print '<p>Nothing here yet. How about you <a href="post.cgi">post</a> something interesting?</p>'
50 else:
51 if form.has_key("id"):
52 if common.getnumposts(conn, form.getvalue("id")) > 0:
53 (date, title, text) = conn.execute("SELECT date, title, text FROM entries WHERE id = ?", (form.getvalue("id"),)).fetchone()
54 displaypost(date, title, text)
55 else:
56 print "<p>No such post.</p>"
57 else:
58 offset = 0
59 if form.has_key("offset"):
60 offset = int(form.getvalue("offset"))
61 for row in conn.execute("SELECT * FROM entries ORDER BY date DESC LIMIT ? OFFSET ?", (config.NUMPOSTS, offset)):
62 displaypost(row[1], '<a href="index.cgi?id=%s">%s</a>' % (row[0], row[2]), row[3])
63 print '<div id="navigation">'
64 if offset > 0:
65 newoffset = offset - config.NUMPOSTS
66 if newoffset < 0:
67 newoffset = 0
68 print '<a href="index.cgi?offset=%s">Prev</a>' % newoffset
69 if offset + config.NUMPOSTS < numposts:
70 newoffset = offset + config.NUMPOSTS
71 print '<a href="index.cgi?offset=%s">Next</a>' % newoffset
72 print '</div>'
74 print '</html>'
76 print '<!-- Page generated in %s seconds. -->' % (time.time() - starttime)
78 conn.close()