Document the configuration directives in config.py.
[bloggy.git] / index.cgi
blob202e68d90d94bc0b934b51412d7246e447250ebf
1 #!/usr/bin/env python
3 import cgi
4 #import cgitb; cgitb.enable()
5 import sqlite3
6 import time
8 import common
9 import config
10 import textile
12 def displaypost(date, title, body):
13 """Formats and prints a post"""
14 print '<h1>%s</h1>' % title
15 print '<h3>%s</h1>' % date
16 print '%s' % textile.textile(body)
18 starttime = time.time()
20 form = cgi.FieldStorage()
22 print "Content-type: text/html; charset=UTF-8\n"
24 conn = common.connect()
26 numposts = common.getnumposts(conn)
27 if numposts == 0:
28 print '<p>Nothing here yet. How about you <a href="post.cgi">post</a> something interesting?</p>'
29 else:
30 if form.has_key("id"):
31 if common.getnumposts(conn, form.getvalue("id")) > 0:
32 (date, title, text) = conn.execute("SELECT date, title, text FROM entries WHERE id = ?", (form.getvalue("id"),)).fetchone()
33 displaypost(date, title, text)
34 else:
35 print "<p>No such post.</p>"
36 else:
37 offset = 0
38 if form.has_key("offset"):
39 offset = int(form.getvalue("offset"))
40 for row in conn.execute("SELECT * FROM entries ORDER BY date DESC LIMIT ? OFFSET ?", (config.NUMPOSTS, offset)):
41 displaypost(row[1], '<a href="index.cgi?id=%s">%s</a>' % (row[0], row[2]), row[3])
42 print '<p align="center">'
43 if offset > 0:
44 newoffset = offset - config.NUMPOSTS
45 if newoffset < 0:
46 newoffset = 0
47 print '<a href="index.cgi?offset=%s">Prev</a>' % newoffset
48 if offset + config.NUMPOSTS < numposts:
49 newoffset = offset + config.NUMPOSTS
50 print '<a href="index.cgi?offset=%s">Next</a>' % newoffset
51 print '</p>'
53 print '<hr />'
54 print '<p>Page generated in %s seconds.</p>' % (time.time() - starttime)
56 conn.close()