def process_edit(isbn): # step 1. retrieve all books in the .csv file in a list all_books = read_books() # step 2. find the book that we have changed changed_book = find_book_generic(isbn) print(changed_book) # step 3. update the changed book to match the form changed_book['title'] = request.form.get('book_title') changed_book['author'] = request.form.get('author') changed_book['year_published'] = request.form.get('year-published') # step 4. overwrite the book information in the list for index in range(0, len(all_books)): if all_books[index]['isbn'] == changed_book['isbn']: all_books[index] = changed_book # step 5. write the entire list back to the csv file with open('books.csv', 'w', newline="\n") as fp: writer = csv.writer(fp, delimiter=",") # write in the header writer.writerow(['isbn', 'title', 'author', 'year_published']) for b in all_books: writer.writerow( [b['isbn'], b['title'], b['author'], b['year_published']]) return redirect(url_for('read_book'))
def test_save_book(): library = data.modify_book('9780316038379', 'Twilight 2', 'Meyer Stephenie 2', '1/1/2020') data.save(library) # save to file books = data.read_books() assert books[3]['title'] == 'Twilight 2' assert books[3]['author'] == 'Meyer Stephenie 2' assert books[3]['year_published'] == '1/1/2020'
def process_delete(isbn, title): #step 1. put all books from csv into a list all_books = read_books() #step 2. find the book to be deleted book_to_delete = find_book_generic(isbn) #step 3. find the item to be deleted in the all_books list & delete it for index in range(len(all_books)): if book_to_delete['isbn'] == all_books[index]['isbn']: del all_books[index] break #4. write back the list to the file write_to_file(all_books) return redirect(url_for('read_book'))
def test_read_books_from_file(): library = data.read_books() assert len(library) == 5 assert library[0]['title'] == 'The Lord of the Rings' assert library[2]['title'] == 'Children of Dune' assert library[-1]['title'] == 'Charlie and the Chocolate Factory'
def select_book_to_edit(): # Get all books into a list all_books = read_books() return render_template('books/select_book.template.html', books=all_books)
def select_to_delete(): all_books = read_books() return render_template('/books/show_delete.template.html', books=all_books)
def read_book(): all_books = read_books() return render_template('index.template.html', books=all_books)
def index(): library = data.read_books() return render_template('index.template.html', library=library)