More CSS improvements.
[bloggy.git] / common.py
blob377108d7dc674be3e81a6f058888d0315d7e0d1a
1 # Copyright (c) 2008, 2009, Simon Morgan <sjm@spamcop.net>
3 # Permission to use, copy, modify, and/or distribute this software for any
4 # purpose with or without fee is hereby granted, provided that the above
5 # copyright notice and this permission notice appear in all copies.
7 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 import sqlite3
16 import sys
17 import time
19 import config
21 import markdown2
23 def connect():
24 try:
25 conn = sqlite3.connect(config.DBPATH)
26 conn.execute("CREATE TABLE IF NOT EXISTS entries (id INTEGER \
27 PRIMARY KEY, date INTEGER, title TEXT, text TEXT)")
28 except sqlite3.OperationalError:
29 print_error("Failed to connect to database. Check file and \
30 directory permissions.")
31 sys.exit(1)
32 return conn
34 def getnumposts(conn, postid=None):
35 """Enumerate the number of posts in the database. If an ID is
36 specified then enumerate the number of posts with that ID. The
37 result of the latter should be 0 or 1 as essentially this is a check
38 for whether the specified post exists.
39 """
40 if postid:
41 numposts = conn.execute("SELECT count(id) FROM entries WHERE id = ?",
42 (postid,)).fetchone()
43 else:
44 numposts = conn.execute("SELECT count(id) FROM entries").fetchone()
45 return int(numposts[0])
47 def print_headers(title):
48 print '<head>'
49 print '<title>' + title + '</title>'
50 print '<link href="default.css" rel="stylesheet" type="text/css">'
51 print '</head>'
53 def print_class(msg, class_):
54 print '<div class="%s">%s</div>' % (class_, msg)
56 def print_id(msg, id_):
57 print '<div id="%s">%s</div>' % (id_, msg)
59 def print_error(msg):
60 print_class(msg, "error")
62 def print_msg(msg):
63 print_class(msg, "message")
65 def print_post(title, body, date=None):
66 """Formats and prints a post"""
67 print '<div class="blogpost">'
68 print '<h1>%s</h1>' % title
69 if date:
70 print '<h3>%s</h3>' % time.ctime(date)
71 print markdown2.markdown(body)
72 print '</div>'
74 def header():
75 print_id(config.HEADER, "header")
77 def footer():
78 print_id(config.FOOTER, "footer")