def add_book(): form = AddBookForm() db_sess = db_session.create_session() if request.method == "POST": book = Book() book.title = form.title.data book.book_author = form.book_author.data book.genre_id = form.genre.data book.created_date = datetime.now() book.updated_date = datetime.now() book.content_analysis = request.form['text'] file_image = request.files['file'] file_book = request.files['file_book'] image_link = f"static/images/skins/{book.title}-{random.randint(1, 100000)}.png" file_link = f"static/files/{file_book.filename}" if file_image: with open(image_link, "wb") as file_write: file_write.write(file_image.read()) book.image_link = image_link if file_book: with open(file_link, "wb") as file_write: file_write.write(file_book.read()) book.pdf_link = file_link current_user.books.append(book) db_sess.merge(current_user) db_sess.commit() last_id = [int(*i) for i in db_sess.query(Book.id).all()] return redirect(f"/show_book/{last_id[-1]}") else: form.genre.choices = [(i.id, i.title) for i in db_sess.query(Genre).all()] return render_template("add_book_page.html", form=form)
def prepare_books(directory="books"): session = db_session.create_session() names = [f"{obj.name}.txt" for obj in session.query(Book).all()] for book in os.listdir(directory): if book not in names: with open(f"{directory}/{book}", encoding="utf-8", errors="ignore") as file: new_book = Book() new_book.name = book.split(".")[0] new_book.author = file.readline().strip() new_book.path = f"{directory}/{book}" session.add(new_book) session.commit()
def create_book(title, author, pages, publish_date, publisher): book = Book() book.title = title book.author.append(author) book.pages = pages if publish_date: book.publish_date = publish_date book.publisher = publisher book.save() return book
def add_book(): form = AddBookForm() if form.validate_on_submit(): session = db_session.create_session() book = Book() book.name = form.name.data book.author = form.author.data book.text = form.text.data book.is_private = form.is_private.data path = add_new_book(book, book.text) book.path = path user = session.query(User).filter(User.id == current_user.id).first() user.added_books.append(book) session.merge(user) session.commit() return redirect("/") return render_template("add_book.html", title="Добавление своей книги", form=form)
def create_book(num): books = [] publishers = Publisher.objects() subscribers = Subscriber.objects() for i in range(num): book = Book() book.title = fake.sentence(nb_words=4) book.author.append(fake.name()) book.pages = fake.pyint(min_value=100, max_value=600) book.publish_date = fake.date() book.publisher = random.choices(publishers)[0] borrow = Borrow() borrow.subscriber = random.choices(subscribers)[0] borrow.start = fake.date_object() borrow.end = borrow.start + datetime.timedelta( days=random.randint(14, 60)) book.borrowing_history.append(borrow) books.append(book) return books
def add_book(): form = BookForm() if form.validate_on_submit(): session = db_session.create_session() user = session.query(User).filter(User.id == current_user.id).first() id = len(session.query(Book).all()) book = Book() book.author = current_user.id book.title = form.title.data book.about = form.about.data book.year_published = form.year_published.data if len(current_user.books_written ) == 0 or current_user.books_written is None: user.books_written += str(id + 1) else: user.books_written += ', ' + str(id + 1) session.add(book) session.commit() f = request.files['file'] f.save('static/img/{}.jpg'.format(str(id + 1))) return redirect('/') return render_template('new_book.html', title='Adding a book', form=form)
def get_book_by_id(id): book = Book.objects(id=id).first() return book
def get_book(bk): book = Book.objects(title=bk).first() return book
def list_book(): book = Book.objects() return book
def book_add_subscriber(bk, subscriber, start, end): book = Book(bk) borrow = Borrow(subscriber=subscriber, start=start, end=end) book.borrowing_history.append(borrow) book.save() return book