def add_book(): user_id = user.get_id() app_user = AppUser.query.get(stormpathUserHash(user_id)) if app_user.is_author: if request.method == 'GET': return render_template('add_book.html', author=app_user.author) book_file = request.files.get('epub_file', None) # POST is a epub file upload if book_file.content_type == 'application/epub+zip' or book_file.content_type == 'application/octet-stream': book_upload = BookUploader(book_file.filename, book_file) book_location = book_upload.file_dir[:-1] # fetch genres too! book_data = { 'author': app_user.author, 'title': request.form.get('title'), 'publisher': request.form.get('publisher'), 'epub_url': book_location } book = Book.book_from_dict(**book_data) db.session.add(book) db.session.commit() print book return render_template('add_book.html', author=app_user.author) return redirect(url_for('index'))
def bootstrapTestDB(db): """ Takes an created SQLAlchemy db and bootstraps the tables with dummy data """ books_copy, authors_copy = deepcopy(books), deepcopy(authors) # load authors for author_data in authors: db.session.add(Author.author_from_dict(**author_data)) db.session.commit() # load genres for book_data in books_copy: for genre in book_data['genres']: g = Genre.query.filter_by(name=genre).first() if not g: db.session.add(Genre(genre)) db.session.flush() db.session.commit() # load books for book_data in books_copy: book_data['genres'] = [ Genre.query.filter_by(name=genre_item).first() for genre_item in book_data['genres'] ] book_data['author'] = Author.query.filter_by(name=book_data['author']).first() db.session.add(Book.book_from_dict(**book_data)) # commit the changes db.session.commit() #load users for author_data in authors_copy: author = Author.query.filter_by(name=author_data['name']).first() db.session.add(AppUser(author_data['user_href'], author)) db.session.commit()
def add_book(): user_id = user.get_id() app_user = AppUser.query.get(stormpathUserHash(user_id)) if app_user.is_author: if request.method == 'GET': return render_template('add_book.html', author=app_user.author) book_file = request.files.get('epub_file', None) cover_file = request.files.get('cover_file', None) # POST is a epub file upload if book_file.content_type == 'application/epub+zip' or book_file.content_type == 'application/octet-stream': book_upload = BookUploader(book_file.filename, book_file, cover_file) epub_url = S3URL + 'epubs/' + book_upload.epub_key cover_url = CLOUDFRONTURL + book_upload.cover_key genres = [] for g in request.form.get('genres').split(','): genre_name = g.strip().title() if not genre_name.isspace(): genre = Genre.query.filter_by(name=genre_name).first() if not genre: genre = Genre(genre_name) db.session.add(genre) genres.append(genre) book_data = { 'author': app_user.author, 'isbn': request.form.get('isbn'), 'title': request.form.get('title'), 'publisher': request.form.get('publisher'), 'description': request.form.get('description'), 'genres': genres, 'epub_url': epub_url, 'cover_large': cover_url } book = Book.book_from_dict(**book_data) db.session.add(book) db.session.commit() return redirect(url_for('author_routes.author_dashboard')) return redirect(url_for('index'))
def bootstrapTestDB(db): """ Takes an created SQLAlchemy db and bootstraps the tables with dummy data """ books_copy, authors_copy = deepcopy(books), deepcopy(authors) # load authors for author_data in authors: db.session.add(Author.author_from_dict(**author_data)) db.session.commit() # load genres for book_data in books_copy: for genre in book_data['genres']: g = Genre.query.filter_by(name=genre).first() if not g: db.session.add(Genre(genre)) db.session.flush() db.session.commit() # load books for book_data in books_copy: book_data['genres'] = [ Genre.query.filter_by(name=genre_item).first() for genre_item in book_data['genres'] ] book_data['author'] = Author.query.filter_by( name=book_data['author']).first() db.session.add(Book.book_from_dict(**book_data)) # commit the changes db.session.commit() #load users for author_data in authors_copy: author = Author.query.filter_by(name=author_data['name']).first() db.session.add( AppUser(stormpathUserHash(author_data['user_href']), author_data['user_href'], author)) db.session.commit()