|
|
@ -6,16 +6,20 @@ This file creates your application. |
|
|
|
""" |
|
|
|
|
|
|
|
from app import app, db, socketio, DOMAIN |
|
|
|
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify, abort |
|
|
|
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 app.forms import UploadForm, EditForm, SearchForm, ChatForm |
|
|
|
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, Instance |
|
|
|
from app.cover import get_cover |
|
|
|
from app.extractText import extract_text |
|
|
|
from os import environ |
|
|
|
from flask_socketio import SocketIO, emit |
|
|
|
import datetime |
|
|
|
import time |
|
|
|
from csv import DictWriter, DictReader |
|
|
|
import io |
|
|
|
from sqlalchemy.inspection import inspect |
|
|
|
|
|
|
|
import os |
|
|
|
from werkzeug.utils import secure_filename |
|
|
@ -50,8 +54,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="librarian") |
|
|
|
return render_template('home.html',domain=DOMAIN,chat=chat_messages, channel = 1, username="librarian", client=client, server=server) |
|
|
|
|
|
|
|
@app.route('/hello/<name>') |
|
|
|
def hello(name): |
|
|
@ -64,6 +74,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) |
|
|
|
|
|
|
@ -90,7 +107,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') |
|
|
@ -101,6 +130,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() |
|
|
|
userin = UserIns.query.filter_by(title="lastViewed").first() |
|
|
|
if userin != None: |
|
|
|
userin.info = book.title |
|
|
@ -112,7 +142,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, all_instances=all_instances) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/books/<int:id>/delete', methods=['POST', 'GET']) |
|
|
@ -157,6 +187,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) |
|
|
|
db.session.commit() |
|
|
|
flash("%s updated" % (title)) |
|
|
|
return redirect(url_for('show_book_by_id', id=id)) |
|
|
@ -198,7 +233,13 @@ def add_book(): |
|
|
|
fullpath = os.path.join(app.config['UPLOAD_FOLDER'], new_filename) |
|
|
|
name, file_extension = os.path.splitext(new_filename) |
|
|
|
file.save(fullpath) |
|
|
|
cover = get_cover(fullpath, name) |
|
|
|
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 |
|
|
@ -221,6 +262,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)) |
|
|
@ -269,7 +315,36 @@ def show_stack_by_id(id): |
|
|
|
else: |
|
|
|
return render_template('show_stack_detail.html', stack=stack) |
|
|
|
|
|
|
|
## search |
|
|
|
@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) |
|
|
|
|
|
|
|
## search |
|
|
|
|
|
|
@ -309,6 +384,96 @@ def search_results(searchtype, query): |
|
|
|
return render_template('results.html', form=search, books=results, books_all=random_order, searchtype=search.select.data, query=query) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/export/csv', methods=['GET']) |
|
|
|
def export_csv(): |
|
|
|
output = io.StringIO() |
|
|
|
#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 book in Book.query.order_by("title"): |
|
|
|
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) |
|
|
|
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_catalogue', 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 |
|
|
|
### |
|
|
@ -384,7 +549,7 @@ def new_message(message): |
|
|
|
my_new_chat = Chat( |
|
|
|
message=message['text'] |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
db.session.add(my_new_chat) |
|
|
|
try: |
|
|
|
db.session.commit() |
|
|
|