def newBook(bookstore_id): """ Description: Create a new book Parameters: bookstore id Return: html page """ session = db.open_session() bookstore = session.query(Bookstore).filter_by( id=bookstore_id).one_or_none() if bookstore.user_id != login_session['user_id']: return render_template('auth/notOwner.html') if request.method == 'POST': newBook = Book(title=request.form['title'], author=request.form['author'], description=request.form['description'], price=request.form['price'], genre=request.form['genre'], bookstore_id=bookstore_id, user_id=bookstore.user_id) session.add(newBook) session.commit() books = session.query(Book).all() file.updateJSON('project/models/json/database/books.json', books) flash("New book %s successfully created" % newBook.title) db.close_session(session) return redirect( url_for('book.bookstoreCatalog', bookstore_id=bookstore_id)) else: db.close_session(session) return render_template('book/newBook.html', bookstore_id=bookstore_id)
def editBookDetails(bookstore_id, book_id): """ Description: Edit a book detail Parameters: bookstore id, book id Return: html page """ session = db.open_session() editedBook = session.query(Book).filter_by(id=book_id).one_or_none() if editedBook.user_id != login_session['user_id']: return render_template('auth/notOwner.html') if (request.method == 'POST'): if (request.form['title']): editedBook.title = request.form['title'] if (request.form['author']): editedBook.author = request.form['author'] if (request.form['description']): editedBook.description = request.form['description'] if (request.form['genre']): editedBook.genre = request.form['genre'] if (request.form['price']): editedBook.price = request.form['price'] session.add(editedBook) session.commit() books = session.query(Book).all() file.updateJSON('project/models/json/database/books.json', books) flash("Book successfully edited") db.close_session(session) return redirect( url_for('book.bookstoreCatalog', bookstore_id=bookstore_id)) else: db.close_session(session) return render_template('book/editBookDetails.html', book=editedBook, bookstore_id=bookstore_id, book_id=book_id)
def deleteBook(bookstore_id, book_id): """ Description: Delete a book Parameters: bookstore id, book id Return: html page """ session = db.open_session() deletedBook = session.query(Book).filter_by(id=book_id).one_or_none() if deletedBook.user_id != login_session['user_id']: return render_template('auth/notOwner.html') if (request.method == 'POST'): session.delete(deletedBook) session.commit() books = session.query(Book).all() file.updateJSON('project/models/json/database/books.json', books) flash("Book %s successfully deleted" % deletedBook.title) db.close_session(session) return redirect( url_for('book.bookstoreCatalog', bookstore_id=bookstore_id)) else: db.close_session(session) return render_template('book/deleteBook.html', book=deletedBook, bookstore_id=bookstore_id, book_id=book_id)
def editBookstore(bookstore_id): """ Description: Edit a bookstore Parameters: bookstore id Return: html page """ session = db.open_session() editedBookstore = session.query(Bookstore).filter_by( id=bookstore_id).one_or_none() if editedBookstore.user_id != login_session['user_id']: return render_template('auth/notOwner.html') if (request.method == 'POST'): if (request.form['name']): editedBookstore.name = request.form['name'] session.add(editedBookstore) session.commit() bookstores = session.query(Bookstore).all() file.updateJSON('project/models/json/database/bookstores.json', bookstores) flash("Bookstore successfully edited") db.close_session(session) return redirect(url_for('bookstore.showBookstores')) else: db.close_session(session) return render_template('bookstore/editBookstore.html', bookstore=editedBookstore, bookstore_id=bookstore_id)
def showBookstores(): """ Description: Show all bookstores Parameters: Nothing Return: html page """ session = db.open_session() bookstores = session.query(Bookstore).order_by(asc(Bookstore.name)) db.close_session(session) if 'username' not in login_session: return render_template('bookstore/publicBookstores.html', bookstores=bookstores) else: return render_template('bookstore/bookstores.html', bookstores=bookstores)
def bookstoreCatalog(bookstore_id): """ Description: Show all books from a bookstore Parameters: bookstore_id Return: html page """ session = db.open_session() bookstore = session.query(Bookstore).filter_by( id=bookstore_id).one_or_none() creator = dao.getUserInfo(db, bookstore.user_id) books = session.query(Book).filter_by(bookstore_id=bookstore.id).all() db.close_session(session) if 'username' not in login_session or creator.id != login_session[ 'user_id']: return render_template('book/publicBookDetails.html', bookstore=bookstore, books=books) else: return render_template('book/bookDetails.html', bookstore=bookstore, books=books)
def newBookstore(): """ Description: Create a new bookstore Parameters: Nothing Return: html page """ if (request.method == 'POST'): session = db.open_session() bookstores = session.query(Bookstore).all() newBookstore = Bookstore(name=request.form['name'], user_id=login_session['user_id']) session.add(newBookstore) session.commit() bookstores = session.query(Bookstore).all() file.updateJSON('project/models/json/database/bookstores.json', bookstores) flash("New bookstore %s successfully created" % newBookstore.name) db.close_session(session) return redirect(url_for('bookstore.showBookstores')) else: return render_template('bookstore/newBookstore.html')