|
@ -78,7 +78,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 = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.author) |
|
|
user_form = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.authors) |
|
|
|
|
|
|
|
|
if request.method == 'POST': |
|
|
if request.method == 'POST': |
|
|
if user_form.validate_on_submit(): |
|
|
if user_form.validate_on_submit(): |
|
@ -91,10 +91,10 @@ def edit_book_by_id(id): |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
book = Book.query.filter_by(title=title).first() |
|
|
book = Book.query.filter_by(title=title).first() |
|
|
author_table = Author.query.filter_by(user_id=book.id).delete() |
|
|
author_table = Author.query.filter_by(book_id=book.id).delete() |
|
|
for this_author in author: |
|
|
for this_author in author: |
|
|
this_author = Author(this_author.get('author_name')) |
|
|
this_author = Author(this_author.get('author_name')) |
|
|
book.author.append(this_author) |
|
|
book.authors.append(this_author) |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
|
|
|
|
|
|
flash("%s updated" % (title)) |
|
|
flash("%s updated" % (title)) |
|
@ -127,17 +127,20 @@ def add_book(): |
|
|
file.save(fullpath) |
|
|
file.save(fullpath) |
|
|
cover = get_cover(fullpath, name) |
|
|
cover = get_cover(fullpath, name) |
|
|
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'] |
|
|
author = user_form.author.data # You could also have used |
|
|
authors = user_form.author.data # You could also have used |
|
|
tag = user_form.tag.data |
|
|
tag = user_form.tag.data |
|
|
print(author) |
|
|
#print(author) |
|
|
print(len(author)) |
|
|
#print(len(author)) |
|
|
book = Book(title, filename, cover, file_extension, tag) |
|
|
book = Book(title, filename, cover, file_extension, tag) |
|
|
db.session.add(book) |
|
|
db.session.add(book) |
|
|
db.session.commit() |
|
|
for author in authors: |
|
|
book = Book.query.filter_by(title=title).first() |
|
|
author_name = author.get("author_name") |
|
|
for this_author in author: |
|
|
if author_name: |
|
|
this_author = Author(this_author.get('author_name')) |
|
|
a = db.session.query(Author).filter_by(author_name=author_name).first() |
|
|
book.author.append(this_author) |
|
|
if a == None: |
|
|
|
|
|
a = Author(author_name=author_name) |
|
|
|
|
|
db.session.add(a) |
|
|
|
|
|
book.authors.append(a) |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
# save user to database |
|
|
# save user to database |
|
|
|
|
|
|
|
@ -159,6 +162,13 @@ def flash_errors(form): |
|
|
error |
|
|
error |
|
|
)) |
|
|
)) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/authors/<int:id>') |
|
|
|
|
|
def show_author_by_id(id): |
|
|
|
|
|
author = Author.query.get(id) |
|
|
|
|
|
if not author: |
|
|
|
|
|
abort(404) |
|
|
|
|
|
else: |
|
|
|
|
|
return render_template('show_author_detail.html', author=author) |
|
|
|
|
|
|
|
|
### |
|
|
### |
|
|
# The API |
|
|
# The API |
|
|