Skip to content

Commit

Permalink
Refactor the database stuff out of index.cgi.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Morgan committed Feb 17, 2009
1 parent ab8698f commit f035035
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
16 changes: 16 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import markdown2

# Database interactions.

def connect():
try:
conn = sqlite3.connect(config.DBPATH)
Expand All @@ -31,6 +33,18 @@ def connect():
sys.exit(1)
return conn

def getposts(conn, offset=0, numposts=config.NUMPOSTS):
posts = []
for row in conn.execute("SELECT * FROM entries ORDER BY date DESC LIMIT ? OFFSET ?",
(numposts, offset)):
posts.append(row)
return posts

def getpost(conn, postid):
row = conn.execute("SELECT * FROM entries WHERE id = ?",
(postid,)).fetchone()
return row

def getnumposts(conn, postid=None):
"""Enumerate the number of posts in the database. If an ID is
specified then enumerate the number of posts with that ID. The
Expand All @@ -44,6 +58,8 @@ def getnumposts(conn, postid=None):
numposts = conn.execute("SELECT count(id) FROM entries").fetchone()
return int(numposts[0])

# Output formatting.

def print_headers(title):
print '<head>'
print '<title>' + title + '</title>'
Expand Down
16 changes: 7 additions & 9 deletions index.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import cgi
#import cgitb; cgitb.enable()
import sqlite3

import common
import config

conn = common.connect()

form = cgi.FieldStorage()

print "Content-type: text/html; charset=UTF-8\n"
Expand All @@ -32,25 +32,23 @@ common.print_headers(config.TITLE)

common.header()

conn = common.connect()

numposts = common.getnumposts(conn)
if numposts == 0:
common.print_msg('Nothing here yet. How about you <a href="post.cgi">post</a> something interesting?')
else:
if form.has_key("id"):
if common.getnumposts(conn, form.getvalue("id")) > 0:
(title, text, date) = conn.execute("SELECT title, text, date FROM entries WHERE id = ?",
(form.getvalue("id"),)).fetchone()
post = common.getpost(conn, form.getvalue("id"))
if post:
(_, date, title, text) = post
common.print_post(title, text, date)
else:
common.print_msg("No such post.")
else:
offset = 0
if form.has_key("offset"):
offset = int(form.getvalue("offset"))
for (postid, title, text, date) in conn.execute("SELECT id, title, text, date FROM entries ORDER BY date DESC LIMIT ? OFFSET ?",
(config.NUMPOSTS, offset)):
posts = common.getposts(conn, offset)
for (postid, date, title, text) in posts:
title = '<a href="index.cgi?id=%s">%s</a>' % (postid, title)
common.print_post(title, text, date)

Expand Down
4 changes: 2 additions & 2 deletions post.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import config
edit_title = "Please enter a title."
edit_text = "Type something interesting."

conn = common.connect()

form = cgi.FieldStorage()

print 'Content-type: text/html; charset=UTF-8\n'
Expand All @@ -34,8 +36,6 @@ print '<html>'

common.print_headers(config.TITLE + " - Post")

conn = common.connect()

if form.has_key("delete"):
for postid in form.getlist("delete"):
conn.execute("DELETE FROM entries WHERE id = ?", (postid,))
Expand Down

0 comments on commit f035035

Please sign in to comment.