Convert the post editing radio button to a hyperlink.
[bloggy.git] / common.py
blobc88567f7a8f414e502ae3a68f186b93dbf057fe0
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
18 import config
20 import markdown2
22 def connect():
23 try:
24 conn = sqlite3.connect(config.DBPATH)
25 conn.execute("CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, date , title TEXT, text TEXT)")
26 except sqlite3.OperationalError:
27 print 'Failed to connect to database. Check file and directory permissions.'
28 sys.exit(1)
29 return conn
31 def getnumposts(conn, id=None):
32 """Enumerate the number of posts in the database. if an ID is specified
33 then enumerate the number of posts with that ID. The latter should be 0 or
34 1 so essentially this is a check for whether the specified post exists.
35 """
36 if id:
37 numposts = conn.execute("SELECT count(id) FROM entries WHERE id = ?", (id,)).fetchone()
38 else:
39 numposts = conn.execute("SELECT count(id) FROM entries").fetchone()
40 return int(numposts[0])
42 def printheaders(title):
43 print '<head>'
44 print '<title>' + title + '</title>'
45 print '<link href="default.css" rel="stylesheet" type="text/css">'
46 print '</head>'
48 def displaypost(title, body, date=None):
49 """Formats and prints a post"""
50 print '<div class="blogpost">'
51 print '<h1>%s</h1>' % title
52 if date:
53 print '<h3>%s</h3>' % date
54 print markdown2.markdown(body)
55 print '</div>'