-
Notifications
You must be signed in to change notification settings - Fork 0
/
showbook.py
59 lines (48 loc) · 1.74 KB
/
showbook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from twisted.web import resource, server
from mako.template import Template
import cgi
from database import Database
class ShowBook(resource.Resource):
isLeaf = False
def getChild(self, name, request):
if name == '':
return self
return BookPage(name)
def render_GET(self, request):
username = request.getCookie('user')
return self.write_form(username)
def write_form(self, username):
db = Database()
fic = db.listBooks(username, 'fiction')
nonfic = db.listBooks(username, 'nonfiction')
myTemplate = Template(filename='templates/listbooks.html')
return str(myTemplate.render(fic=fic, nonfic=nonfic))
class BookPage(resource.Resource):
def __init__(self, idnum):
resource.Resource.__init__(self)
self.idnum = idnum
def render_GET(self, request):
username = request.getCookie('user')
bookPage = self.getBookInfo(username)
if bookPage:
return bookPage
request.redirect('/')
request.finish()
return server.NOT_DONE_YET
def getBookInfo(self, username):
if self.idnum.isdigit():
db = Database()
bookinfo = db.getBookInfo(self.idnum, username)
if bookinfo:
title = bookinfo[2]
booktype = bookinfo[3]
rating = bookinfo[4]
author = bookinfo[5]
numpages = bookinfo[6]
yearpub = bookinfo[7]
yearread = bookinfo[8]
return self.write_form(title, booktype, rating, author, numpages, yearpub, yearread)
return None
def write_form(self, title='', booktype='', rating='', author='', numpages='', yearpub='', yearread='', error=''):
mytemplate = Template(filename='templates/bookinfo.html')
return str(mytemplate.render(idnum=self.idnum, title=title, booktype=booktype, rating=rating, author=author, numpages=numpages, yearpub=yearpub, yearread=yearread, error=error))