From 7c82f2af95614b36c7931efb7c12621a91e9c313 Mon Sep 17 00:00:00 2001 From: nberting Date: Thu, 7 Jun 2018 23:52:41 +0200 Subject: [PATCH] fixed bug grid / list view --- app/views.py | 92 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/app/views.py b/app/views.py index 2fe1bc3..2290a64 100755 --- a/app/views.py +++ b/app/views.py @@ -348,45 +348,55 @@ def remove_from_stack(bookid, stackid): ## search +view = ['1'] @app.route('/books', methods= ['POST','GET']) def show_books(): - books = db.session.query(Book).all() + books = db.session.query(Book).order_by(Book.title) search = SearchForm(request.form) + view.append('1') + viewby = '1' if search.grid.data: + viewby = '2' + view.append('2') return render_template ('show_books_grid.html', books=books, form=search) + if search.listview.data: + viewby = '1' + view.append('1') return render_template ('show_books.html', books=books, form=search) + 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) -@app.route('/search///', methods=['POST', 'GET']) -def search_results(searchtype, query): +@app.route('/search///', methods=['POST', 'GET']) +def search_results(searchtype, query, viewby): search = SearchForm(request.form, search=query) 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': - results=Book.query.filter(Book.title.contains(query)) + results=Book.query.filter(Book.title.contains(query)).order_by(Book.title) 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': - 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': - 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': # results=Book.query.whoosh_search(query) results=Book.query.filter(Book.title.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.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: upload_form = UploadForm(title= query, author='') @@ -394,43 +404,53 @@ def search_results(searchtype, query): count = results.count() whole = Book.query.count() - percentage = float(count / whole * 100) + percentage = float(count / whole * 100) 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) 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) if request.method == 'POST': query = search.search.data results = [] - return redirect((url_for('search_results', searchtype=search.select.data, query=search.search.data))) - - 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 -autocomplete_suggestions = [] + 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))) + + 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) -@app.route('/autocomplete_suggestions', methods=['GET', 'POST']) -def test1(): - if request.method == 'POST': - autocomplete.load() - 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) - - print(autocomplete_suggestions) - - return Response(json.dumps(autocomplete_suggestions), mimetype='application/json') + 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 +# autocomplete_suggestions = [] + +# @app.route('/autocomplete_suggestions', methods=['GET', 'POST']) +# def test1(): +# if request.method == 'POST': +# autocomplete.load() +# 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) + +# print(autocomplete_suggestions) + +# return Response(json.dumps(autocomplete_suggestions), mimetype='application/json') @app.route('/add_to_stack/', methods=['GET', 'POST']) def add_to_stack(id):