fixed bug grid / list view

This commit is contained in:
nberting 2018-06-07 23:52:41 +02:00
parent 3296a2efcf
commit 7c82f2af95

View File

@ -348,45 +348,55 @@ def remove_from_stack(bookid, stackid):
## search ## search
view = ['1']
@app.route('/books', methods= ['POST','GET']) @app.route('/books', methods= ['POST','GET'])
def show_books(): def show_books():
books = db.session.query(Book).all() books = db.session.query(Book).order_by(Book.title)
search = SearchForm(request.form) search = SearchForm(request.form)
view.append('1')
viewby = '1'
if search.grid.data: if search.grid.data:
viewby = '2'
view.append('2')
return render_template ('show_books_grid.html', books=books, form=search) return render_template ('show_books_grid.html', books=books, form=search)
if search.listview.data: if search.listview.data:
viewby = '1'
view.append('1')
return render_template ('show_books.html', books=books, form=search) 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))) 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=query) search = SearchForm(request.form, search=query)
random_order=Book.query.order_by(func.random()).limit(14) random_order=Book.query.order_by(func.random()).limit(14)
results=Book.query.filter(Book.title.contains(query)) results=Book.query.filter(Book.title.contains(query)).order_by(Book.title)
viewby = view[-1]
if searchtype == 'Title': if searchtype == 'Title':
results=Book.query.filter(Book.title.contains(query)) 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)) results=Book.query.filter(Book.category.contains(query)).order_by(Book.title)
if searchtype== 'Author': if searchtype== 'Author':
results=db.session.query(Book).join(Book.authors).filter(Author.author_name.contains(query)) results=db.session.query(Book).join(Book.authors).filter(Author.author_name.contains(query)).order_by(Book.title)
if searchtype== 'Stack': if searchtype== 'Stack':
results=db.session.query(Book).join(Book.stacks).filter(Stack.stack_name.contains(query)) results=db.session.query(Book).join(Book.stacks).filter(Stack.stack_name.contains(query)).order_by(Book.title)
if searchtype== 'All': if searchtype== 'All':
# results=Book.query.whoosh_search(query) # results=Book.query.whoosh_search(query)
results=Book.query.filter(Book.title.contains(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.category.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.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_name.contains(query))).order_by(Book.title)
if results.count() == 0: if results.count() == 0:
upload_form = UploadForm(title= query, author='') upload_form = UploadForm(title= query, author='')
@ -397,40 +407,50 @@ def search_results(searchtype, query):
percentage = float(count / whole * 100) percentage = float(count / whole * 100)
if search.listview.data: if search.listview.data:
view.append('1')
return render_template('results.html', books=results, form=search, query=query, books_all=random_order, searchtype=search.select.data, count = count, whole = whole, percentage = percentage) return render_template('results.html', books=results, form=search, query=query, books_all=random_order, searchtype=search.select.data, count = count, whole = whole, percentage = percentage)
if search.grid.data: if search.grid.data:
view.append('2')
return render_template('results_grid.html', books=results, form=search, query=query, books_all=random_order, searchtype=search.select.data, count = count, whole = whole, percentage = percentage) return render_template('results_grid.html', books=results, form=search, query=query, books_all=random_order, searchtype=search.select.data, count = count, whole = whole, percentage = percentage)
if request.method == 'POST': if request.method == 'POST':
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, count = count, whole = whole, percentage = percentage) if viewby == '2':
return render_template('results_grid.html', form=search, books=results, books_all=random_order, searchtype=search.select.data, query=query, count = count, whole = whole, percentage = percentage)
else:
return render_template('results.html', form=search, books=results, books_all=random_order, searchtype=search.select.data, query=query, count = count, whole = whole, percentage = percentage)
## Search - autocomplete # ## Search - autocomplete
autocomplete_suggestions = [] # autocomplete_suggestions = []
@app.route('/autocomplete_suggestions', methods=['GET', 'POST']) # @app.route('/autocomplete_suggestions', methods=['GET', 'POST'])
def test1(): # def test1():
if request.method == 'POST': # if request.method == 'POST':
autocomplete.load() # autocomplete.load()
query = request.form['search'] # query = request.form['search']
query_tokenized = query.lower().split() # query_tokenized = query.lower().split()
print(query_tokenized) # print(query_tokenized)
word_1 = query_tokenized[-2] # word_1 = query_tokenized[-2]
word_2 = query_tokenized[-1] # word_2 = query_tokenized[-1]
#print(word_1) # #print(word_1)
autocomplete_output = autocomplete.predict(word_1 , word_2) # autocomplete_output = autocomplete.predict(word_1 , word_2)
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)
print(autocomplete_suggestions) # print(autocomplete_suggestions)
return Response(json.dumps(autocomplete_suggestions), mimetype='application/json') # return Response(json.dumps(autocomplete_suggestions), mimetype='application/json')
@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):