@ -11,13 +11,15 @@ from flask_weasyprint import HTML, render_pdf
import json
import json
import os
import os
from sqlalchemy . sql . expression import func , select
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 . forms import UploadForm , EditForm , SearchForm , ChatForm , StackForm , AddtoStackForm , EditStackForm
from app . models import Book , BookSchema , Author , AuthorSchema , Stack , StackSchema , UserIns , Chat , ChatSchema
from app . models import Book , BookSchema , Author , AuthorSchema , Stack , StackSchema , UserIns , Chat , ChatSchema , Potential
from app . cover import get_cover
from app . cover import get_cover
from app . getannot import get_annotations
from app . getannot import get_annotations
from urllib . parse import quote as urlquote
from urllib . parse import quote as urlquote
from os import environ
from os import environ
from flask_socketio import SocketIO , emit
from flask_socketio import SocketIO , emit
from weasyprint import HTML
import datetime
import datetime
import time
import time
import autocomplete
import autocomplete
@ -47,6 +49,7 @@ def allowed_file(filename):
def home ( ) :
def home ( ) :
chat_form = ChatForm ( )
chat_form = ChatForm ( )
chat_messages = db . session . query ( Chat ) . all ( )
chat_messages = db . session . query ( Chat ) . all ( )
username = ' librarian '
# if request.method == 'POST':
# if request.method == 'POST':
# if chat_form.validate_on_submit():
# if chat_form.validate_on_submit():
@ -55,7 +58,7 @@ def home():
# db.session.add(msg)
# db.session.add(msg)
# db.session.commit()
# db.session.commit()
return render_template ( ' home.html ' , domain = DOMAIN , chat = chat_messages , channel = 1 , username = " librarian " )
return render_template ( ' home.html ' , domain = DOMAIN , chat = chat_messages , channel = 1 , username = username )
@app.route ( ' /hello/<name> ' )
@app.route ( ' /hello/<name> ' )
def hello ( name ) :
def hello ( name ) :
@ -109,6 +112,15 @@ def show_books_grid():
@app.route ( ' /books/<int:id> ' )
@app.route ( ' /books/<int:id> ' )
def show_book_by_id ( id ) :
def show_book_by_id ( id ) :
book = Book . query . get ( 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 ( )
userin = UserIns . query . filter_by ( title = " lastViewed " ) . first ( )
if userin != None :
if userin != None :
userin . info = book . title
userin . info = book . title
@ -120,7 +132,7 @@ def show_book_by_id(id):
if not book :
if not book :
return render_template ( ' red_link.html ' , id = id )
return render_template ( ' red_link.html ' , id = id )
else :
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 ' ] )
@app.route ( ' /books/<int:id>/delete ' , methods = [ ' POST ' , ' GET ' ] )
@ -136,24 +148,23 @@ def remove_book_by_id(id):
@app.route ( ' /books/<int:id>/edit ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /books/<int:id>/edit ' , methods = [ ' POST ' , ' GET ' ] )
def edit_book_by_id ( id ) :
def edit_book_by_id ( id ) :
book_to_edit = Book . query . filter_by ( id = id ) . first ( )
book_to_edit = Book . query . filter_by ( id = id ) . first ( )
user_form = EditForm ( title = book_to_edit . title , author = book_to_edit . authors , category = book_to_edit . category , year_published = book_to_edit . year_published )
user_form = EditForm ( title = book_to_edit . title , author = book_to_edit . authors , category = book_to_edit . category , year_published = book_to_edit . year_published , message = book_to_edit . message )
if request . method == ' POST ' :
if request . method == ' POST ' :
if user_form . validate_on_submit ( ) :
if user_form . validate_on_submit ( ) :
# check if the post request has the file part
# on submit, check fields
title = user_form . title . data # You could also have used request.form['name']
title = user_form . title . data
input_authors = user_form . author . data # You could also have used request.form['email']
input_authors = user_form . author . data
category = user_form . category . data
category = user_form . category . data
year_published = user_form . year_published . data
year_published = user_form . year_published . data
message = user_form . message . data
if year_published == " " :
if year_published == " " :
year_published = None
year_published = None
# save user to database
#book = Book(title, author, filename, cover, file_extension)
book = Book . query . filter_by ( id = id ) . first ( )
book = Book . query . filter_by ( id = id ) . first ( )
book . title = title
book . title = title
book . category = category
book . category = category
book . year_published = year_published
book . year_published = year_published
book . message = message
#authors update
#authors update
book . authors . clear ( )
book . authors . clear ( )
@ -165,6 +176,26 @@ def edit_book_by_id(id):
a = Author ( author_name = author_name )
a = Author ( author_name = author_name )
db . session . add ( a )
db . session . add ( a )
book . authors . append ( a )
book . authors . append ( a )
# editing / uploading new file
if user_form . file . data :
file = request . files [ ' file ' ]
if file . filename == ' ' :
flash ( ' No selected file ' )
return redirect ( request . url )
if file and allowed_file ( file . filename ) :
allbooks = db . session . query ( Book ) . all ( )
id = book . id
filename = str ( id ) + " _ " + secure_filename ( file . filename )
fullpath = os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , filename )
name , file_extension = os . path . splitext ( filename )
file . save ( fullpath )
book . cover = get_cover ( fullpath , name )
book . file = filename
else :
flash ( ' allowed file formats: %s ' % ALLOWED_EXTENSIONS )
db . session . commit ( )
db . session . commit ( )
flash ( " %s updated " % ( title ) )
flash ( " %s updated " % ( title ) )
return redirect ( url_for ( ' show_book_by_id ' , id = id ) )
return redirect ( url_for ( ' show_book_by_id ' , id = id ) )
@ -175,6 +206,17 @@ def edit_book_by_id(id):
@app.route ( ' /add-book ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /add-book ' , methods = [ ' POST ' , ' GET ' ] )
def add_book ( ) :
def add_book ( ) :
upload_form = UploadForm ( )
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 request . method == ' POST ' :
if upload_form . validate_on_submit ( ) :
if upload_form . validate_on_submit ( ) :
@ -182,7 +224,14 @@ def add_book():
title = upload_form . title . data
title = upload_form . title . data
authors = upload_form . author . data
authors = upload_form . author . data
category = upload_form . category . data
category = upload_form . category . data
message = upload_form . message . data
year_published = upload_form . year_published . data
year_published = upload_form . year_published . data
sameness = upload_form . sameness . data
gender = upload_form . gender . data
diversity = upload_form . diversity . data
time = upload_form . time . data
who = upload_form . who . data
if year_published == " " :
if year_published == " " :
year_published = None
year_published = None
@ -199,27 +248,34 @@ def add_book():
flash ( ' No selected file ' )
flash ( ' No selected file ' )
return redirect ( request . url )
return redirect ( request . url )
if file and allowed_file ( file . filename ) :
if file and allowed_file ( file . filename ) :
filename = secure_filename ( file . filename )
allbooks = db . session . query ( Book ) . all ( )
allbooks = db . session . query ( Book ) . all ( )
id = len ( allbooks ) + 1
id = len ( allbooks ) + 1
new_filename = str ( id ) + " _ " + filename
filename = str ( id ) + " _ " + secure_filename ( file . filename )
fullpath = os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , new_filename )
name , file_extension = os . path . splitext ( new_filename )
fullpath = os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , filename )
name , file_extension = os . path . splitext ( filename )
file . save ( fullpath )
file . save ( fullpath )
cover = get_cover ( fullpath , name )
cover = get_cover ( fullpath , name )
else :
else :
flash ( ' allowed file formats: %s ' % ALLOWED_EXTENSIONS )
flash ( ' allowed file formats: %s ' % ALLOWED_EXTENSIONS )
#if upload without file -> wishform, with potential PDF
#if upload without file -> wishform, with potential PDF
if upload_form . wish . data :
if upload_form . wish . data :
#TO DO: make pdf generator
#pdf generator
#file = open('app/uploads/potential.pdf')
filename = ' potential.pdf '
#filename = 'potential.pdf'
file_extension = ' .pdf '
#file_extension = '.pdf'
filename = ' '
file_extension = ' '
cover = ' '
cover = ' '
ptitle = upload_form . title . data
book = Book ( title , filename , cover , file_extension , category , year_published )
pbook = Potential ( ptitle )
db . session . add ( pbook )
db . session . commit ( )
pbooks = Potential . query . all ( )
template = ' app/templates/potential_pdf.html '
html_string = render_template ( ' potential_pdf.html ' , pbooks = pbooks )
html = HTML ( string = html_string )
html . write_pdf ( target = ' app/uploads/potential.pdf ' ) ;
print ( ' potential_pdf ' )
book = Book ( title , filename , cover , file_extension , category , year_published , message , sameness , diversity , gender , who , time )
db . session . add ( book )
db . session . add ( book )
for author in authors :
for author in authors :
author_name = author . get ( " author_name " )
author_name = author . get ( " author_name " )
@ -235,7 +291,7 @@ def add_book():
return redirect ( url_for ( ' show_books ' ) )
return redirect ( url_for ( ' show_books ' ) )
flash_errors ( upload_form )
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
# Flash errors from the form if validation fails
@ -277,9 +333,10 @@ def add_stack():
if form . validate_on_submit ( ) :
if form . validate_on_submit ( ) :
stack_name = form . stack_name . data
stack_name = form . stack_name . data
stack_description = form . stack_description . data
stack_description = form . stack_description . data
stack = Stack ( stack_name , stack_description )
stack_author = form . stack_author . data
stack = Stack ( stack_name , stack_description , stack_author )
if form . stack_name . data :
if form . stack_name . data :
stack = Stack ( stack_name , stack_description )
stack = Stack ( stack_name , stack_description , stack_author )
db . session . add ( stack )
db . session . add ( stack )
stacks = db . session . query ( Stack ) . all ( )
stacks = db . session . query ( Stack ) . all ( )
return redirect ( url_for ( ' show_stacks ' ) )
return redirect ( url_for ( ' show_stacks ' ) )
@ -324,49 +381,140 @@ def edit_stack_by_id(id):
return redirect ( url_for ( ' show_stack_by_id ' , id = id ) )
return redirect ( url_for ( ' show_stack_by_id ' , id = id ) )
return render_template ( ' edit_stack_detail.html ' , stack = stack , form = form )
return render_template ( ' edit_stack_detail.html ' , stack = stack , form = form )
## search
@app.route ( ' /stacks/<int:stackid>/remove/<int:bookid> ' , methods = [ ' POST ' , ' GET ' ] )
def remove_from_stack ( bookid , stackid ) :
book = Book . query . get ( bookid )
stack = Stack . query . get ( stackid )
if book not in stack . books :
return render_template ( ' show_book_detail.html ' , book = book )
stack . books . remove ( book )
db . session . commit ( )
return render_template ( ' show_book_detail.html ' , book = book )
## search
## search
view = [ ' 1 ' ]
@app.route ( ' /books ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /books ' , methods = [ ' POST ' , ' GET ' ] )
def show_books ( ) :
def show_books ( ) :
<< << << < HEAD
autocomplete . load ( ) #Train markov model once, for autocomplete in search
autocomplete . load ( ) #Train markov model once, for autocomplete in search
books = db . session . query ( Book ) . all ( )
books = db . session . query ( Book ) . all ( )
== == == =
books = db . session . query ( Book ) . order_by ( Book . title )
>> >> >> > stack_stuff
search = SearchForm ( request . form )
search = SearchForm ( request . form )
view . append ( ' 1 ' )
viewby = ' 1 '
if search . grid . data :
viewby = ' 2 '
view . append ( ' 2 ' )
return render_template ( ' show_books_grid.html ' , books = books , form = search )
if search . listview . data :
viewby = ' 1 '
view . append ( ' 1 ' )
return render_template ( ' show_books.html ' , books = books , form = search )
if request . method == ' POST ' :
if request . method == ' POST ' :
return redirect ( ( url_for ( ' search_results ' , searchtype = search . select . data , query = search . search . data ) ) )
newmsg = ' searched for: ' + search . search . data
socketio . emit ( ' channel- ' + str ( 1 ) , {
' username ' : ' Search form ' ,
' text ' : search . search . data ,
' time ' : str ( datetime . datetime . utcnow ( ) . strftime ( " %d . % m. % Y % H: % M " ) ) } , broadcast = True )
# Save message
my_new_chat = Chat ( message = newmsg )
db . session . add ( my_new_chat )
try :
db . session . commit ( )
except :
db . session . rollback ( )
return redirect ( ( url_for ( ' search_results ' , searchtype = search . select . data , query = search . search . data , viewby = viewby ) ) )
return render_template ( ' show_books.html ' , books = books , form = search )
return render_template ( ' show_books.html ' , books = books , form = search )
@app.route ( ' /search/<searchtype>/<query>/ ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /search/<searchtype>/<viewby>/<query> ' , methods = [ ' POST ' , ' GET ' ] )
def search_results ( searchtype , query ) :
def search_results ( searchtype , query , viewby ) :
search = SearchForm ( request . form )
search = SearchForm ( request . form , search = query )
random_order = Book . query . order_by ( func . random ( ) ) . limit ( 10 )
results = Book . query . filter ( Book . title . contains ( query ) ) . order_by ( Book . title )
results = Book . query . filter ( Book . title . contains ( query ) ) . all ( )
allbooks = set ( Book . query . all ( ) )
viewby = view [ - 1 ]
if searchtype == ' Title ' :
if searchtype == ' Title ' :
results = Book . query . filter ( Book . title . contains ( query ) ) . all ( )
results = Book . query . filter ( Book . title . contains ( query ) ) . order_by( Book . title )
if searchtype == ' Category ' :
if searchtype == ' Category ' :
results = Book . query . filter ( Book . category . contains ( query ) ) . all( )
results = Book . query . filter ( Book . category . contains ( query ) ) . order_by( Book . title )
if searchtype == ' All ' :
if searchtype == ' Author ' :
results = Book . query . whoosh_search ( query ) . all ( )
results = db . session . query ( Book ) . join ( Book . authors ) . filter ( Author . author_name . contains ( query ) ) . order_by ( Book . title )
if searchtype == ' Stack ' :
results = db . session . query ( Book ) . join ( Book . stacks ) . filter ( Stack . stack_name . contains ( query ) ) . order_by ( Book . title )
<< << << < HEAD
# if searchtype== 'Annotation':
# if searchtype== 'Annotation':
# results=Book.query.filter(Book.category.contains(query)).all()
# results=Book.query.filter(Book.category.contains(query)).all()
if not results :
if not results :
== == == =
if searchtype == ' All ' :
# results=Book.query.whoosh_search(query)
results = Book . query . filter ( Book . title . contains ( query ) )
results = results . union ( Book . query . filter ( Book . category . contains ( query ) ) )
results = results . union ( Book . query . filter ( Book . year_published . contains ( query ) ) )
results = results . union ( db . session . query ( Book ) . join ( Book . authors ) . filter ( Author . author_name . contains ( query ) ) )
results = results . union ( db . session . query ( Book ) . join ( Book . stacks ) . filter ( Stack . stack_name . contains ( query ) ) )
results = results . union ( db . session . query ( Book ) . join ( Book . stacks ) . filter ( Stack . stack_description . contains ( query ) ) ) . order_by ( Book . title )
if results . count ( ) == 0 :
books = Book . query . filter ( Book . file . like ( ' potential.pdf ' ) )
>> >> >> > stack_stuff
upload_form = UploadForm ( title = query , author = ' ' )
upload_form = UploadForm ( title = query , author = ' ' )
return render_template ( ' red_link.html ' , form = upload_form , title = query )
return render_template ( ' red_link.html ' , form = upload_form , title = query , books = books )
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 = 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 = books_all , searchtype = search . select . data , count = count , whole = whole , percentage = percentage )
if request . method == ' POST ' :
if request . method == ' POST ' :
newmsg = ' searched for: ' + search . search . data
socketio . emit ( ' channel- ' + str ( 1 ) , {
' username ' : ' Search form ' ,
' text ' : search . search . data ,
' time ' : str ( datetime . datetime . utcnow ( ) . strftime ( " %d . % m. % Y % H: % M " ) ) } , broadcast = True )
# Save message
my_new_chat = Chat ( message = newmsg )
db . session . add ( my_new_chat )
try :
db . session . commit ( )
except :
db . session . rollback ( )
query = search . search . data
query = search . search . data
results = [ ]
results = [ ]
return redirect ( ( url_for ( ' search_results ' , searchtype = search . select . data , query = search . search . data ) ) )
if viewby == ' 1 ' :
print ( view [ - 1 ] )
return redirect ( ( url_for ( ' search_results ' , searchtype = search . select . data , query = search . search . data , viewby = viewby ) ) )
else :
return redirect ( ( url_for ( ' search_results ' , searchtype = search . select . data , query = search . search . data , viewby = viewby ) ) )
return render_template ( ' results.html ' , form = search , books = results , books_all = random_order , searchtype = search . select . data , query = query )
if viewby == ' 2 ' :
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 )
<< << << < HEAD
## Search - autocomplete
## Search - autocomplete
autocomplete_suggestions = [ ]
autocomplete_suggestions = [ ]
autocomplete . load ( ) #Train markov model once, for autocomplete in search
autocomplete . load ( ) #Train markov model once, for autocomplete in search
@ -392,6 +540,33 @@ def test1():
return Response ( json . dumps ( session [ ' autocomplete_suggestions ' ] ) , mimetype = ' application/json ' )
return Response ( json . dumps ( session [ ' autocomplete_suggestions ' ] ) , mimetype = ' application/json ' )
## STACKS!
## STACKS!
== == == =
else :
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
# autocomplete_suggestions = []
# @app.route('/autocomplete_suggestions', methods=['GET', 'POST'])
# def test1():
# if request.method == 'POST':
# autocomplete.load()
# query = request.form['search']
# query_tokenized = query.lower().split()
# print(query_tokenized)
# word_1 = query_tokenized[-2]
# word_2 = query_tokenized[-1]
# #print(word_1)
# autocomplete_output = autocomplete.predict(word_1 , word_2)
# autocomplete_suggestions.clear()
# for suggestion, score in autocomplete_output:
# autocomplete_suggestions.append(suggestion)
# print(autocomplete_suggestions)
# return Response(json.dumps(autocomplete_suggestions), mimetype='application/json')
>> >> >> > stack_stuff
@app.route ( ' /add_to_stack/<int:id> ' , methods = [ ' GET ' , ' POST ' ] )
@app.route ( ' /add_to_stack/<int:id> ' , methods = [ ' GET ' , ' POST ' ] )
def add_to_stack ( id ) :
def add_to_stack ( id ) :
@ -520,7 +695,6 @@ def new_message(message):
db . session . rollback ( )
db . session . rollback ( )
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :
# socketio.run(app)
# socketio.run(app)
app . run ( debug = True , host = " 0.0.0.0 " , port = " 8080 " )
app . run ( debug = True , host = " 0.0.0.0 " , port = " 8080 " )