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