def add_book(): if not current_user.is_moderator: return redirect(url_for('index')) form = NewBookForm() if form.validate_on_submit(): book = Book( writer_id=Writer.query.filter_by(name=form.author.data).first().id, title=form.title.data, genre=form.genre.data) db.session.add(book) db.session.commit() flash(f'You\'ve added book {form.title.data} by {form.author.data}') return redirect(url_for('add_book')) return render_template('add_book.html', title='add book', form=form)
def addbook(): form = NewBookForm() form.authors.choices = [(author.id, author.name) for author in Author.query.all()] if form.validate_on_submit(): name = form.name.data authors = [Author.query.filter_by(id=author).first() for author in form.authors.data] book = Book(name=name) for author in authors: book.authors.append(author) db.session.add(book) db.session.commit() return redirect(url_for('books')) return render_template("addbook.html", form=form, title='Home', user=current_user, books=books, )
def new_book(): form = NewBookForm() if form.validate_on_submit(): if form.book_title.data is None: flash("Fill in a book title to create a book!") return redirect(url_for('show_books')) book = Book(book_title=form.book_title.data) author = form.author.data book.authors.append(author) book.user = g.user if book.book_title in db.session.query(Book.book_title).all(): flash('This book title already exists.') return redirect(url_for('new_book')) db.session.add(book) db.session.commit() flash('Book successfully added.') return redirect(url_for('show_books')) return render_template('newbook.html', form=form)
def new_book(): # Handle form POST request form = NewBookForm() if form.validate_on_submit(): book_params = form.data.copy() book_params['authors'] = [author.strip() for author in book_params['authors'].split(',')] book_params['keywords'] = [keyword.strip() for keyword in book_params['keywords'].split(',')] book = Book(**book_params) if save(book): flash('New book created successfully!') return redirect(url_for('store_manager.edit_book', ISBN=book.ISBN)) else: flash('Failed to create new book. Please try again.') return render_template('store_manager/book/new.html', form=form)
def editbook(id=None, name=''): book = Book.query.filter_by(id=id).first() form = NewBookForm() form.authors.choices = [(author.id, author.name) for author in Author.query.all()] form.name.default = book.name form.authors.data=[author.id for author in book.authors] print(book.name) if form.validate_on_submit(): name = form.name.data authors = [Author.query.filter_by(id=author).first() for author in form.authors.data] book.name=name book.authors=[] for author in authors: book.authors.append(author) db.session.add(book) db.session.commit() return redirect(url_for('books')) return render_template("editBook.html", form=form, title='Edit', user=current_user, books=books, )
def new_book(): if current_user.id != 1: flash('Данная страница не доступна для Вас!') return redirect(url_for('index')) form = NewBookForm() if form.validate_on_submit(): cursor.execute('select id from book where isbn= %s;', (form.isbn.data, )) book = cursor.fetchone() if book is None: cursor.execute( 'insert into book (isbn, title, price, publishing_house, quantity_in_stock, image, description) values(%s, %s, %s, %s, %s, %s, %s);', ( form.isbn.data, form.title.data, form.price.data, form.publishing_house.data, form.quantity_in_stock.data, form.image.data, form.description.data, )) cursor.execute('select id from book where isbn= %s;', (form.isbn.data, )) id_book = cursor.fetchone() conn.commit() authors = form.authors.data.split('; ') categories = form.categories.data.split('; ') for author in authors: cursor.execute('select id from author where fio= %s;', (author, )) id_author = cursor.fetchone() if id_author is None: cursor.execute('insert into author (fio) values(%s);', (author, )) cursor.execute('select id from author where fio= %s;', (author, )) id_author = cursor.fetchone() conn.commit() cursor.execute( 'insert into author_book (id_book, id_author) values(%s, %s);', ( id_book, id_author, )) conn.commit() for category in categories: cursor.execute('select id from category where title = %s;', (category, )) id_category = cursor.fetchone() if id_category is None: cursor.execute('insert into category (title) values(%s);', (category, )) cursor.execute('select id from category where title = %s;', (category, )) id_category = cursor.fetchone() conn.commit() cursor.execute( 'insert into category_book (id_book, id_category) values(%s, %s);', ( id_book, id_category, )) conn.commit() flash('Книга добавлена в базу!') return redirect(url_for('books')) else: flash('По номеру ISBN уже существует книга в базе!') return redirect(url_for('new_book')) return render_template('new_book.html', title='Добавление книги в базу', form=form)