示例#1
0
def add_book():
    form = BookForm()
    if form.validate_on_submit():
        if request.method == 'POST':
            existing_book = Book.query.filter(
                Book.name == form.name.data).filter(
                    Book.author == form.author.data).first()
            book = Book(name=form.name.data, author=form.author.data)

            if (existing_book == None):
                book.create_time = datetime.utcnow()
                db.session.add(book)
            elif (current_user not in book.user):
                book.user.append(current_user)
                book.create_time = datetime.utcnow()
            else:
                flash('Book already exists!')
                return redirect(url_for('add_book'))
            #   return redirect(url_for('add_book'))
        #and(existing_book.name == form.name.data and  existing_book.author == form.author.data and existing_book.user_id == current_user.id)):
        #   flash('Book already exists!')
        #   return redirect(url_for('add_book'))
            db.session.commit()

            flash('Your book succesfully added!')
            return redirect(url_for('index'))
    return render_template("add_book.html", title='Add_book', form=form)