|
|
@ -60,7 +60,11 @@ def show_books(): |
|
|
|
books = db.session.query(Book).all() |
|
|
|
search = SearchForm(request.form) |
|
|
|
if request.method == 'POST': |
|
|
|
if search.select.data == 'Title': |
|
|
|
return redirect((url_for('search_results', query=search.search.data))) |
|
|
|
if search.select.data == 'Category': |
|
|
|
return redirect((url_for('search_cat', query=search.search.data))) |
|
|
|
|
|
|
|
return render_template('show_books.html', books=books, form=search) |
|
|
|
|
|
|
|
@app.route('/scape', methods=['POST', 'GET']) |
|
|
@ -155,6 +159,10 @@ def add_book(): |
|
|
|
upload_form = UploadForm() |
|
|
|
|
|
|
|
if request.method == 'POST': |
|
|
|
title = upload_form.title.data |
|
|
|
authors = upload_form.author.data |
|
|
|
category = upload_form.category.data |
|
|
|
year_published = upload_form.year_published.data |
|
|
|
|
|
|
|
if upload_form.validate_on_submit(): |
|
|
|
if upload_form.upload.data: |
|
|
@ -177,13 +185,6 @@ def add_book(): |
|
|
|
name, file_extension = os.path.splitext(new_filename) |
|
|
|
file.save(fullpath) |
|
|
|
cover = get_cover(fullpath, name) |
|
|
|
title = upload_form.title.data # You could also have used request.form['name'] |
|
|
|
authors = upload_form.author.data # You could also have used |
|
|
|
category = upload_form.category.data |
|
|
|
|
|
|
|
year_published = upload_form.year_published.data |
|
|
|
#print(author) |
|
|
|
#print(len(author)) |
|
|
|
book = Book(title, filename, cover, file_extension, category, year_published) |
|
|
|
db.session.add(book) |
|
|
|
for author in authors: |
|
|
@ -197,7 +198,6 @@ def add_book(): |
|
|
|
db.session.commit() |
|
|
|
# save user to database |
|
|
|
|
|
|
|
|
|
|
|
flash("%s added to the library" % (title)) |
|
|
|
return redirect(url_for('show_books')) |
|
|
|
else: |
|
|
@ -207,15 +207,7 @@ def add_book(): |
|
|
|
file = open('app/uploads/potential.pdf') |
|
|
|
filename = 'potential.pdf' |
|
|
|
file_extension = '.pdf' |
|
|
|
cover = get_cover('app/uploads/potential.pdf', 'default') |
|
|
|
|
|
|
|
title = upload_form.title.data # You could also have used request.form['name'] |
|
|
|
authors = upload_form.author.data # You could also have used |
|
|
|
category = upload_form.category.data |
|
|
|
|
|
|
|
year_published = upload_form.year_published.data |
|
|
|
#print(author) |
|
|
|
#print(len(author)) |
|
|
|
cover = '' |
|
|
|
book = Book(title, filename, cover, file_extension, category,year_published) |
|
|
|
db.session.add(book) |
|
|
|
for author in authors: |
|
|
@ -231,7 +223,6 @@ def add_book(): |
|
|
|
flash("%s added to the library" % (title)) |
|
|
|
return redirect(url_for('show_books')) |
|
|
|
|
|
|
|
|
|
|
|
flash_errors(upload_form) |
|
|
|
return render_template('add_book.html', form=upload_form) |
|
|
|
|
|
|
@ -277,10 +268,9 @@ def show_stack_by_id(id): |
|
|
|
|
|
|
|
## search |
|
|
|
|
|
|
|
@app.route('/search/<query>/', methods=['POST', 'GET']) |
|
|
|
@app.route('/search/titles/<query>/', methods=['POST', 'GET']) |
|
|
|
def search_results(query): |
|
|
|
search = SearchForm(request.form) |
|
|
|
books_all=Book.query.all() |
|
|
|
random_order=Book.query.order_by(func.random()).limit(10) |
|
|
|
results=Book.query.filter(Book.title.contains(query)).all() |
|
|
|
|
|
|
@ -291,11 +281,32 @@ def search_results(query): |
|
|
|
if request.method == 'POST': |
|
|
|
query = search.search.data |
|
|
|
results = [] |
|
|
|
results=Book.query.filter(Book.title.contains(query)).all() |
|
|
|
if search.select.data == 'Title': |
|
|
|
return redirect((url_for('search_results', query=search.search.data))) |
|
|
|
if search.select.data == 'Category': |
|
|
|
return redirect((url_for('search_cat', query=search.search.data))) |
|
|
|
|
|
|
|
return render_template('results.html', form=search, books=results, books_all=random_order, query=query) |
|
|
|
|
|
|
|
@app.route('/search/cat/<query>/', methods=['POST', 'GET']) |
|
|
|
def search_cat(query): |
|
|
|
search = SearchForm(request.form) |
|
|
|
random_order=Book.query.order_by(func.random()).limit(10) |
|
|
|
results=Book.query.filter(Book.category.contains(query)).all() |
|
|
|
|
|
|
|
if not results: |
|
|
|
upload_form = UploadForm(category=query) |
|
|
|
return render_template('red_link.html', form=upload_form, category=query) |
|
|
|
|
|
|
|
if request.method == 'POST': |
|
|
|
query = search.search.data |
|
|
|
results = [] |
|
|
|
if search.select.data == 'Title': |
|
|
|
return redirect((url_for('search_results', query=search.search.data))) |
|
|
|
if search.select.data == 'Category': |
|
|
|
return redirect((url_for('search_cat', query=search.search.data))) |
|
|
|
|
|
|
|
return render_template('results.html', form=search, books=results, books_all=random_order, query=query) |
|
|
|
|
|
|
|
### |
|
|
|
# The API |
|
|
|