def store_books_in_database(): a = get_books("cooking") id_record= [] for b in a: id=b[0] title = b[1] crud.create_book(id ,title,"None")
def test_create_book(self): """Tests creation of user in database""" b = crud.create_book(title='Tipping the Velvet', author='Sarah Waters', genre='fiction, historical fiction, LGBT', description=None, image_url='https://res.cloudinary.com/rosieslibrary/image/upload/v1609811721/Books/tipping_the_velvet_kmsojp.jpg', owner=1, available=True) self.assertIsNotNone(b.book_id)
model.connect_to_db(server.app, "testbookworm") model.db.create_all() #create fake books for testing new_books = [] for n in range(10): isbn = f"testing12345{n}" title = f"Test Title Book {n}" author = f"Test Author {n}" description = f"Test overview for book {n}" page_length = n image = f"Test image path {n}" new_book = crud.create_book(isbn, title, author, description, page_length, image) new_books.append(new_book) #create fake users for testing new_users = [] for n in range(6): first_name = f"Test User First {n}" last_name = f"Test User Last {n}" email = f"user{n}@test.com" password = "******" new_user = crud.create_user(first_name, last_name, email, password) new_users.append(new_user)
def get_and_update_categories(): """Gets or updates a user's categories""" if session.get("user_id"): user_id = session["user_id"] if request.method == "GET": categories = [] category_objects = crud.get_all_user_categories(user_id) for category_object in category_objects: dict_category = category_object.to_dict() categories.append(dict_category) return jsonify({"categories": categories}) elif request.method == "POST": if request.json.get("label"): label = request.json.get("label") user = crud.get_user_by_id(user_id) if crud.get_category_by_label(user_id, label): return ({ "error": f"{label} is already in {user.first_name}'s bookshelf!" }) new_category = crud.create_category(user_id, label) return jsonify({ "success": f"{new_category.label} has been added to {user.first_name}'s bookshelf!" }) else: old_label = request.json.get("old_label") new_label = request.json.get("new_label") crud.update_category_label(user_id, old_label, new_label) return jsonify({ "success": f"{old_label} has been changed to {new_label}!", "label": new_label }) elif request.method == "PUT": label = request.json.get("label") book_dict = request.json.get("book") isbn = book_dict["id"] book = crud.get_book_by_isbn(isbn) category = crud.get_category_by_label(user_id, label) if not book: authors = "" for author in book_dict["volumeInfo"]["authors"]: authors += f"{author} " page_count = book_dict["volumeInfo"].get("pageCount") if not page_count: page_count = 000 book = crud.create_book( isbn, book_dict["volumeInfo"]["title"], authors, book_dict["volumeInfo"]["description"], page_count, book_dict["volumeInfo"]["imageLinks"]["thumbnail"]) if not category: category = crud.create_category(user_id, label) added_books = crud.create_book_category(book, category) return jsonify({ "success": f"""A new category, {category.label}, has been added to your bookshelf and {book.title} has been added to it""" }) if book in crud.get_all_books_in_category(user_id, label): return jsonify({ "error": f"{book.title} is already in your {category.label} books" }) added_books = crud.create_book_category(book, category) return jsonify({ "success": f"{book.title} has been added to {category.label} books" }) elif request.method == "DELETE": if request.json.get("label"): label = request.json.get("label") crud.delete_category(label, user_id) return jsonify({ "success": f"{label} has successfully been removed from your bookshelf.", "label": "" }) else: label = request.json.get("category") isbn = request.json.get("isbn") title = request.json.get("title") this_category = crud.get_category_by_label(user_id, label) crud.remove_book_from_category(isbn, this_category.id) return jsonify({ "success": f"{title} has successfully been removed from {label}.", }) else: return jsonify({"error": "User must be logged in to view this page."})
while i < len(results['items']): book_info = results['items'][i] try: google_id, cover_img, title = ( book_info['id'], book_info['volumeInfo']['imageLinks']['thumbnail'], book_info['volumeInfo']['title']) except: i += 1 continue if crud.Book.query.get(google_id) == None: try: book = crud.create_book(google_id, title, cover_img) except: book = crud.create_book(google_id, title) else: book = crud.Book.query.get(google_id) database_books.append(book) i += 1 counter += 1 names = [ 'lizzie', 'jane', 'lydia', 'kitty', 'mary', 'fitzwilliam', 'charles', 'gigi', 'bennet', 'caroline' ] user_email = f'{names[0]}@test.com'
def add_book_to_db(new_book_info, user, shelfname): cover_img_source = 'static/images/generic-book-cover.jpg' # FIND BOOKSHELF if crud.return_user_bookshelf_by_name(user.user_id, shelfname): #add a shelvedbook shelf = crud.return_user_bookshelf_by_name(user.user_id, shelfname) # CREATE SHELF IF NECESSARY else: shelf = crud.create_user_bookshelf(user.user_id, shelfname) # FIND BOOK IN DATABASE if crud.get_book_by_name(new_book_info['title']): book = crud.get_book_by_name(new_book_info['title']) print("BOOK IN DB ADDING TO SHELF IF APPROPRIATE") # TODO be able to handle books with mutliple authors else: # for key in ['title', 'publisher', 'year_published', 'isbn', 'description', 'cover_img_source'] title = new_book_info['title'][:49] if 'author' in new_book_info: author = new_book_info['author'][0][:29] else: author = "unknown" if 'publisher' in new_book_info: publisher = new_book_info['publisher'][:29] else: publisher = "unknown" if 'year_published' in new_book_info: year_published = new_book_info['year_published'] else: year_published = '0000' if 'isbn' in new_book_info: isbn = new_book_info['isbn'][:29] else: isbn = None if 'description' in new_book_info: description = new_book_info['description'] else: description = 'unknown' if 'cover_img_source' in new_book_info: cover_img_source = new_book_info['cover_img_source'] #MAKE A NEW BOOK ADD TO DB book = crud.create_book(title, author, publisher, year_published, isbn, description, cover_img_source) # MAKE A SHELVED BOOK shelved = crud.create_shelvedbook(shelf.shelf_id, book.book_id, 5, 4) print("shelvedbook, try to refresh shelf", shelf, shelved) return ({ 'book_id': book.book_id, "title": book.title, 'author': book.author, 'publisher': book.publisher, 'description': book.description, 'img': cover_img_source })
def test_data(): #create books for testing harry_potter = crud.create_book( "oxxszQEACAAJ", "Harry Potter and the Half-Blood Prince", "J. K. Rowling", "Harry Potter, now sixteen-years-old, begins his sixth year at school in the midst of the battle between good and evil which has heated up with the return of the Dark Lord Voldemort.", 652, "http://books.google.com/books/content?id=oxxszQEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api" ) neverwhere = crud.create_book( "yuCUZ3km3qIC", "Neverwhere", "Neil Gaiman", "Richard Mayhew is a young man with a good heart and an ordinarylife, which is changed forever when he stops to help a girl he finds bleeding on a London sidewalk. His small act of kindness propels him into a world he never dreamed existed. There are people who fall through the cracks, and Richard has become one of them. And he must learn to survive in this city of shadows and darkness, monsters and saints, murderers and angels, if he is ever to return to the London that he knew.", 400, "http://books.google.com/books/content?id=yuCUZ3km3qIC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) bees = crud.create_book( "FiIXot_e10sC", "The Secret Life of Bees", "Sue Monk Kidd", "After her \"stand-in mother,\" a bold black woman named Rosaleen, insults the three biggest racists in town, Lily Owens joins Rosaleen on a journey to Tiburon, South Carolina, where they are taken in by three black, bee-keeping sisters.", 317, "http://books.google.com/books/content?id=FiIXot_e10sC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) hidden = crud.create_book( "bxahDwAAQBAJ", "Hidden Valley Road", "Robert Kolker", "OPRAH’S BOOK CLUB PICK #1 NEW YORK TIMES BESTSELLER ONE OF THE NEW YORK TIMES TOP TEN BOOKS OF THE YEAR ONE OF THE WALL STREET JOURNAL TOP TEN BOOKS OF THE YEAR PEOPLE'S #1 BEST BOOK OF THE YEAR Named a BEST BOOK OF THE YEAR by The New York Times, The Washington Post, NPR, TIME, Slate, Smithsonian, The New York Post, and Amazon The heartrending story of a midcentury American family with twelve children, six of them diagnosed with schizophrenia, that became science's great hope in the quest to understand the disease. Don and Mimi Galvin seemed to be living the American dream. After World War II, Don's work with the Air Force brought them to Colorado, where their twelve children perfectly spanned the baby boom: the oldest born in 1945, the youngest in 1965. In those years, there was an established script for a family like the Galvins--aspiration, hard work, upward mobility, domestic harmony--and they worked hard to play their parts. But behind the scenes was a different story: psychological breakdown, sudden shocking violence, hidden abuse. By the mid-1970s, six of the ten Galvin boys, one after another, were diagnosed as schizophrenic. How could all this happen to one family? What took place inside the house on Hidden Valley Road was so extraordinary that the Galvins became one of the first families to be studied by the National Institute of Mental Health. Their story offers a shadow history of the science of schizophrenia, from the era of institutionalization, lobotomy, and the schizophrenogenic mother to the search for genetic markers for the disease, always amid profound disagreements about the nature of the illness itself. And unbeknownst to the Galvins, samples of their DNA informed decades of genetic research that continues today, offering paths to treatment, prediction, and even eradication of the disease for future generations. With clarity and compassion, bestselling and award-winning author Robert Kolker uncovers one family's unforgettable legacy of suffering, love, and hope.", 400, "http://books.google.com/books/content?id=bxahDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) crawdads = crud.create_book( "CGVDDwAAQBAJ", "Where the Crawdads Sing", "Delia Owens", "#1 New York Times Bestseller A Reese Witherspoon x Hello Sunshine Book Club Pick \"I can't even express how much I love this book! I didn't want this story to end!\"--Reese Witherspoon \"Painfully beautiful.\"--The New York Times Book Review \"Perfect for fans of Barbara Kingsolver.\"--Bustle For years, rumors of the \"Marsh Girl\" have haunted Barkley Cove, a quiet town on the North Carolina coast. So in late 1969, when handsome Chase Andrews is found dead, the locals immediately suspect Kya Clark, the so-called Marsh Girl. But Kya is not what they say. Sensitive and intelligent, she has survived for years alone in the marsh that she calls home, finding friends in the gulls and lessons in the sand. Then the time comes when she yearns to be touched and loved. When two young men from town become intrigued by her wild beauty, Kya opens herself to a new life--until the unthinkable happens. Perfect for fans of Barbara Kingsolver and Karen Russell, Where the Crawdads Sing is at once an exquisite ode to the natural world, a heartbreaking coming-of-age story, and a surprising tale of possible murder. Owens reminds us that we are forever shaped by the children we once were, and that we are all subject to the beautiful and violent secrets that nature keeps.", 384, "http://books.google.com/books/content?id=CGVDDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) wind = crud.create_book( "BcG2dVRXKukC", "The Name of the Wind", "Patrick Rothfuss", "'This is a magnificent book' Anne McCaffrey 'I was reminded of Ursula K. Le Guin, George R. R. Martin, and J. R. R. Tolkein, but never felt that Rothfuss was imitating anyone' THE TIMES 'I have stolen princesses back from sleeping barrow kings. I burned down the town of Trebon. I have spent the night with Felurian and left with both my sanity and my life. I was expelled from the University at a younger age than most people are allowed in. I tread paths by moonlight that others fear to speak of during day. I have talked to Gods, loved women, and written songs that make the minstrels weep. My name is Kvothe. You may have heard of me' So begins the tale of Kvothe - currently known as Kote, the unassuming innkeepter - from his childhood in a troupe of traveling players, through his years spent as a near-feral orphan in a crime-riddled city, to his daringly brazen yet successful bid to enter a difficult and dangerous school of magic. In these pages you will come to know Kvothe the notorious magician, the accomplished thief, the masterful musician, the dragon-slayer, the legend-hunter, the lover, the thief and the infamous assassin.", 672, "http://books.google.com/books/content?id=BcG2dVRXKukC&printsec=frontcover&img=1&zoom=1&source=gbs_api" ) addie = crud.create_book( "vH3LDwAAQBAJ", "The Invisible Life of Addie LaRue", "V. E. Schwab", "AN INSTANT NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER NATIONAL INDIE BESTSELLER THE WASHINGTON POST BESTSELLER #1 Indie Next Pick and #1 LibraryReads Pick - October 2020 Recommended by Entertainment Weekly, Real Simple, NPR, Slate, and Oprah Magazine A “Best Of” Book From: CNN *Amazon Editors * Goodreads * Bustle * PopSugar * BuzzFeed * Barnes & Noble * Kirkus Reviews * Lambda Literary * Nerdette * The Nerd Daily * Polygon * Library Reads * io9 * Smart Bitches Trashy Books * LiteraryHub * Medium * BookBub * The Mary Sue * Chicago Tribune * NY Daily News * SyFy Wire * Powells.com * Bookish * Book Riot * In the vein of The Time Traveler’s Wife and Life After Life, The Invisible Life of Addie LaRue is New York Times bestselling author V. E. Schwab’s genre-defying tour de force. A Life No One Will Remember. A Story You Will Never Forget. France, 1714: in a moment of desperation, a young woman makes a Faustian bargain to live forever—and is cursed to be forgotten by everyone she meets. Thus begins the extraordinary life of Addie LaRue, and a dazzling adventure that will play out across centuries and continents, across history and art, as a young woman learns how far she will go to leave her mark on the world. But everything changes when, after nearly 300 years, Addie stumbles across a young man in a hidden bookstore and he remembers her name. At the Publisher's request, this title is being sold without Digital Rights Management Software (DRM) applied.", 480, "http://books.google.com/books/content?id=vH3LDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) mistborn = crud.create_book( "t_ZYYXZq4RgC", "Mistborn", "Brandon Sanderson", "From #1 New York Times bestselling author Brandon Sanderson, the Mistborn series is a heist story of political intrigue and magical, martial-arts action. For a thousand years the ash fell and no flowers bloomed. For a thousand years the Skaa slaved in misery and lived in fear. For a thousand years the Lord Ruler, the \"Sliver of Infinity,\" reigned with absolute power and ultimate terror, divinely invincible. Then, when hope was so long lost that not even its memory remained, a terribly scarred, heart-broken half-Skaa rediscovered it in the depths of the Lord Ruler's most hellish prison. Kelsier \"snapped\" and found in himself the powers of a Mistborn. A brilliant thief and natural leader, he turned his talents to the ultimate caper, with the Lord Ruler himself as the mark. Kelsier recruited the underworld's elite, the smartest and most trustworthy allomancers, each of whom shares one of his many powers, and all of whom relish a high-stakes challenge. Only then does he reveal his ultimate dream, not just the greatest heist in history, but the downfall of the divine despot. But even with the best criminal crew ever assembled, Kel's plan looks more like the ultimate long shot, until luck brings a ragged girl named Vin into his life. Like him, she's a half-Skaa orphan, but she's lived a much harsher life. Vin has learned to expect betrayal from everyone she meets, and gotten it. She will have to learn to trust, if Kel is to help her master powers of which she never dreamed. This saga dares to ask a simple question: What if the hero of prophecy fails? Other Tor books by Brandon Sanderson The Cosmere The Stormlight Archive The Way of Kings Words of Radiance Edgedancer (Novella) Oathbringer The Mistborn trilogy Mistborn: The Final Empire The Well of Ascension The Hero of Ages Mistborn: The Wax and Wayne series Alloy of Law Shadows of Self Bands of Mourning Collection Arcanum Unbounded Other Cosmere novels Elantris Warbreaker The Alcatraz vs. the Evil Librarians series Alcatraz vs. the Evil Librarians The Scrivener's Bones The Knights of Crystallia The Shattered Lens The Dark Talent The Rithmatist series The Rithmatist Other books by Brandon Sanderson The Reckoners Steelheart Firefight Calamity At the Publisher's request, this title is being sold without Digital Rights Management Software (DRM) applied.", 544, "http://books.google.com/books/content?id=t_ZYYXZq4RgC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) kings = crud.create_book( "QVn-CgAAQBAJ", "The Way of Kings", "Brandon Sanderson", "Introduces the world of Roshar through the experiences of a war-weary royal compelled by visions, a highborn youth condemned to military slavery, and a woman who is desperate to save her impoverished house.", 1007, "http://books.google.com/books/content?id=QVn-CgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) evelyn = crud.create_book( "PxNcDwAAQBAJ", "The 7 1⁄2 Deaths of Evelyn Hardcastle", "Stuart Turton", "\"Agatha Christie meets Groundhog Day...quite unlike anything I've ever read, and altogether triumphant.\"—A. J. Finn, #1 New York Times-bestselling author of The Woman in the Window The Rules of Blackheath Evelyn Hardcastle will be murdered at 11:00 p.m. There are eight days, and eight witnesses for you to inhabit. We will only let you escape once you tell us the name of the killer. Understood? Then let's begin... *** Evelyn Hardcastle will die. Every day until Aiden Bishop can identify her killer and break the cycle. But every time the day begins again, Aiden wakes up in the body of a different guest. And some of his hosts are more helpful than others. For fans of Claire North and Kate Atkinson, The 71⁄2 Deaths of Evelyn Hardcastle is a breathlessly addictive novel that follows one man's race against time to find a killer—but an astonishing time-turning twist means that nothing and no one are quite what they seem. Praise for The 7 1⁄2 Deaths of Evelyn Hardcastle: Costa First Novel Award 2018 Winner One of Stylist Magazine's 20 Must-Read Books of 2018 One of Harper's Bazaar's 10 Must-Read Books of 2018 One of Guardian's Best Books of 2018", 480, "http://books.google.com/books/content?id=PxNcDwAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" ) #create users for testing new_users = [] hunter = crud.create_user("Hunter", "Laine", "*****@*****.**", "test") haley = crud.create_user("Haley", "Laine", "*****@*****.**", "test") connor = crud.create_user("Connor", "Laine", "*****@*****.**", "test") steven = crud.create_user("Steven", "Laine", "*****@*****.**", "test") mary = crud.create_user("Mary", "Laine", "*****@*****.**", "test") jake = crud.create_user("Jake", "Laine", "*****@*****.**", "test") # Create fake events for testing event_1 = crud.create_event(1, "Pleasanton", "2021-03-26", "18:00", "20:00", "CA") event_2 = crud.create_event(2, "Berkeley", "2021-03-19", "19:00", "21:00", "CA") event_3 = crud.create_event(3, "San Francisco", "2021-04-09", "20:00", "22:00", "CA") event_4 = crud.create_event(4, "Remote", "2021-04-22", "18:00", "20:00") event_5 = crud.create_event(5, "Lafayette", "2021-04-14", "17:00", "19:00", "CA") event_6 = crud.create_event(6, "Oakland", "2021-05-05", "18:00", "20:00", "CA") # Create fake events_attendees for testing crud.create_event_attendee(hunter.id, event_1.id) crud.create_event_attendee(haley.id, event_1.id) crud.create_event_attendee(mary.id, event_2.id) crud.create_event_attendee(steven.id, event_2.id) crud.create_event_attendee(hunter.id, event_3.id) crud.create_event_attendee(connor.id, event_3.id) crud.create_event_attendee(jake.id, event_4.id) crud.create_event_attendee(haley.id, event_4.id) crud.create_event_attendee(hunter.id, event_5.id) crud.create_event_attendee(steven.id, event_5.id) crud.create_event_attendee(mary.id, event_6.id) crud.create_event_attendee(jake.id, event_6.id) crud.create_event_attendee(connor.id, event_3.id) crud.create_event_attendee(haley.id, event_2.id) crud.create_event_attendee(steven.id, event_1.id) # Create fake event_books for testing crud.create_event_book(event_1, evelyn) crud.create_event_book(event_2, harry_potter) crud.create_event_book(event_3, wind) crud.create_event_book(event_4, bees) crud.create_event_book(event_5, mistborn) crud.create_event_book(event_6, kings) crud.create_event_book(event_1, harry_potter) crud.create_event_book(event_2, mistborn) crud.create_event_book(event_3, crawdads) crud.create_event_book(event_4, neverwhere) crud.create_event_book(event_5, hidden) crud.create_event_book(event_6, evelyn) crud.create_event_book(event_1, addie) crud.create_event_book(event_2, wind) crud.create_event_book(event_3, harry_potter) crud.create_event_book(event_4, hidden) crud.create_event_book(event_5, crawdads) crud.create_event_book(event_6, addie) crud.create_event_book(event_1, bees) crud.create_event_book(event_2, evelyn) crud.create_event_book(event_3, addie) crud.create_event_book(event_4, crawdads) crud.create_event_book(event_5, neverwhere) crud.create_event_book(event_6, mistborn) # Create fake categories for testing hunter_cat_1 = crud.create_category(1, "Book Club") hunter_cat_2 = crud.create_category(1, "Want to Read") hunter_cat_3 = crud.create_category(1, "My Favorite Books") haley_cat_1 = crud.create_category(2, "Book Club") haley_cat_2 = crud.create_category(2, "Thrillers") haley_cat_3 = crud.create_category(2, "Beach Reads") connor_cat_1 = crud.create_category(3, "Non-Fiction") connor_cat_2 = crud.create_category(3, "Learn Something New") connor_cat_3 = crud.create_category(3, "My Favorite Books") steven_cat_1 = crud.create_category(4, "Want to Read") steven_cat_2 = crud.create_category(4, "Books to Recommend") steven_cat_3 = crud.create_category(4, "My Favorite Books") mary_cat_1 = crud.create_category(5, "Book Club") mary_cat_2 = crud.create_category(5, "Beach Reads") mary_cat_3 = crud.create_category(5, "Classics") jake_cat_1 = crud.create_category(6, "Historical Fiction") jake_cat_2 = crud.create_category(6, "Classics") jake_cat_3 = crud.create_category(6, "My Favorite Books") # Create fake book_categories for testing crud.create_book_category(hidden, hunter_cat_1) crud.create_book_category(addie, hunter_cat_1) crud.create_book_category(wind, hunter_cat_2) crud.create_book_category(kings, hunter_cat_2) crud.create_book_category(mistborn, hunter_cat_2) crud.create_book_category(wind, hunter_cat_3) crud.create_book_category(harry_potter, hunter_cat_3) crud.create_book_category(addie, haley_cat_1) crud.create_book_category(hidden, haley_cat_1) crud.create_book_category(wind, haley_cat_2) crud.create_book_category(evelyn, haley_cat_2) crud.create_book_category(mistborn, haley_cat_2) crud.create_book_category(bees, haley_cat_3) crud.create_book_category(crawdads, haley_cat_3) crud.create_book_category(addie, connor_cat_1) crud.create_book_category(hidden, connor_cat_1) crud.create_book_category(wind, connor_cat_1) crud.create_book_category(evelyn, connor_cat_2) crud.create_book_category(mistborn, connor_cat_2) crud.create_book_category(bees, connor_cat_3) crud.create_book_category(crawdads, connor_cat_3) crud.create_book_category(addie, steven_cat_1) crud.create_book_category(hidden, steven_cat_1) crud.create_book_category(wind, steven_cat_1) crud.create_book_category(evelyn, steven_cat_2) crud.create_book_category(mistborn, steven_cat_2) crud.create_book_category(bees, steven_cat_3) crud.create_book_category(crawdads, steven_cat_3) crud.create_book_category(addie, mary_cat_1) crud.create_book_category(neverwhere, mary_cat_1) crud.create_book_category(wind, mary_cat_1) crud.create_book_category(evelyn, mary_cat_2) crud.create_book_category(mistborn, mary_cat_2) crud.create_book_category(bees, mary_cat_3) crud.create_book_category(crawdads, mary_cat_3) crud.create_book_category(harry_potter, jake_cat_1) crud.create_book_category(hidden, jake_cat_1) crud.create_book_category(wind, jake_cat_1) crud.create_book_category(evelyn, jake_cat_2) crud.create_book_category(mistborn, jake_cat_2) crud.create_book_category(bees, jake_cat_3) crud.create_book_category(kings, jake_cat_3)