def test_list_books(): books = bookdb.BookDB() titles = books.titles() assert len(titles) > 1 print titles for title in titles: assert 'title' in title assert 'id' in title
def ind_book_info(id): books = bookdb.BookDB() bookdic = books.title_info(id) bktitle = bookdic['title'] bkisbn = bookdic['isbn'] bkpublisher = bookdic['publisher'] bkauthor = bookdic['author'] infoaboutbook = '<P> Tittle: %s</P><P> ISBN: %s</P><P> Publisher: %s</P><P> Author: %s</P>' % (bktitle, bkisbn, bkpublisher, bkauthor) return infoaboutbook
def book_details(bookID): books = bookdb.BookDB() details = books.title_info(bookID) hold = [] for key, value in details.items(): hold.append(key + ": " + value) page = message_template2 % '<br>'.join(hold) return page
def book_info(): books = bookdb.BookDB() titles = books.titles() numbooks = len(titles) counter = 0 while counter < numbooks: id = titles[counter]['id'] print id info = books.title_info(id) print info counter += 1
def test_get_book_info(): books = bookdb.BookDB() titles = books.titles() id = titles[0]['id'] print id info = books.title_info(id) print info assert 'title' in info assert info['title'] == titles[0]['title'] assert 'publisher' in info assert 'isbn' in info assert 'author' in info
def mk_index(): books = bookdb.BookDB() titles = books.titles() numbooks = len(titles) counter = 0 bksindexpage = '' while counter < numbooks: id = titles[counter]['id'] title = titles[counter]['title'] #print id #print title bksindexpage = bksindexpage + '<p><a href="http://localhost:8080/getbook?%s"> %s </a></p>' % (id, title) counter += 1 return bksindexpage
def book_titles(): books = bookdb.BookDB() titles = books.titles() #assert len(titles) > 1 #print titles messages = "" for title in titles: x = title["title"] y = title["id"] link = '<a href= ' + '"/' + y + '"' + '>%s</a><br>' messages = (link % x) + messages #messages = ('<a href=%s>%s</a><br>' %x) + messages # insert at head page = message_template2 % messages return page # li
<head> <title>%s</title> </head> <body> %s </body> </html> """ # No need for message page # Flask converts view function return string to HTML page app = Flask(__name__) app.debug = True # development only - remove on production machines bookDb = bookdb.BookDB() # View functions generate HTTP responses including HTML pages and headers @app.route('/book_pages.py') #decorate another view function. def message_page(): # Flask Quickstart suggests request.form should work, but here it is empty # Flask converts return string to HTML page #return 'Message: %s' % request.args['message'] # args is a dictionary. 'message' is the key. bookTitle = [] bookTitleLines = '' for titleDict in bookDb.titles(): title = titleDict['title'] bookTitle.append(title) for b in bookTitle: bookTitleLines = bookTitleLines + "<a href='%s'>%s</a>" %(b, b) + "\n<br>" return bookTitleLines
return (301, uri + '/') else: return (404, uri) except IOError, e: return (404, e) def get_mime(uri): return mime_types.get(os.path.splitext(uri)[1], 'text/plain') def send_response(stream, content): stream.write(response[content[0]] % content[1:]) if __name__ == '__main__': B = bookdb.BookDB(database, linklist) args, nargs = sys.argv[1:], len(sys.argv) - 1 host, port = (args + defaults[-2 + nargs:])[0:2] server = server_socket(host, port) print 'starting %s on %s...' % (host, port) try: while True: stream = listen(server) send_response(stream, get_content(get_request(stream), B)) stream.close() except KeyboardInterrupt: print 'shutting down...' server.close()