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