#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cherrypy
import sqlite3
class Root(object):
def index(self, lang='fr', *args):
return """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Bank DB</title>
</head><body>
<h1>Bank DB</h1>
<h3>What do you want to do?</h3>
<a href="/branch">Branches</a>
<a href="/client">Clients</a>
</form>
</body></html>"""
index.exposed = True
def branch(self, branchno=None, accountno=None):
query = "SELECT * FROM branch"
if branchno:
query = "SELECT * FROM account"
query += " WHERE branchno=\"" + branchno+"\""
cherrypy.response.headers['Content-Type'] = 'text/html'
results = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Bank DB</title>
</head><body>"""
results += self.exec_query("bank.db", query, "/branch?branchno=")
results += """</body></html>"""
return results
branch.exposed = True
def client(self, clientno=None):
query = "SELECT * FROM client"
if clientno:
query = "SELECT accountno FROM clientAccount"
query += " WHERE clientno = " + clientno
cherrypy.response.headers['Content-Type'] = 'text/html'
results = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Bank DB</title>
</head><body>"""
results += self.exec_query("bank.db", query, "/account?accountno=")
results += """</body></html>"""
return results
client.exposed = True
def account (self, accountno=None):
query = "SELECT * FROM account"
target = "/account?accountno="
if accountno:
query = "SELECT clientno, lastname, firstname, account.*, trans.*"
query += " FROM client, clientAccount, account, trans"
query += " WHERE account.accountno = " + accountno + " AND trans.accountno = account.accountno AND"
query += " clientAccount.accountno = account.accountno AND client.clientno = clientAccount.clientno"
target= "/client?clientno="
cherrypy.response.headers['Content-Type'] = 'text/html'
results = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Bank DB</title>
</head><body>"""
results += self.exec_query("bank.db", query, target)
results += """</body></html>"""
return results
account.exposed = True
def exec_query(self, database, query, linkprefix=""):
results = """<table rules="all" width="75%" border="4" cellpadding="5"><tr>"""
try:
con = sqlite3.connect(database)
cur = con.cursor()
cur.execute(query)
desc = cur.description
for att in range(len(desc)):
results += "<th>" + desc[att][0] + "</th>"
results += "</tr>"
for tup in cur.fetchall():
results += '<tr><td><a href="' + linkprefix + unicode(tup[0]) + '">' + unicode(tup[0]) + '</a></td>'
for c in range(1, len(tup)):
results += "<td>" + unicode(tup[c]) + "</td>"
results += "</tr>"
results += "</table>"
except sqlite3.Error, e:
return unicode(e)
con.commit()
con.close()
return results
cherrypy.quickstart(Root(), '/', 'bank.config')
|