@ -13,16 +13,23 @@ import os
from sqlalchemy . sql . expression import func , select
from sqlalchemy . sql . expression import func , select
from sqlalchemy . sql import except_
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 , Potential
from app . models import Book , BookSchema , Author , AuthorSchema , Stack , StackSchema , UserIns , Chat , ChatSchema , Instance, Potential
from app . cover import get_cover
from app . cover import get_cover
<< << << < HEAD
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 app . extractText import extract_text
>> >> >> > master
from os import environ
from os import environ
from flask_socketio import SocketIO , emit
from flask_socketio import SocketIO , emit
from weasyprint import HTML
from weasyprint import HTML
import datetime
import datetime
import time
import time
import autocomplete
from csv import DictWriter , DictReader
import io
from sqlalchemy . inspection import inspect
#import autocomplete
import sys
import sys
from werkzeug . utils import secure_filename
from werkzeug . utils import secure_filename
@ -57,8 +64,14 @@ def home():
# msg = Chat(message)
# msg = Chat(message)
# db.session.add(msg)
# db.session.add(msg)
# db.session.commit()
# db.session.commit()
#client = request.remote_addr
server = request . host
if request . environ . get ( ' HTTP_X_FORWARDED_FOR ' ) is None :
client = request . environ [ ' REMOTE_ADDR ' ]
else :
client = request . environ [ ' HTTP_X_FORWARDED_FOR ' ]
return render_template ( ' home.html ' , domain = DOMAIN , chat = chat_messages , channel = 1 , username = username )
return render_template ( ' home.html ' , domain = DOMAIN , chat = chat_messages , channel = 1 , username = username , client = client , server = server )
@app.route ( ' /hello/<name> ' )
@app.route ( ' /hello/<name> ' )
def hello ( name ) :
def hello ( name ) :
@ -71,6 +84,13 @@ def about():
@app.route ( ' /uploads/<filename> ' )
@app.route ( ' /uploads/<filename> ' )
def uploaded_file ( filename ) :
def uploaded_file ( filename ) :
book = Book . query . filter_by ( file = filename ) . first ( )
i = Instance ( request . host , " download " )
existing_ip = db . session . query ( Instance ) . filter_by ( ip = request . host ) . first ( )
if existing_ip :
i . name = existing_ip . name
book . instances . append ( i )
db . session . commit ( )
return send_from_directory ( app . config [ ' UPLOAD_FOLDER ' ] ,
return send_from_directory ( app . config [ ' UPLOAD_FOLDER ' ] ,
filename )
filename )
@ -101,7 +121,19 @@ def scape():
book . scapeY = data [ ' y ' ]
book . scapeY = data [ ' y ' ]
db . session . commit ( )
db . session . commit ( )
books = db . session . query ( Book ) . all ( ) # or you could have used User.query.all()
books = db . session . query ( Book ) . all ( ) # or you could have used User.query.all()
return render_template ( ' scape.html ' , books = books )
all_instances = db . session . query ( Instance ) . all ( )
instances = [ ]
for instance in all_instances :
exists = False
for existing_inst in instances :
if existing_inst . name == instance . name :
exists = True
break
else :
exists = False
if not exists :
instances . append ( instance )
return render_template ( ' scape.html ' , books = books , instances = instances )
@app.route ( ' /books_grid ' )
@app.route ( ' /books_grid ' )
@ -112,6 +144,7 @@ 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 )
all_instances = db . session . query ( Instance ) . all ( )
previousbook = Book . query . filter_by ( id = id - 1 ) . first ( )
previousbook = Book . query . filter_by ( id = id - 1 ) . first ( )
nextbook = Book . query . filter_by ( id = id + 1 ) . first ( )
nextbook = Book . query . filter_by ( id = id + 1 ) . first ( )
allbooks = db . session . query ( Book ) . all ( )
allbooks = db . session . query ( Book ) . all ( )
@ -132,7 +165,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 , previousbook = previousbook , nextbook = nextbook )
return render_template ( ' show_book_detail.html ' , book = book , previousbook = previousbook , nextbook = nextbook , all_instances = all_instances )
@app.route ( ' /books/<int:id>/delete ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /books/<int:id>/delete ' , methods = [ ' POST ' , ' GET ' ] )
@ -176,6 +209,11 @@ 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 )
i = Instance ( request . host , " edit " )
existing_ip = db . session . query ( Instance ) . filter_by ( ip = request . host ) . first ( )
if existing_ip :
i . name = existing_ip . name
book . instances . append ( i )
# editing / uploading new file
# editing / uploading new file
if user_form . file . data :
if user_form . file . data :
@ -255,7 +293,13 @@ def add_book():
fullpath = os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , filename )
fullpath = os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , filename )
name , file_extension = os . path . splitext ( filename )
name , file_extension = os . path . splitext ( filename )
file . save ( fullpath )
file . save ( fullpath )
cover = get_cover ( fullpath , name )
try :
cover = get_cover ( fullpath , name )
except :
cover = ' '
extract_text ( 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
@ -285,6 +329,11 @@ def add_book():
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 )
i = Instance ( request . host , " add " )
existing_ip = db . session . query ( Instance ) . filter_by ( ip = request . host ) . first ( )
if existing_ip :
i . name = existing_ip . name
book . instances . append ( i )
db . session . commit ( )
db . session . commit ( )
flash ( " %s added to the library " % ( title ) )
flash ( " %s added to the library " % ( title ) )
@ -378,10 +427,40 @@ def edit_stack_by_id(id):
stack . stack_name = stack_name
stack . stack_name = stack_name
stack . stack_description = stack_description
stack . stack_description = stack_description
db . session . commit ( )
db . session . commit ( )
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 )
@app.route ( ' /instances ' , methods = [ ' POST ' , ' GET ' ] )
def show_instances ( ) :
all_instances = db . session . query ( Instance ) . all ( )
instances = [ ]
for instance in all_instances :
exists = False
for existing_inst in instances :
if existing_inst . name == instance . name :
exists = True
break
else :
exists = False
if not exists :
instances . append ( instance )
if request . method == ' POST ' :
for item in request . form . items ( ) :
for i , itm in enumerate ( item ) :
if i == 0 :
oldname = itm
if i == 1 :
name = itm
all_instances = db . session . query ( Instance ) . filter_by ( name = oldname ) . all ( )
for instance in all_instances :
instance . name = name
print ( oldname )
print ( name )
db . session . commit ( )
return render_template ( ' show_instances.html ' , instances = instances )
@app.route ( ' /stacks/<int:stackid>/remove/<int:bookid> ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /stacks/<int:stackid>/remove/<int:bookid> ' , methods = [ ' POST ' , ' GET ' ] )
def remove_from_stack ( bookid , stackid ) :
def remove_from_stack ( bookid , stackid ) :
book = Book . query . get ( bookid )
book = Book . query . get ( bookid )
@ -392,18 +471,21 @@ def remove_from_stack(bookid, stackid):
db . session . commit ( )
db . session . commit ( )
return render_template ( ' show_book_detail.html ' , book = book )
return render_template ( ' show_book_detail.html ' , book = book )
## search
## search
view = [ ' 1 ' ]
view = [ ' 1 ' ]
@app.route ( ' /books ' , methods = [ ' POST ' , ' GET ' ] )
@app.route ( ' /books ' , methods = [ ' POST ' , ' GET ' ] )
def show_books ( ) :
def show_books ( ) :
<< << << < HEAD
<< << << < HEAD
<< << << < 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 )
books = db . session . query ( Book ) . order_by ( Book . title )
>> >> >> > stack_stuff
>> >> >> > stack_stuff
== == == =
books = db . session . query ( Book ) . order_by ( Book . title )
>> >> >> > master
search = SearchForm ( request . form )
search = SearchForm ( request . form )
view . append ( ' 1 ' )
view . append ( ' 1 ' )
viewby = ' 1 '
viewby = ' 1 '
@ -514,6 +596,7 @@ def search_results(searchtype, query, viewby):
if viewby == ' 2 ' :
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 )
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
<< << << < HEAD
<< << << < HEAD
## Search - autocomplete
## Search - autocomplete
autocomplete_suggestions = [ ]
autocomplete_suggestions = [ ]
@ -541,6 +624,8 @@ def test1():
## STACKS!
## STACKS!
== == == =
== == == =
== == == =
>> >> >> > master
else :
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 )
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 )
@ -562,11 +647,21 @@ def test1():
# autocomplete_suggestions.clear()
# autocomplete_suggestions.clear()
# for suggestion, score in autocomplete_output:
# for suggestion, score in autocomplete_output:
# autocomplete_suggestions.append(suggestion)
# autocomplete_suggestions.append(suggestion)
#
# session['autocomplete_suggestions'] = str(autocomplete_suggestions)
#
# print(session['autocomplete_suggestions'])
#
# return Response(json.dumps(session['autocomplete_suggestions']), mimetype='application/json')
<< << << < HEAD
# print(autocomplete_suggestions)
# print(autocomplete_suggestions)
# return Response(json.dumps(autocomplete_suggestions), mimetype='application/json')
# return Response(json.dumps(autocomplete_suggestions), mimetype='application/json')
>> >> >> > stack_stuff
>> >> >> > stack_stuff
== == == =
## STACKS!
>> >> >> > master
@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 ) :
@ -583,23 +678,97 @@ def add_to_stack(id):
db . session . commit ( )
db . session . commit ( )
return render_template ( ' show_stack_detail.html ' , stack = stack )
return render_template ( ' show_stack_detail.html ' , stack = stack )
from csv import DictWriter
import io
@app.route ( ' /export/csv ' , methods = [ ' GET ' ] )
@app.route ( ' /export/csv ' , methods = [ ' GET ' ] )
def export_csv ( ) :
def export_csv ( ) :
output = io . StringIO ( )
output = io . StringIO ( )
fieldnames = [ ' title ' , ' authors ' ]
#fieldnames = ['title', 'authors', 'file', 'fileformat', 'category', 'year_published', 'description' ]
csv = DictWriter ( output , fieldnames )
fields = Book . __mapper__ . columns
fieldnames = [ ]
for columns in fields :
fieldnames . append ( columns . name )
i = inspect ( Book )
referred_classes = [ r . mapper . class_ for r in i . relationships ]
referred_classes_tablenames = [ r . mapper . class_ . __tablename__ for r in i . relationships ]
print ( fieldnames + referred_classes_tablenames )
csv = DictWriter ( output , fieldnames + referred_classes_tablenames )
csv . writeheader ( )
csv . writeheader ( )
# for i in range(10):
# csv.writerow({'ID': i, 'fruit': "Tomato"})
for book in Book . query . order_by ( " title " ) :
for book in Book . query . order_by ( " title " ) :
authors = " , " . join ( [ x . author_name for x in book . authors ] )
row = { }
csv . writerow ( { " title " : book . title , " authors " : authors } )
for col in fieldnames :
resp = Response ( output . getvalue ( ) , mimetype = " text/plain " )
print ( getattr ( book , col ) )
# resp.headers["Content-Disposition"] = "attachment;filename=export.csv"
row [ col ] = getattr ( book , col )
for col in referred_classes :
subattr = [ ]
for subcol in getattr ( book , col . __tablename__ ) :
for metacol in subcol . __mapper__ . columns :
query = metacol . name
if query != " id " :
this = getattr ( subcol , query )
subattr . append ( this )
row [ col . __tablename__ ] = " | " . join ( subattr )
csv . writerow ( row )
#print(row)
resp = Response ( output . getvalue ( ) , mimetype = " text/csv " )
resp . headers [ " Content-Disposition " ] = " attachment;filename=export.csv "
return resp
return resp
import codecs
@app.route ( ' /import/csv ' , methods = [ ' POST ' , ' GET ' ] )
def import_csv ( ) :
if request . method == ' POST ' :
if ' file ' not in request . files :
flash ( ' No file part ' )
return redirect ( request . url )
else :
file = request . files [ ' file ' ]
for row in DictReader ( codecs . iterdecode ( file , ' utf-8 ' ) ) :
numberadded = 0 ;
book = Book . query . filter_by ( title = row [ ' title ' ] ) . first ( )
if book :
print ( " allreadyexists " )
else :
cover = ' '
if row [ ' file ' ] :
fullpath = os . path . join ( app . config [ ' UPLOAD_FOLDER ' ] , row [ ' file ' ] )
name , file_extension = os . path . splitext ( row [ ' file ' ] )
print ( ' get_cover ' , fullpath , name )
cover = get_cover ( fullpath , name )
if row [ ' year_published ' ] :
year_published = int ( row [ ' year_published ' ] )
else :
year_published = None ;
book = Book ( row [ ' title ' ] , row [ ' file ' ] , cover , row [ ' fileformat ' ] , row [ ' category ' ] , year_published , None , None , None , None , None , None )
book . scapeX = float ( row [ ' scapeX ' ] )
book . scapeY = float ( row [ ' scapeY ' ] )
db . session . add ( book )
numberadded = numberadded + 1
authors = row [ ' authors ' ] . split ( ' | ' )
authors = [ x . strip ( ) for x in authors ]
for author in authors :
if author :
a = db . session . query ( Author ) . filter_by ( author_name = author ) . first ( )
if a == None :
a = Author ( author_name = author )
db . session . add ( a )
book . authors . append ( a )
db . session . commit ( )
return render_template ( ' import_csv.html ' , numberadded = numberadded )
@app.route ( ' /empty_catalogue487352698237465 ' , methods = [ ' POST ' , ' GET ' ] )
def empty_catalogue ( ) :
meta = db . metadata
for table in reversed ( meta . sorted_tables ) :
if str ( table ) == " books " or str ( table ) == " authors " or str ( table ) == " books_authors " :
print ( ' Clear table %s ' % table )
db . session . execute ( table . delete ( ) )
db . create_all ( )
db . session . commit ( )
return " ALL CLEARED "
###
###
# The API
# The API
###
###
@ -673,10 +842,14 @@ def page_not_found(error):
""" Custom 404 page. """
""" Custom 404 page. """
return render_template ( ' 404.html ' ) , 404
return render_template ( ' 404.html ' ) , 404
### SOCKET for the chat
@socketio.on ( ' new_message ' )
@socketio.on ( ' new_message ' )
def new_message ( message ) :
def new_message ( message ) :
# Send message to alls users
# Send message to all users
print ( " new message " )
# print("new message")
# channel is always 1 now, but might be interesting for further development
emit ( ' channel- ' + str ( message [ ' channel ' ] ) , {
emit ( ' channel- ' + str ( message [ ' channel ' ] ) , {
' username ' : message [ ' username ' ] ,
' username ' : message [ ' username ' ] ,
' text ' : message [ ' text ' ] ,
' text ' : message [ ' text ' ] ,
@ -688,6 +861,7 @@ def new_message(message):
my_new_chat = Chat (
my_new_chat = Chat (
message = message [ ' text ' ]
message = message [ ' text ' ]
)
)
db . session . add ( my_new_chat )
db . session . add ( my_new_chat )
try :
try :
db . session . commit ( )
db . session . commit ( )