@ -6,10 +6,10 @@ This file creates your application.
"""
from app import app , db , socketio , DOMAIN
from flask import Flask , Response , render_template, request , redirect , url_for , flash , send_from_directory , jsonify , abort
from flask import Flask , Response , session, 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 . forms import UploadForm , EditForm , SearchForm , ChatForm , StackForm , AddtoStackForm , EditStackForm
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
@ -20,6 +20,8 @@ import time
from csv import DictWriter , DictReader
import io
from sqlalchemy . inspection import inspect
import autocomplete
import sys
import os
from werkzeug . utils import secure_filename
@ -307,13 +309,60 @@ def show_stacks():
stacks = db . session . query ( Stack ) . all ( )
return render_template ( ' show_stacks.html ' , stacks = stacks )
@app.route ( ' /stacks/add_stack ' , methods = [ ' POST ' , ' GET ' ] )
def add_stack ( ) :
form = StackForm ( )
stacks = db . session . query ( Stack ) . all ( )
if form . validate_on_submit ( ) :
stack_name = form . stack_name . data
stack_description = form . stack_description . data
stack = Stack ( stack_name , stack_description )
if form . stack_name . data :
stack = Stack ( stack_name , stack_description )
db . session . add ( stack )
stacks = db . session . query ( Stack ) . all ( )
return redirect ( url_for ( ' show_stacks ' ) )
flash ( " %s stack created " % ( stack_name ) )
return render_template ( ' add_stack.html ' , stacks = stacks , form = form )
@app.route ( ' /stacks/tab/<int:id> ' , methods = [ ' POST ' , ' GET ' ] )
def show_stack_in_tab ( id ) :
return show_stack_by_id ( id , is_tab = True )
@app.route ( ' /stacks/<int:id> ' , methods = [ ' POST ' , ' GET ' ] )
def show_stack_by_id ( id ) :
def show_stack_by_id ( id , is_tab = False ) :
stack = Stack . query . get ( id )
if not stack :
abort ( 404 )
else :
if is_tab == False :
return render_template ( ' show_stack_detail.html ' , stack = stack )
else :
return render_template ( ' show_stack_detail_tab.html ' , stack = stack )
@app.route ( ' /stacks/<int:id>/delete ' , methods = [ ' POST ' , ' GET ' ] )
def remove_stack_by_id ( id ) :
Stack . query . filter_by ( id = id ) . delete ( )
db . session . commit ( )
return redirect ( url_for ( ' show_stacks ' ) )
@app.route ( ' /stacks/<int:id>/edit ' , methods = [ ' POST ' , ' GET ' ] )
def edit_stack_by_id ( id ) :
stack = Stack . query . filter_by ( id = id ) . first ( )
form = EditStackForm ( edit_stack_name = stack . stack_name , edit_stack_description = stack . stack_description )
if request . method == ' POST ' :
if form . validate_on_submit ( ) :
stack_name = form . edit_stack_name . data
stack_description = form . edit_stack_description . data
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 ( ) :
@ -350,6 +399,8 @@ def show_instances():
@app.route ( ' /books ' , methods = [ ' POST ' , ' GET ' ] )
def show_books ( ) :
autocomplete . load ( ) #Train markov model once, for autocomplete in search
books = db . session . query ( Book ) . all ( )
search = SearchForm ( request . form )
if request . method == ' POST ' :
@ -383,6 +434,46 @@ def search_results(searchtype, query):
return render_template ( ' results.html ' , form = search , books = results , books_all = random_order , searchtype = search . select . data , query = query )
## Search - autocomplete
autocomplete_suggestions = [ ]
autocomplete . load ( ) #Train markov model once, for autocomplete in search
@app.route ( ' /autocomplete_suggestions ' , methods = [ ' GET ' , ' POST ' ] )
def test1 ( ) :
if request . method == ' POST ' :
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 )
session [ ' autocomplete_suggestions ' ] = str ( autocomplete_suggestions )
print ( session [ ' autocomplete_suggestions ' ] )
return Response ( json . dumps ( session [ ' autocomplete_suggestions ' ] ) , mimetype = ' application/json ' )
## STACKS!
@app.route ( ' /add_to_stack/<int:id> ' , methods = [ ' GET ' , ' POST ' ] )
def add_to_stack ( id ) :
stacks = db . session . query ( Stack ) . all ( )
add_form = AddtoStackForm ( request . form )
add_form . select_stack . choices = [ ( stack . id , stack . stack_name ) for stack in stacks ]
if request . method == ' GET ' :
book = Book . query . get ( id )
return render_template ( ' add_to_stacks.html ' , id = id , stacks = stacks , book = book , add_form = add_form )
else :
stack = Stack . query . get ( int ( add_form . select_stack . data ) )
book = Book . query . get ( id )
stack . books . append ( book )
db . session . commit ( )
return render_template ( ' show_stack_detail.html ' , stack = stack )
@app.route ( ' /export/csv ' , methods = [ ' GET ' ] )