Source

py4u-opensource / pybook / pybook.py

Full commit
# -*- coding: utf-8 *-*
from flask import Blueprint, render_template,session,request,redirect,g,send_file,Markup,abort
from configure import configure
from pymodel import Pybook
import sqlite3
import base64
import time
from blinker import signal
#from flask.ext.cache import Cache
pybookapp = Blueprint('pybook', __name__, static_folder='static',template_folder='templates')
#connect db
DATABASE = 'pybook/pybookdb'
cache = None

def connect_db():
    """Returns a new connection to the database."""
    return sqlite3.connect(DATABASE,detect_types=sqlite3.PARSE_DECLTYPES)

@pybookapp.before_request
def before_request():
    """Make sure we are connected to the database each request."""
    #signal('init_cache').send(cache)
    g.time = time.time()
    g.pybookdb = connect_db()
    # enable foreign key support
    g.pybookdb.cursor().execute("PRAGMA foreign_keys = ON")
    g.pybookdb.row_factory = sqlite3.Row


@pybookapp.teardown_request
def teardown_request(exception):
    """Closes the database again at the end of the request."""
    if hasattr(g, 'db'):
        g.pybookdb.close()
'''
def get_books(limit = 15, offset = 0):
    key = 'books-'+str(limit)+'-'+str(offset)
    rv = cache.get(key)
    if rv is None:
        rv = Pybook.get_pybook(limit, offset)
        cache.set(key, rv, timeout=60 * 60*24)
    return rv
'''

def get_books(limit = 15, offset = 0):
    return Pybook.get_pybook(limit, offset)
@pybookapp.route('/')
def pybook():
    books = get_books(15, 0)
    return render_template('booklist.html',books = books, currentpage = 1, pages = [x for x in xrange(1,10)], generate_time = time.time() - g.time)

@pybookapp.route('/<int:page>')
def pybook_page(page):
    books = get_books(15, int(page) -1)
    if(len(books) == 0 ):
        abort(404)
    return render_template('booklist.html',books = books,currentpage = page, pages = [x for x in xrange(1,10)],generate_time = time.time() - g.time)

@pybookapp.route('/showbook')
def showbook():
    theid =request.args['id']
    book = Pybook.get_pybook_byid(theid)
    if(book is None):
        abort(404)
    signal('book_post_news').send(book)
    photo = base64.b64encode(book.cover_image.getvalue())
    book.cover_image = "data:image/png;base64," + photo
    book.comments = []
    signal('init_book_comments').send(book)
    return render_template('showbook.html',book = book, comments = book.comments)

@pybookapp.route('/download')
def download_pybook():
    id = request.args['id']
    content,title = Pybook.get_pybook_content_title_byid(id)
    from StringIO import StringIO
    strIO = StringIO(content)
    title = title.encode('utf-8', 'ignore')
    title = title +'.pdf'
    return send_file(strIO,
                     attachment_filename=title,
                     as_attachment=True)
@pybookapp.route('/report')
def report():
    id = request.form['error']
    return "None"
@pybookapp.route('/error_handle')
def error_handle():
    return "None"