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 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)