Beispiel #1
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())
Beispiel #2
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)
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'})
Beispiel #4
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)