diff --git a/app/views.py b/app/views.py index e10bd74..7e54a0d 100755 --- a/app/views.py +++ b/app/views.py @@ -85,19 +85,24 @@ def edit_book_by_id(id): if user_form.validate_on_submit(): # check if the post request has the file part title = user_form.title.data # You could also have used request.form['name'] - author = user_form.author.data # You could also have used request.form['email'] + authors = user_form.author.data # You could also have used request.form['email'] # save user to database #book = Book(title, author, filename, cover, file_extension) - book_to_edit.title = title db.session.commit() - book = Book.query.filter_by(title=title).first() - author_table = Author.query.filter_by(book_id=book.id).delete() - for this_author in author: - this_author = Author(this_author.get('author_name')) - book.authors.append(this_author) - db.session.commit() - + book = Book.query.filter_by(id=id).first() + book.title = title + book.authors= [] + db.session.commit() + 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 updated" % (title)) return redirect(url_for('show_books'))