|
|
@ -110,7 +110,7 @@ def remove_book_by_id(id):
|
|
|
|
@app.route('/books/<int:id>/edit', methods=['POST', 'GET'])
|
|
|
|
@app.route('/books/<int:id>/edit', methods=['POST', 'GET'])
|
|
|
|
def edit_book_by_id(id):
|
|
|
|
def edit_book_by_id(id):
|
|
|
|
book_to_edit = Book.query.filter_by(id=id).first()
|
|
|
|
book_to_edit = Book.query.filter_by(id=id).first()
|
|
|
|
user_form = EditForm(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category )
|
|
|
|
user_form = EditForm(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category, year_published= book_to_edit.year_published)
|
|
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if request.method == 'POST':
|
|
|
|
if user_form.validate_on_submit():
|
|
|
|
if user_form.validate_on_submit():
|
|
|
@ -118,15 +118,18 @@ def edit_book_by_id(id):
|
|
|
|
title = user_form.title.data # You could also have used request.form['name']
|
|
|
|
title = user_form.title.data # You could also have used request.form['name']
|
|
|
|
input_authors = user_form.author.data # You could also have used request.form['email']
|
|
|
|
input_authors = user_form.author.data # You could also have used request.form['email']
|
|
|
|
category = user_form.category.data
|
|
|
|
category = user_form.category.data
|
|
|
|
|
|
|
|
year_published = user_form.year_published.data
|
|
|
|
|
|
|
|
if year_published=="":
|
|
|
|
|
|
|
|
year_published = None
|
|
|
|
# save user to database
|
|
|
|
# save user to database
|
|
|
|
#book = Book(title, author, filename, cover, file_extension)
|
|
|
|
#book = Book(title, author, filename, cover, file_extension)
|
|
|
|
|
|
|
|
|
|
|
|
book = Book.query.filter_by(id=id).first()
|
|
|
|
book = Book.query.filter_by(id=id).first()
|
|
|
|
|
|
|
|
|
|
|
|
book.title = title
|
|
|
|
book.title = title
|
|
|
|
book.category = category
|
|
|
|
book.category = category
|
|
|
|
|
|
|
|
book.year_published = year_published
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#authors update
|
|
|
|
book.authors.clear()
|
|
|
|
book.authors.clear()
|
|
|
|
for i, author in enumerate(input_authors):
|
|
|
|
for i, author in enumerate(input_authors):
|
|
|
|
author_name = author.get("author_name")
|
|
|
|
author_name = author.get("author_name")
|
|
|
@ -138,7 +141,7 @@ def edit_book_by_id(id):
|
|
|
|
book.authors.append(a)
|
|
|
|
book.authors.append(a)
|
|
|
|
db.session.commit()
|
|
|
|
db.session.commit()
|
|
|
|
flash("%s updated" % (title))
|
|
|
|
flash("%s updated" % (title))
|
|
|
|
return redirect(url_for('show_books'))
|
|
|
|
return redirect(url_for('show_book_by_id', id=id))
|
|
|
|
|
|
|
|
|
|
|
|
return render_template('edit_book_detail.html', book=book_to_edit, form=user_form)
|
|
|
|
return render_template('edit_book_detail.html', book=book_to_edit, form=user_form)
|
|
|
|
|
|
|
|
|
|
|
@ -148,12 +151,16 @@ def add_book():
|
|
|
|
upload_form = UploadForm()
|
|
|
|
upload_form = UploadForm()
|
|
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
if upload_form.validate_on_submit():
|
|
|
|
|
|
|
|
#get data from form
|
|
|
|
title = upload_form.title.data
|
|
|
|
title = upload_form.title.data
|
|
|
|
authors = upload_form.author.data
|
|
|
|
authors = upload_form.author.data
|
|
|
|
category = upload_form.category.data
|
|
|
|
category = upload_form.category.data
|
|
|
|
year_published = upload_form.year_published.data
|
|
|
|
year_published = upload_form.year_published.data
|
|
|
|
|
|
|
|
if year_published=="":
|
|
|
|
|
|
|
|
year_published = None
|
|
|
|
|
|
|
|
|
|
|
|
if upload_form.validate_on_submit():
|
|
|
|
#if upload with file
|
|
|
|
if upload_form.upload.data:
|
|
|
|
if upload_form.upload.data:
|
|
|
|
# check if the post request has the file part
|
|
|
|
# check if the post request has the file part
|
|
|
|
if 'file' not in request.files:
|
|
|
|
if 'file' not in request.files:
|
|
|
@ -174,29 +181,18 @@ def add_book():
|
|
|
|
name, file_extension = os.path.splitext(new_filename)
|
|
|
|
name, file_extension = os.path.splitext(new_filename)
|
|
|
|
file.save(fullpath)
|
|
|
|
file.save(fullpath)
|
|
|
|
cover = get_cover(fullpath, name)
|
|
|
|
cover = get_cover(fullpath, name)
|
|
|
|
book = Book(title, filename, cover, file_extension, category, year_published)
|
|
|
|
|
|
|
|
db.session.add(book)
|
|
|
|
|
|
|
|
for author in authors:
|
|
|
|
|
|
|
|
author_name = author.get("author_name")
|
|
|
|
|
|
|
|
if author_name:
|
|
|
|
|
|
|
|
a = db.session.query(Author).filter_by(author_name=author_name).first()
|
|
|
|
|
|
|
|
if a == None:
|
|
|
|
|
|
|
|
a = Author(author_name=author_name)
|
|
|
|
|
|
|
|
db.session.add(a)
|
|
|
|
|
|
|
|
book.authors.append(a)
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# save user to database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flash("%s added to the library" % (title))
|
|
|
|
|
|
|
|
return redirect(url_for('show_books'))
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
flash('allowed file formats: %s' % ALLOWED_EXTENSIONS)
|
|
|
|
flash('allowed file formats: %s' % ALLOWED_EXTENSIONS)
|
|
|
|
|
|
|
|
#if upload without file -> wishform, with potential PDF
|
|
|
|
if upload_form.wish.data:
|
|
|
|
if upload_form.wish.data:
|
|
|
|
file = open('app/uploads/potential.pdf')
|
|
|
|
#TO DO: make pdf generator
|
|
|
|
filename = 'potential.pdf'
|
|
|
|
#file = open('app/uploads/potential.pdf')
|
|
|
|
file_extension = '.pdf'
|
|
|
|
#filename = 'potential.pdf'
|
|
|
|
|
|
|
|
#file_extension = '.pdf'
|
|
|
|
|
|
|
|
filename = ''
|
|
|
|
|
|
|
|
file_extension = ''
|
|
|
|
cover = ''
|
|
|
|
cover = ''
|
|
|
|
|
|
|
|
|
|
|
|
book = Book(title, filename, cover, file_extension, category,year_published)
|
|
|
|
book = Book(title, filename, cover, file_extension, category,year_published)
|
|
|
|
db.session.add(book)
|
|
|
|
db.session.add(book)
|
|
|
|
for author in authors:
|
|
|
|
for author in authors:
|
|
|
|