def series_by_books(): """ Shows series linked to book """ book_info = request.form.get("book").split("||") book_id = book_info[0].strip() book_name = book_info[1].strip() title = "Series That {} Belong To".format(book_name) if book_id: payload = {"key": goodreads_key} url = "https://www.goodreads.com/work/{}/series".format(book_id) response = requests.get(url, params=payload) if response.status_code == 200: tree = ET.fromstring(response.content) series = {} if tree.find("series_works"): # if the book is part of a series all_series = list(tree.find("series_works")) series = sort_series(all_series) return render_template("series_results.html", series=series, title=title, tfs=timeframes) else: # something went wrong with the request flash("Something went wrong. Please try again.", "danger") return redirect("/adv-search") else: # for some reason we don't have a book id flash("Something went wrong.", "danger") return redirect("/adv-search")
def test_sort_series_multi_series(self): """Tests to see if sort_series returns correct results when given multiple series of different user_positions""" test_xml = """<series_works> <series_work> <user_position>1</user_position> <series> <id>5</id> <title>The Answer</title> </series> </series_work> <series_work><user_position>5</user_position> <series> <id>40</id> <title>Not The Answer</title> </series> </series_work> <series_work> <user_position>0.5</user_position> <series> <id>573</id><title>Not Right</title> </series> </series_work> <series_work> <user_position>1</user_position> <series> <id>60</id> <title>Another Answer</title> </series> </series_work> <series_work> <user_position>5</user_position> <series> <id>7</id> <title>Wrong!</title> </series> </series_work> </series_works> """ test_lst = list(ET.fromstring(test_xml)) self.assertEqual(sort_series(test_lst), {"5": "The Answer", "60": "Another Answer"})
def test_sort_series_one_series(self): """ Tests to see if sort_series returns a dictionary when passed in one series""" test_lst = [] test_xml = """<series_work> <user_position>1</user_position> <series> <id>400</id> <title>Test</title> </series> </series_work>""" test_lst.append(ET.fromstring(test_xml)) self.assertEqual(sort_series(test_lst), {"400": "Test"})
def test_sort_series_two_series(self): """Tests to see if sort_series returns a dictionary with the right id when passed in two series of different user_position values""" test_xml = """<series_works> <series_work> <user_position>1</user_position> <series> <id>5</id> <title>The Answer</title> </series> </series_work> <series_work><user_position>5</user_position> <series> <id>40</id> <title>Not The Answer</title> </series> </series_work> </series_works> """ test_lst = list(ET.fromstring(test_xml)) self.assertEqual(sort_series(test_lst), {"5": "The Answer"})
def test_sort_series_zero_series(self): """ Tests to see if sort_series functions returns an empty dictionary if passed in an empty list""" self.assertEqual(sort_series([]), {})