From f0350351d2580e70d5c214c39b360686a0499bfb Mon Sep 17 00:00:00 2001 From: Simon Morgan Date: Tue, 17 Feb 2009 14:04:03 +0000 Subject: [PATCH] Refactor the database stuff out of index.cgi. --- common.py | 16 ++++++++++++++++ index.cgi | 16 +++++++--------- post.cgi | 4 ++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/common.py b/common.py index 377108d..3906b68 100644 --- a/common.py +++ b/common.py @@ -20,6 +20,8 @@ import config import markdown2 +# Database interactions. + def connect(): try: conn = sqlite3.connect(config.DBPATH) @@ -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 @@ -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 '' print '' + title + '' diff --git a/index.cgi b/index.cgi index 0c98dac..e993f66 100755 --- a/index.cgi +++ b/index.cgi @@ -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" @@ -32,16 +32,14 @@ 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 post 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.") @@ -49,8 +47,8 @@ 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 = '%s' % (postid, title) common.print_post(title, text, date) diff --git a/post.cgi b/post.cgi index 291373c..55cb6b2 100755 --- a/post.cgi +++ b/post.cgi @@ -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' @@ -34,8 +36,6 @@ print '' 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,)) -- 2.11.4.GIT