def addNewAuthor(genreId): if 'username' not in login_session: return redirect('/login') if request.method == 'POST': nAutId = session.query(func.max(Authors.id)).scalar() nAutId += 1 newAuthor = Authors(id=nAutId, name=request.form['name'], user_id=login_session['user_id']) session.add(newAuthor) session.commit() return redirect(url_for('addNewBook', genreId=genreId)) else: return render_template('newAuthor.html', genreId=genreId)
def authorCheck(author_name): '''Checks if an author already exists in the database If the author does not exist, the author is added''' DBSession = sessionmaker(bind=engine) session = DBSession() author_id = None authors = session.query(Authors).all() for a in authors: if a.name == request.form['author']: author_id = a.id break if author_id: return author_id else: newAuthor = Authors(name=author_name, book_count=1, owner_id=login_session['username']) session.add(newAuthor) session.flush() author_id = newAuthor.id session.commit() return author_id
def add_author(first_name, last_name): new_author = Authors(first_name=first_name, last_name=last_name) session.add(new_author) session.commit() return session.execute(f"SELECT MAX(id) FROM authors;")
def new_book(): # Check if user is logged in if 'username' not in login_session: return render_template('public.html', genres=genres) genres = session.execute(q_genres) genres = sorted([i[0] for i in genres]) if request.method == 'POST': # EXAMPLE row: # 1 War and Peace 1 HIF 1225 The novel chronicles the history of the French invasion of Russia and the impact of the Napoleonic era on Tsarist society through the stories of five Russian aristocratic families. 2019-01-01 user_id = getUserID(login_session['email']) existing_authors = session.execute( q_get_existing_authors.format(user_id)) existing_authors = [i[0] for i in list(existing_authors)] first_name = request.form['author_first_name'].strip() last_name = request.form['author_last_name'].strip() full_name = first_name + ' ' + last_name # If author is not in 'authors', add new entry to 'authors' table if full_name not in existing_authors: new_author = Authors(last_name=request.form['author_last_name'], first_name=request.form['author_first_name']) session.add(new_author) session.commit() author_id = session.execute( f"SELECT id FROM authors WHERE first_name='{first_name}' AND last_name='{last_name}';" ) author_id = list(author_id)[0][0] genre_id = session.execute( f"SELECT id FROM genres WHERE genre='{request.form['genre']}';") genre_id = list(genre_id)[0][0] title = request.form['title'] pages = request.form['pages'] synopsis = request.form['synopsis'] date_finished = request.form['date_finished'] new_item = Books(title=title, author_id=author_id, genre_id=genre_id, pages=pages, synopsis=synopsis, date_finished=date_finished, user_id=user_id) try: session.add(new_item) session.commit() new_book_id = session.execute(f"SELECT MAX(id) FROM books;") new_book_id = list(new_book_id)[0][0] user_id = getUserID(login_session['email']) print('USERID', user_id) info = session.execute(q_book_info.format(new_book_id, user_id)) print(info) info = [i for i in list(info)[0]] print(info) flash(f'New book added: {info[0]}') return render_template('book.html', info=info, book_id=new_book_id) except exc.IntegrityError as e: session.rollback() flash( f'Error! You already have this book-author pairing: {title} by {first_name} {last_name}.' ) return redirect(url_for('new_book')) else: return render_template('newBook.html', genres=genres)
def new_book(): # Check if user is logged in if 'username' not in login_session: return render_template('public.html', genres=genres) genres = session.execute(q_genres) genres = sorted([i[0] for i in genres]) if request.method == 'POST': user_id = getUserID(login_session['email']) existing_authors = \ session.execute(q_get_existing_authors.format(user_id)) existing_authors = [i[0] for i in list(existing_authors)] first_name = request.form['author_first_name'].strip() last_name = request.form['author_last_name'].strip() full_name = first_name + ' ' + last_name print('FULLNAME', full_name) # If author is not in 'authors', add new entry to 'authors' table if full_name not in existing_authors: new_author = Authors(last_name=request.form['author_last_name'], first_name=request.form['author_first_name'], user_id=user_id) session.add(new_author) session.commit() author_id = session.execute("SELECT id \ FROM authors \ WHERE first_name='{}' \ AND last_name='{}';".format(first_name, last_name)) author_id = list(author_id) print('AUTHOR_ID', author_id) author_id = author_id[0][0] genre_id = session.execute("SELECT id \ FROM genres \ WHERE genre='{}';".format(request.form['genre'])) genre_id = list(genre_id)[0][0] genre = request.form['genre'] title = request.form['title'] pages = request.form['pages'] synopsis = request.form['synopsis'] date_finished = request.form['date_finished'] new_item = Books(title=title, author_id=author_id, genre_id=genre_id, pages=pages, synopsis=synopsis, date_finished=date_finished, user_id=user_id) try: session.add(new_item) session.commit() new_book_id = session.execute("SELECT MAX(id) FROM books;") new_book_id = list(new_book_id)[0][0] user_id = getUserID(login_session['email']) info = session.execute(q_book_info.format(new_book_id, user_id)) info = [i for i in list(info)[0]] flash('New book added: {}'.format(info[0])) return render_template('book.html', info=info, book_id=new_book_id, genre=genre) except exc.IntegrityError as e: session.rollback() flash('Error! You already have this book-author pairing: \ {title} by {} {}.'.format(first_name, last_name)) return redirect(url_for('new_book')) else: return render_template('newBook.html', genres=genres)
patron1 = Patrons(name='Duncan Kraus', email='*****@*****.**') session.add(patron1) session.commit() patron2 = Patrons(name='Beatrice Beran', email='*****@*****.**') session.add(patron2) session.commit() patron3 = Patrons(name='Lex Killough', email='*****@*****.**') session.add(patron3) session.commit() print "patrons added" author1 = Authors(name='J.K. Rowling', book_count=7, owner_id='null') session.add(author1) session.commit() author2 = Authors(name='Becky Albertalli', book_count=2, owner_id='null') session.add(author2) session.commit() author3 = Authors(name='Gillian Flynn', book_count=1, owner_id='null') session.add(author3) session.commit() author4 = Authors(name='Toni Morrison', book_count=3, owner_id='null') session.add(author4) session.commit()
from database_setup import Authors, Base, Novels, Users import datetime engine = create_engine('sqlite:///authorlibrarywithusersandtime.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Menu for UrbanBurger user = Users(name="abhi1111", picture="aapota1111", email="*****@*****.**") session.add(user) session.commit() restaurant1 = Authors(name="Stephen King", user=user) session.add(restaurant1) session.commit() novels = Novels(name="Novel", year="2001", description="aaaaaaabbbbnnnnfnf", author=restaurant1, user=user) session.add(novels) session.commit() novels = Novels(name="Novel", year="2001", description="aaaaaaabbbbnnnnfnf", author=restaurant1,
# 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() # First user user1 = LibraryUsers(id=1, name="Tim Brown", email="*****@*****.**", picture="") session.add(user1) session.commit() # First author author1 = Authors(id=1, name="Anne McCaffrey", user_id=1) session.add(author1) session.commit() # First Genres genre1 = Genre(id=1, genre="SciFi", user_id=1) genre2 = Genre(id=2, genre="Fantasy", user_id=1) session.add(genre1) session.commit() session.add(genre2) session.commit()