def newBook(): if request.method == 'POST': newBook = Book() newBook.title = request.form['title'] newBook.author = request.form['author'] newBook.price = request.form['price'] newBook.description = request.form['description'] db.add(newBook) db.commit() flash(newBook.title + ' book created!') return redirect(url_for('list')) return render_template('new.html')
def newBook(): if request.method == 'POST': newBook = Book(title=request.form['name'], author=request.form['author'], genre=request.form['genre']) session.add(newBook) session.commit() return redirect(url_for('showBooks')) else: return render_template('createbook.html')
def book(): form = BookForm(request.form) print(form) catgories = db_session.query(Category).all() # create category list catgories_list = [(category.id, category.name) for category in catgories] form.category.choices = catgories_list if request.method == 'POST' and form.validate(): # auto generate category id in bettwen 10 - 20000 book = Book(randint(10, 20000), form.category.data, form.name.data, form.author.data, form.price.data, form.description.data, form.image.data) db_session.add(book) return redirect('/home') return render_template('book_create.html', form=form)
def addNewBook(category_id): # Add a new book to the category if 'username' not in login_session: return redirect('/login') category = session.query(Category).filter_by(id=category_id).one() if request.method == 'POST': newBook = Book(title=request.form['title'], author=request.form['author'], description=request.form['description'], category_id=category_id, user_id=login_session['user_id']) session.add(newBook) session.commit() flash('new book %s was added to the catalog' % (newBook.title)) return redirect(url_for('showCategory', category_id=category_id)) else: return render_template('addbook.html', category=category, categories=getCategories())
# this file fills the books data and put them to database from db_setup import session from db_setup import Category, Book # populate the database: geography = Category(name="Geography") session.add(geography) session.commit() history = Category(name="History") session.add(history) session.commit() computer_science = Category(name="Computer Science") session.add(computer_science) session.commit() history_of_england = Book( name="History of England", author="Arthur Dale", category=history.id, ) session.add(history_of_england) session.commit()
# bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance engine = create_engine( "mysql://*****:*****@54.74.234.11/peter_demo?charset=utf8mb4") Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. session = DBSession() # Create bookOne = Book(title="The No. 1 Ladies' Detective Agency", author="Alexander McCall Smith", genre='fiction') session.add(bookOne) session.commit() # Read session.query(Book).all() session.query(Book).first() # Update #editedBook = session.query(Book).filter_by(id=1).one() #editedBook.author = "Alexander McCall Smith" #session.add(editedBook) #session.commit()
def makeANewBook(title, author, genre): addedbook = Book(title=title, author=author, genre=genre) session.add(addedbook) session.commit() return jsonify(Book=addedbook.serialize)
firstUser = User(name='Chess Lover', email='*****@*****.**', picture='http://bit.ly/2DdqbNl') session.add(firstUser) session.commit() ''' Create category 'Improvement' ''' category1 = Category(name='Improvement', user=firstUser) session.add(category1) session.commit() # Add book 'My System' description = 'My System is at the top of a very short list of chess classics' mySystem = Book(title='My System', author='Aron Nimzowitsch', description=description, category=category1, user=firstUser) session.add(mySystem) session.commit() # Add book 'Under the Surface' description = '''This book invites you beneath the surface, where you can learn to navigate the depths of chess''' underTheSurface = Book(title='Under the Surface', author='Jan Markos', description=description, category=category1, user=firstUser) session.add(underTheSurface) session.commit() # Add book 'Understanding Chess Move by Move'