|
|
@ -13,16 +13,23 @@ import os |
|
|
|
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.models import Book, BookSchema, Author, AuthorSchema, Stack, StackSchema, UserIns, Chat, ChatSchema, Instance, Potential |
|
|
|
from app.cover import get_cover |
|
|
|
<<<<<<< HEAD |
|
|
|
from app.getannot import get_annotations |
|
|
|
from urllib.parse import quote as urlquote |
|
|
|
======= |
|
|
|
from app.extractText import extract_text |
|
|
|
>>>>>>> master |
|
|
|
from os import environ |
|
|
|
from flask_socketio import SocketIO, emit |
|
|
|
from weasyprint import HTML |
|
|
|
import datetime |
|
|
|
import time |
|
|
|
import autocomplete |
|
|
|
from csv import DictWriter, DictReader |
|
|
|
import io |
|
|
|
from sqlalchemy.inspection import inspect |
|
|
|
#import autocomplete |
|
|
|
import sys |
|
|
|
from werkzeug.utils import secure_filename |
|
|
|
|
|
|
@ -57,8 +64,14 @@ def home(): |
|
|
|
# msg = Chat(message) |
|
|
|
# db.session.add(msg) |
|
|
|
# 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>') |
|
|
|
def hello(name): |
|
|
@ -71,6 +84,13 @@ def about(): |
|
|
|
|
|
|
|
@app.route('/uploads/<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'], |
|
|
|
filename) |
|
|
|
|
|
|
@ -101,7 +121,19 @@ def scape(): |
|
|
|
book.scapeY = data['y'] |
|
|
|
db.session.commit() |
|
|
|
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') |
|
|
@ -112,6 +144,7 @@ def show_books_grid(): |
|
|
|
@app.route('/books/<int:id>') |
|
|
|
def show_book_by_id(id): |
|
|
|
book = Book.query.get(id) |
|
|
|
all_instances = db.session.query(Instance).all() |
|
|
|
previousbook = Book.query.filter_by(id=id - 1).first() |
|
|
|
nextbook = Book.query.filter_by(id=id + 1).first() |
|
|
|
allbooks = db.session.query(Book).all() |
|
|
@ -132,7 +165,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, 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']) |
|
|
@ -176,6 +209,11 @@ def edit_book_by_id(id): |
|
|
|
a = Author(author_name=author_name) |
|
|
|
db.session.add(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 |
|
|
|
if user_form.file.data: |
|
|
@ -255,7 +293,13 @@ def add_book(): |
|
|
|
fullpath = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
|
|
|
name, file_extension = os.path.splitext(filename) |
|
|
|
file.save(fullpath) |
|
|
|
try: |
|
|
|
cover = get_cover(fullpath, name) |
|
|
|
except: |
|
|
|
cover = '' |
|
|
|
|
|
|
|
extract_text(fullpath, name) |
|
|
|
|
|
|
|
else: |
|
|
|
flash('allowed file formats: %s' % ALLOWED_EXTENSIONS) |
|
|
|
#if upload without file -> wishform, with potential PDF |
|
|
@ -285,6 +329,11 @@ def add_book(): |
|
|
|
a = Author(author_name=author_name) |
|
|
|
db.session.add(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() |
|
|
|
|
|
|
|
flash("%s added to the library" % (title)) |
|
|
@ -378,10 +427,40 @@ def edit_stack_by_id(id): |
|
|
|
stack.stack_name = stack_name |
|
|
|
stack.stack_description = stack_description |
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
return redirect(url_for('show_stack_by_id', id=id)) |
|
|
|
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']) |
|
|
|
def remove_from_stack(bookid, stackid): |
|
|
|
book = Book.query.get(bookid) |
|
|
@ -392,18 +471,21 @@ def remove_from_stack(bookid, stackid): |
|
|
|
db.session.commit() |
|
|
|
return render_template('show_book_detail.html', book=book) |
|
|
|
|
|
|
|
|
|
|
|
## search |
|
|
|
view = ['1'] |
|
|
|
|
|
|
|
@app.route('/books', methods= ['POST','GET']) |
|
|
|
def show_books(): |
|
|
|
<<<<<<< HEAD |
|
|
|
<<<<<<< HEAD |
|
|
|
autocomplete.load() #Train markov model once, for autocomplete in search |
|
|
|
books = db.session.query(Book).all() |
|
|
|
======= |
|
|
|
books = db.session.query(Book).order_by(Book.title) |
|
|
|
>>>>>>> stack_stuff |
|
|
|
======= |
|
|
|
books = db.session.query(Book).order_by(Book.title) |
|
|
|
>>>>>>> master |
|
|
|
search = SearchForm(request.form) |
|
|
|
view.append('1') |
|
|
|
viewby = '1' |
|
|
@ -514,6 +596,7 @@ def search_results(searchtype, query, viewby): |
|
|
|
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 |
|
|
|
<<<<<<< HEAD |
|
|
|
## Search - autocomplete |
|
|
|
autocomplete_suggestions = [] |
|
|
@ -541,6 +624,8 @@ def test1(): |
|
|
|
|
|
|
|
## STACKS! |
|
|
|
======= |
|
|
|
======= |
|
|
|
>>>>>>> master |
|
|
|
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) |
|
|
|
|
|
|
@ -562,11 +647,21 @@ def test1(): |
|
|
|
# autocomplete_suggestions.clear() |
|
|
|
# for suggestion, score in autocomplete_output: |
|
|
|
# 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) |
|
|
|
|
|
|
|
# return Response(json.dumps(autocomplete_suggestions), mimetype='application/json') |
|
|
|
>>>>>>> stack_stuff |
|
|
|
======= |
|
|
|
## STACKS! |
|
|
|
>>>>>>> master |
|
|
|
|
|
|
|
@app.route('/add_to_stack/<int:id>', methods=['GET', 'POST']) |
|
|
|
def add_to_stack(id): |
|
|
@ -583,23 +678,97 @@ def add_to_stack(id): |
|
|
|
db.session.commit() |
|
|
|
return render_template('show_stack_detail.html', stack=stack) |
|
|
|
|
|
|
|
from csv import DictWriter |
|
|
|
import io |
|
|
|
|
|
|
|
@app.route('/export/csv', methods=['GET']) |
|
|
|
def export_csv(): |
|
|
|
output = io.StringIO() |
|
|
|
fieldnames = ['title', 'authors'] |
|
|
|
csv = DictWriter(output,fieldnames) |
|
|
|
#fieldnames = ['title', 'authors', 'file', 'fileformat', 'category', 'year_published', 'description' ] |
|
|
|
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() |
|
|
|
# for i in range(10): |
|
|
|
# csv.writerow({'ID': i, 'fruit': "Tomato"}) |
|
|
|
for book in Book.query.order_by("title"): |
|
|
|
authors = ", ".join([x.author_name for x in book.authors]) |
|
|
|
csv.writerow({"title": book.title, "authors": authors}) |
|
|
|
resp = Response(output.getvalue(), mimetype="text/plain") |
|
|
|
# resp.headers["Content-Disposition"] = "attachment;filename=export.csv" |
|
|
|
row = {} |
|
|
|
for col in fieldnames: |
|
|
|
print(getattr(book, col)) |
|
|
|
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 |
|
|
|
|
|
|
|
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 |
|
|
|
### |
|
|
@ -673,10 +842,14 @@ def page_not_found(error): |
|
|
|
"""Custom 404 page.""" |
|
|
|
return render_template('404.html'), 404 |
|
|
|
|
|
|
|
|
|
|
|
### SOCKET for the chat |
|
|
|
|
|
|
|
@socketio.on('new_message') |
|
|
|
def new_message(message): |
|
|
|
# Send message to alls users |
|
|
|
print("new message") |
|
|
|
# Send message to all users |
|
|
|
# print("new message") |
|
|
|
# channel is always 1 now, but might be interesting for further development |
|
|
|
emit('channel-' + str(message['channel']), { |
|
|
|
'username': message['username'], |
|
|
|
'text': message['text'], |
|
|
@ -688,6 +861,7 @@ def new_message(message): |
|
|
|
my_new_chat = Chat( |
|
|
|
message=message['text'] |
|
|
|
) |
|
|
|
|
|
|
|
db.session.add(my_new_chat) |
|
|
|
try: |
|
|
|
db.session.commit() |
|
|
|