def newBook(city_id): if 'username' not in login_session: flash('You need to be logged in to add a book.', 'error') return redirect('/cities') city = session.query(City).filter_by(id=city_id).one() if not city: flash("There is no city with id %d" % city_id) return redirect('/cities') if request.method == 'POST': if request.form['token'] != login_session['token']: # no flash message, we don't answer CSRFs return redirect('/cities') newBook = Book(title=request.form['title'], author=request.form['author'], city_id=city_id, owner_id=login_session['user_id'], status="Available") if request.form['image_url']: newBook.image_url = request.form['image_url'] session.add(newBook) session.commit() flash("New book was successfully added: %s" % request.form['title']) return redirect(url_for('bookList', city_id=city_id)) else: # token to protect against CSRF (cross-site request forgery) login_session['token'] = ''.join( random.choice(string.ascii_uppercase + string.digits) for x in xrange(16)) return render_template('newbook.html', city=city, TOKEN=login_session['token'])
def addBook(author): author = session.query(Author).filter_by(last_name=author.title()).first() if login_session['user_id'] != author.user_id: return redirect(url_for('showAuthor', author=author.last_name)) if request.method == 'POST': newBook = (request.form['name'].title(), request.form['image'], request.form['amazon'], request.form['description']) if newBook: userID = login_session['user_id'] session.add( Book(name=newBook[0], image=newBook[1], amazon=newBook[2], description=newBook[3], author_id=author.id, user_id=userID)) session.commit() flash("Book has been added!") return redirect( url_for('showBook', author=author.last_name, book=newBook[0])) else: flash("You need to fill out all the fields!") return render_template('addbook.html', author=author) return render_template('addbook.html', author=author)
def make(self): with open('data/tiki.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row[0].strip()) book = Book(row[0].strip(), row[1].strip(), row[2].strip(), row[3].strip(), row[4].strip()) db.session.add(book) db.session.commit()
def newbook(): if 'username' not in login_session: return redirect('/login') #return "Create new book" if request.method == 'POST': upload() newbook = Book(name=request.form['name'], author=request.form['author'], picture=request.form['picture'], user_id=login_session['user_id']) session.add(newbook) session.commit() return redirect(url_for('books')) else: return render_template('newbook.html')
def new_book(author_id): author = db.session.query(Author).filter_by(id=author_id).first() books = db.session.query(Book).filter_by(author_id=author_id).all() if request.method == 'POST': newBook = Book(name=request.form['name'], description=request.form['description'], price=request.form['price'], author_id=author_id) db.session.add(newBook) db.session.commit() return redirect(url_for('index_2')) else: return render_template('newBook.html', books=books, author_id=author_id, author=author)
def add_book(): if not request.json or Session.query(Book).filter( Book.id == request.json['id']).first(): abort(400) new_book = Book(id=request.json['id'], title=request.json['title'], created_at=datetime.now()) Session.add(new_book) authors = request.json['authors_id'] for author_id in authors: author = Session.query(Author).filter(Author.id == author_id) if not author: Session.rollback() return 'No author with id %i' % author_id Session.add( AuthorBookLink(author_id=author_id, book_id=request.json['id'])) Session.commit() return 'OK', 200
def post(self): args = booksV.parse_args() result = Book.getAll(args) return result['data'], result['code']
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. Any change made against the objects in the session won't be persisted into the database until you call session.commit(). # If you're not happy about the changes, you can revert all of them back to the last commit by calling session.rollback() session = DBSession() #Add a Dummy User User1 = User(name='Lucy Cloudy', email='*****@*****.**') session.add(User1) session.commit() #Add books Book1 = Book( name="Harry Potter and Philosopher's Stone", author='J. K. Rowling', picture= "https://upload.wikimedia.org/wikipedia/en/6/6b/Harry_Potter_and_the_Philosopher's_Stone_Book_Cover.jpg" ) session.add(Book1) session.commit() Book2 = Book( name='Atlas Shrugged', author='Ayn Rand', picture='https://upload.wikimedia.org/wikipedia/en/8/84/AtlasShrugged.jpg') session.add(Book2) session.commit() Book3 = Book( name='Nineteen Eighty-Four', author='George Orwell',
def add(book_index): book = books[int(book_index)] db.session.add(Book(user_id=current_user.id, **book)) db.session.commit() return redirect(url_for("dashboard"))
def dashboard(): # Create the page with the contents of the database. books = Book.get_books(user_id=current_user.id) return render_template("dashboard.html", items=books)
def get(self): args = listV.parse_args() result = Book.getAll(args) return result['data'], result['code']
def patch(self, bookId): args = updateV.parse_args() result = Book.updateById(bookId, args) return result['data'], result['code']
def get(self, bookId): result = Book.getById(bookId) return result['data'], result['code']
def post(self): args = createV.parse_args() result = Book.create(args) return result['data'], result['code']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True #whooshalachemy才知道 app.config['DEBUG'] = True app.config['WHOOSH_BASE'] = 'whoosh' db = SQLAlchemy(app) db.create_all() wa.whoosh_index(app, Book) # Book for Alexis Angel author1 = Author(name="Alexis Angel") db.session.add(author1) db.session.commit() book1 = Book(name="Java Usage", description="how to use java", price="9.99", author=author1) db.session.add(book1) db.session.commit() book2 = Book(name="C++ Usage", description="how to use c++", price="5.50", author=author1) db.session.add(book2) db.session.commit() book3 = Book(name="Chinese Collection", description="chinese poetry collection from Ming dynasty", price="3.99", author=author1)
from datetime import datetime if __name__ == '__main__': Session.query(AuthorBookLink).delete() Session.query(Book).delete() Session.query(Author).delete() with open('books.json') as f: data = json.load(f) book_index = 1 index = 1 authors = {} for line in data: exists = Session.query(Author).filter_by(name=line['author']).first() if not exists: authors[line['author']] = index Session.add( Author(id=index, name=line['author'], created_at=datetime.now())) index += 1 Session.add( Book(id=book_index, title=line['title'], created_at=datetime.now())) Session.commit() Session.add( AuthorBookLink(author_id=authors[line['author']], book_id=book_index)) Session.commit() book_index += 1
def delete(self, bookId): result = Book.delete(bookId) return result['data'], result['code']
category6 = Category(name="Nature", user=user2) db_session.add(category6) db_session.commit() category7 = Category(name="Business", user=user1) db_session.add(category7) db_session.commit() # Add Autobiography books; remove user if dev 1 bookItem1 = Book(name="Steve Jobs: A Biography", description="Driven by demons, Jobs could drive those around " "him to fury and despair. But his personality and products " "were interrelated, just as Apples hardware and software " "tended to be, as if part of an integrated system. His tale " "is instructive and cautionary, filled with lessons about " "innovation, character, leadership, and values.", author="Walter Isaacson", price="9.99", category=category1, user=user1) db_session.add(bookItem1) db_session.commit() # Add web dev books; remove user if dev 1 bookItem2 = Book(name="Automate the Boring Stuff", description="Practical programming for total beginners. In " "Automate the Boring Stuff with Python, you'll learn how to " "use Python to write programs that do in minutes what would " "take you hours to do by hand-no prior programming " "experience required.",