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 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 book_view(request, user_id): books = Book.objects.filter(reader=user_id) reader = User.objects.get(id=user_id) avg_cost = books.aggregate(Avg('cost')) form = NewBookForm(request.POST or None) if form.is_valid(): book_name = form.cleaned_data['book_name'] author_full_name = form.cleaned_data['author_full_name'] year = form.cleaned_data['year'] cost = form.cleaned_data['cost'] pages = form.cleaned_data['pages'] Book.objects.create( reader=reader, book_name=book_name, author_full_name=author_full_name, year=year, cost=cost, pages=pages) return HttpResponseRedirect('/book/'+user_id+'/') context = { 'books': books, 'reader': reader, 'avg_cost': avg_cost.get('cost__avg'), 'form': form } return render(request, 'book.html', context)
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)
def test_new_form_year_field_label(self): form = NewBookForm() self.assertTrue(form.fields['year'].label == 'Год издания')
def test_new_form_author_field_label(self): form = NewBookForm() self.assertTrue(form.fields['author_full_name'].label == 'Автор')
def test_new_form_name_field_label(self): form = NewBookForm() self.assertTrue(form.fields['book_name'].label == 'Название книги')