Ejemplo n.º 1
0
def add_book_db(message, src):
    db_sess = db_session.create_session()
    new_book = Book()
    new_book.name = message.text
    new_book.container = src
    db_sess.add(new_book)
    db_sess.commit()
    bot.reply_to(message, "Пожалуй, я сохраню это!")
Ejemplo n.º 2
0
def add_book():
    form = BookForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Book()
        book.title = form.title.data
        book.author = form.author.data
        genre = db_sess.query(Genre).filter(Genre.title == form.genre.data).first()
        if not genre:
            new_genre = Genre(title=form.genre.data)
            db_sess.add(new_genre)
            db_sess.commit()
            genre = db_sess.query(Genre).filter(Genre.title == form.genre.data).first()
        book.genre = genre
        book.created_date = form.created_date.data
        book.annotation = form.annotation.data
        book.img_file = form.img_file.data.filename
        print(form.img_file.data)
        print(type(form.img_file.data))
        form.img_file.data.save(f'static/img/{form.img_file.data.filename}')
        book.text_file = form.text_file.data.filename
        form.text_file.data.save(f'static/text/{form.text_file.data.filename}')
        genre.book.append(book)
        db_sess.merge(genre)
        db_sess.commit()
        return redirect('/')
    return render_template('book.html', title='Добавление книги', form=form)
Ejemplo n.º 3
0
    def add_book():
        # Метод для добавления новой книги.
        # Информация о коллекция выбирается автоматически из коллекций, принадлежащих пользователю.
        # Из-за того, что в RadioField находится только индекс выбранного элемента, выбор коллекции производится вручную
        form = BookForm()

        db_sess = db_session.create_session()
        if current_user.is_authenticated:
            collections = db_sess.query(Collection).filter((Collection.user == current_user))
        else:
            collections = []

        index = form.collection_id.data

        if request.method == "POST" and form.validate_on_submit():
            db_sess = db_session.create_session()
            book = Book()
            book.title = form.title.data
            book.author = form.author.data
            book.opinion = form.opinion.data
            book.year = form.year.data
            book.is_private = form.is_private.data
            if index is not None:
                book.collection_id = collections[int(index) - 1].id
            current_user.books.append(book)
            db_sess.merge(current_user)
            db_sess.commit()
            return redirect('/books')

        choices = []
        for item in collections:
            choices.append((item.id, item.name))
        form.collection_id.choices = choices

        return render_template('add_book.html', title='Добавление книги', form=form, collections=collections.count())
Ejemplo n.º 4
0
def add_book():
    if not current_user.is_authenticated:
        return redirect('/library/login')
        # не работает
    if not current_user.admin:
        access_error('Добавление книги')
    form = BooksForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Book()
        book.title = form.title.data
        book.author = form.author.data
        book.year = form.year.data
        book.genre = form.genre.data
        book.description = form.description.data
        book.text = form.text.data
        db_sess.add(book)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect('/library')

    return render_template('add_book_page.html',
                           title='Добавление книги',
                           ddescription='YLibrary',
                           form=form)
Ejemplo n.º 5
0
def add_book():
    form = BooksForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Book(
            title=form.title.data,
            letter=form.letter.data,
        )
        db_sess.add(book)
        db_sess.commit()
        return redirect('/add-book')
    return render_template('add-book.html', form=form)
Ejemplo n.º 6
0
def add_book():
    form = BookForm()
    session = db_session.create_session()
    form.categories.choices = [
        (category.id, category.name)
        for category in session.query(Category).order_by(Category.name).all()
    ]
    if form.validate_on_submit():
        user = session.query(User).get(current_user.id)

        # noinspection PyArgumentList
        book = Book(title=form.title.data,
                    description=form.description.data,
                    price=form.price.data,
                    stock=form.stock.data,
                    release_date=form.release_date.data)

        if form.is_user_author:
            author = session.query(Author).get(user.author_id)
            if not author:
                # noinspection PyArgumentList
                author = Author(first_name=current_user.first_name,
                                last_name=current_user.last_name)
                user.author = author
            book.author = author
        # Save image and/or book file to server if provided
        if form.image.data:
            image_path = resize_and_save_image(form.image.data.filename)
            book.image_path = image_path
        if form.content.data:
            file_path = save_file(form.content.data.filename)
            book.file_path = file_path
        session.merge(book)
        session.commit()
        return redirect('/')
    return render_template('add_book.html',
                           title='Add book',
                           form=form,
                           header='Add book')
Ejemplo n.º 7
0
def add_book():
    form = AddBookForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Book()
        book.title = form.title.data
        book.user_uploaded = current_user.id
        f = request.files['file']
        book_text = f.read()
        new_file = open(
            f"static/books/{form.author.data} - {form.title.data}.txt",
            mode="wb")
        new_file.write(book_text)
        new_file.close()
        book.file = f"static/books/{form.author.data} - {form.title.data}.txt"
        a_name = form.author.data
        book.author_name = a_name
        book.user_uploaded_name = current_user.username
        a_exists = 0
        for a in db_sess.query(Author).filter(Author.name == a_name).all():
            a_exists = 1
            book.author_id = a.id
        if not a_exists:
            author = Author()
            author.name = a_name
            author.books = ""
            db_sess.add(author)
            for a in db_sess.query(Author).filter(Author.name == a_name).all():
                book.author_id = a.id
        db_sess.add(book)
        for bk in db_sess.query(Book).filter(
                Book.title == form.title.data).all():
            book_id = bk.id
        for a in db_sess.query(Author).filter(Author.name == a_name).all():
            a.books = ", ".join(
                [i for i in str(a.books).split(", ") + [str(book_id)] if i])

        db_sess.commit()
        return redirect('/')
    return render_template('add_book.html', form=form)
Ejemplo n.º 8
0
def add_book():
    form = BooksForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        book = Book()
        book.name = form.name.data
        book.author = form.author.data
        book.min_age = form.min_age.data
        book.is_booked = form.is_booked.data
        db_sess.add(book)
        db_sess.commit()
        return redirect('/')
    return render_template('books.html', title='Adding a book', form=form)
Ejemplo n.º 9
0
def create_book():
    # Апи для добавления книги.
    # Если какой-то из параметров не передан, то будет возвращена ошибка
    if not request.json:
        return jsonify({'error': 'Empty request'})
    elif not all(key in request.json for key in
                 ['title', 'author', 'year', 'is_private', 'user_id']):
        return jsonify({'error': 'Bad request'})
    db_sess = db_session.create_session()
    book = Book()
    book.title = request.json['title']
    book.author = request.json['author']
    book.year = request.json['year']
    book.is_private = request.json['is_private']
    book.user_id = request.json['user_id']
    db_sess.add(book)
    db_sess.commit()
    return jsonify({'success': 'OK'})
Ejemplo n.º 10
0
def add_job():
    try:
        if not current_user.is_authenticated:
            return redirect('/library/login')
        if not current_user.admin:
            return AccessError
        form = BooksForm()
        if form.validate_on_submit():
            db_sess = create_session()
            book = Book()
            book.title = form.title.data
            book.author = form.author.data
            book.year = form.year.data
            book.genre = form.genre.data
            book.description = form.description.data
            db_sess.add(book)
            db_sess.merge(current_user)
            db_sess.commit()
            return redirect('/library')
    except AccessError:
        return  # ругается на отсутствие прав доступа
    return render_template('books.html', title='Добавление книги',
                           form=form)