def change_book(book_id): if not session.get("logged"): abort(401) if request.method != "POST": book = db_handler.get_book(book_id) list_author_id = [author.id for author in book.authors_by] form = form_handler.AddBookForm(title = book.title, choose_authors = list_author_id) else: form = form_handler.AddBookForm(request.form) authors = db_handler.get_all_authors() form.choose_authors.choices = [(g.id, g.name) for g in authors] if request.method == "POST" and form.validate(): db_handler.change_book(book_id, form.title.data, form.choose_authors.data) flash("Changes book ({0}) save successfully".format(form.title.data)) return redirect(url_for('index')) elif request.method == "POST": for field in form: for error in field.errors: flash(error) return render_template("change_book.html", form=form, id=book_id)
def change_book(book_id): if not session.get("logged"): abort(401) if request.method != "POST": book = db_handler.get_book(book_id) list_author_id = [author.id for author in book.authors_by] form = form_handler.AddBookForm(title=book.title, choose_authors=list_author_id) else: form = form_handler.AddBookForm(request.form) authors = db_handler.get_all_authors() form.choose_authors.choices = [(g.id, g.name) for g in authors] if request.method == "POST" and form.validate(): db_handler.change_book(book_id, form.title.data, form.choose_authors.data) flash("Changes book ({0}) save successfully".format(form.title.data)) return redirect(url_for('index')) elif request.method == "POST": for field in form: for error in field.errors: flash(error) return render_template("change_book.html", form=form, id=book_id)