@ -9,6 +9,7 @@ from app import app, db, socketio, DOMAIN
from flask import Flask , Response , render_template , request , redirect , url_for , flash , send_from_directory , jsonify , abort
import json
from sqlalchemy . sql . expression import func , select
from sqlalchemy . sql import except_
from app . forms import UploadForm , EditForm , SearchForm , ChatForm , StackForm , AddtoStackForm , EditStackForm
from app . models import Book , BookSchema , Author , AuthorSchema , Stack , StackSchema , UserIns , Chat , ChatSchema , Potential
from app . cover import get_cover
@ -105,6 +106,15 @@ def show_books_grid():
@app.route ( ' /books/<int:id> ' )
def show_book_by_id ( id ) :
book = Book . query . get ( id )
previousbook = Book . query . filter_by ( id = id - 1 ) . first ( )
nextbook = Book . query . filter_by ( id = id + 1 ) . first ( )
allbooks = db . session . query ( Book ) . all ( )
edge = len ( allbooks )
if id == 1 :
previousbook = None
if id == edge :
nextbook = None
userin = UserIns . query . filter_by ( title = " lastViewed " ) . first ( )
if userin != None :
userin . info = book . title
@ -116,7 +126,7 @@ def show_book_by_id(id):
if not book :
return render_template ( ' red_link.html ' , id = id )
else :
return render_template ( ' show_book_detail.html ' , book = book )
return render_template ( ' show_book_detail.html ' , book = book , previousbook = previousbook , nextbook = nextbook )
@app.route ( ' /books/<int:id>/delete ' , methods = [ ' POST ' , ' GET ' ] )
@ -141,12 +151,14 @@ def edit_book_by_id(id):
input_authors = user_form . author . data
category = user_form . category . data
year_published = user_form . year_published . data
message = user_form . message . data
if year_published == " " :
year_published = None
book = Book . query . filter_by ( id = id ) . first ( )
book . title = title
book . category = category
book . year_published = year_published
book . message = message
#authors update
book . authors . clear ( )
@ -188,6 +200,17 @@ def edit_book_by_id(id):
@app.route ( ' /add-book ' , methods = [ ' POST ' , ' GET ' ] )
def add_book ( ) :
upload_form = UploadForm ( )
allbooks = db . session . query ( Book ) . all ( )
books_all = len ( allbooks )
allauthors = db . session . query ( Author ) . all ( )
authors_all = len ( allauthors )
stacks_all = [ s . stack_name for s in db . session . query ( Stack . stack_name ) ]
categories = [ r . category for r in db . session . query ( Book . category ) . distinct ( ) ]
allpotential = db . session . query ( Book ) . filter ( Book . file . contains ( ' potential.pdf ' ) ) . all ( )
books_potential = len ( allpotential )
earliest = db . session . query ( func . min ( Book . year_published ) ) . scalar ( )
latest = db . session . query ( func . max ( Book . year_published ) ) . scalar ( )
if request . method == ' POST ' :
if upload_form . validate_on_submit ( ) :
@ -256,7 +279,7 @@ def add_book():
return redirect ( url_for ( ' show_books ' ) )
flash_errors ( upload_form )
return render_template ( ' add_book.html ' , form = upload_form )
return render_template ( ' add_book.html ' , form = upload_form , books_all = books_all , authors_all = authors_all , categories = categories , stacks_all = stacks_all , books_potential = books_potential , earliest = earliest , latest = latest )
# Flash errors from the form if validation fails
@ -397,8 +420,8 @@ def show_books():
@app.route ( ' /search/<searchtype>/<viewby>/<query> ' , methods = [ ' POST ' , ' GET ' ] )
def search_results ( searchtype , query , viewby ) :
search = SearchForm ( request . form , search = query )
random_order = Book . query . all ( )
results = Book . query . filter ( Book . title . contains ( query ) ) . order_by ( Book . title )
allbooks = set ( Book . query . all ( ) )
viewby = view [ - 1 ]
if searchtype == ' Title ' :
@ -430,14 +453,16 @@ def search_results(searchtype, query, viewby):
count = results . count ( )
whole = Book . query . count ( )
percentage = float ( count / whole * 100 )
fbooks = set ( results )
books_all = allbooks - fbooks
if search . listview . data :
view . append ( ' 1 ' )
return render_template ( ' results.html ' , books = results , form = search , query = query , books_all = random_order , searchtype = search . select . data , count = count , whole = whole , percentage = percentage )
return render_template ( ' results.html ' , books = results , form = search , query = query , books_all = books_all , searchtype = search . select . data , count = count , whole = whole , percentage = percentage )
if search . grid . data :
view . append ( ' 2 ' )
return render_template ( ' results_grid.html ' , books = results , form = search , query = query , books_all = random_order , searchtype = search . select . data , count = count , whole = whole , percentage = percentage )
return render_template ( ' results_grid.html ' , books = results , form = search , query = query , books_all = books_all , searchtype = search . select . data , count = count , whole = whole , percentage = percentage )
if request . method == ' POST ' :
newmsg = ' searched for: ' + search . search . data
@ -462,10 +487,10 @@ def search_results(searchtype, query, viewby):
return redirect ( ( url_for ( ' search_results ' , searchtype = search . select . data , query = search . search . data , viewby = viewby ) ) )
if viewby == ' 2 ' :
return render_template ( ' results_grid.html ' , form = search , books = results , books_all = random_order , searchtype = search . select . data , query = query , count = count , whole = whole , percentage = percentage )
return render_template ( ' results_grid.html ' , form = search , books = results , books_all = books_all , searchtype = search . select . data , query = query , count = count , whole = whole , percentage = percentage )
else :
return render_template ( ' results.html ' , form = search , books = results , books_all = random_order , searchtype = search . select . data , query = query , count = count , whole = whole , percentage = percentage )
return render_template ( ' results.html ' , form = search , books = results , books_all = books_all , searchtype = search . select . data , query = query , count = count , whole = whole , percentage = percentage )
# ## Search - autocomplete