示例#1
0
def add_book():
    form = BookForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Books()
        book.title = form.title.data
        book.pic_url = form.pic_url.data
        book.author = form.author.data
        book.content = form.content.data
        book.file_name = form.file.data.filename
        book.marked_file_name = f"marked_{form.file.data.filename}"
        form.file.data.save(os.path.join(
            app.auto_find_instance_path(), form.file.data.filename))
        current_user.books.append(book)
        db_sess.merge(current_user)
        db_sess.commit()
        book_load(os.path.join(
            app.auto_find_instance_path(), form.file.data.filename), f'{book.file_name}')
        path = os.path.join(
            app.auto_find_instance_path(), form.file.data.filename)
        path2 = os.path.join(
            app.auto_find_instance_path(), f'{book.marked_file_name}.csv')
        if marking(book.file_name, path, path2):
            book_load(os.path.join(
                app.auto_find_instance_path(), f'{book.marked_file_name}.csv'), f'{book.marked_file_name}.csv')
        os.remove(os.path.join(
            app.auto_find_instance_path(), book.file_name))
        os.remove(os.path.join(
            app.auto_find_instance_path(), f'{book.marked_file_name}.csv'))
        disk.publish(f"/book/{book.file_name}")
        disk.publish(f"/book/{book.marked_file_name}.csv")
        return redirect('/')
    return render_template('books.html', file_label='Файл книги (.txt)', title='Добавление книги',
                           form=form)
示例#2
0
def add_books():
    form = BooksForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        books = Books()
        books.title = form.title.data
        books.author = form.author.data
        books.content = form.content.data
        current_user.books.append(books)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect('/')
    return render_template('newBook.html', title='Добавление книги', form=form)