Time offsets support.
[bloggy.git] / index.cgi
blob0c98dac8c8cc2392df3987705f82869dd54495e4
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
21 import common
22 import config
24 form = cgi.FieldStorage()
26 print "Content-type: text/html; charset=UTF-8\n"
28 print '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
29 print '<html>'
31 common.print_headers(config.TITLE)
33 common.header()
35 conn = common.connect()
37 numposts = common.getnumposts(conn)
38 if numposts == 0:
39 common.print_msg('Nothing here yet. How about you <a href="post.cgi">post</a> something interesting?')
40 else:
41 if form.has_key("id"):
42 if common.getnumposts(conn, form.getvalue("id")) > 0:
43 (title, text, date) = conn.execute("SELECT title, text, date FROM entries WHERE id = ?",
44 (form.getvalue("id"),)).fetchone()
45 common.print_post(title, text, date)
46 else:
47 common.print_msg("No such post.")
48 else:
49 offset = 0
50 if form.has_key("offset"):
51 offset = int(form.getvalue("offset"))
52 for (postid, title, text, date) in conn.execute("SELECT id, title, text, date FROM entries ORDER BY date DESC LIMIT ? OFFSET ?",
53 (config.NUMPOSTS, offset)):
54 title = '<a href="index.cgi?id=%s">%s</a>' % (postid, title)
55 common.print_post(title, text, date)
57 # Only print the navigation bar if the number of posts exceeds
58 # the number to be displayed per page.
59 if numposts > config.NUMPOSTS:
60 print '<div id="navigation">'
61 if offset > 0:
62 newoffset = offset - config.NUMPOSTS
63 if newoffset < 0:
64 newoffset = 0
65 print '<a href="index.cgi?offset=%s">Prev</a>' % newoffset
66 if offset + config.NUMPOSTS < numposts:
67 newoffset = offset + config.NUMPOSTS
68 print '<a href="index.cgi?offset=%s">Next</a>' % newoffset
69 print '</div>'
71 common.footer()
73 print '</html>'
75 conn.close()