def test_get_bookrecord(self): book = BookFactory() librarian.db.session.add(book) librarian.db.session.flush() author = BookContributionFactory(role=Role.get_preset_role("Author"), book=book, creator=self.admin_user) librarian.db.session.add(author) translator = BookContributionFactory( role=Role.get_preset_role("Translator"), book=book, creator=self.admin_user) librarian.db.session.add(translator) illustrator = BookContributionFactory( role=Role.get_preset_role("Illustrator"), book=book, creator=self.admin_user) librarian.db.session.add(illustrator) librarian.db.session.commit() librarian.app.logger.debug("book has id %s" % book.id) expected_book_record = { "isbn": book.isbn, "title": book.title, "publisher": book.publisher.name, "author": [author.contributor.make_plain_person().to_dict()], "translator": [translator.contributor.make_plain_person().to_dict()], "illustrator": [illustrator.contributor.make_plain_person().to_dict()], "editor": [], "id": book.id, "genre": book.genre.name, "printer": None, "year": book.publish_year } retrieved_book_record = BookRecord.get_bookrecord(book.id) self.assertEqual({ "spam": "spammy", "cling": "clingy" }, { "cling": "clingy", "spam": "spammy" }) self.assertEqual(expected_book_record, retrieved_book_record)
def get_books(): """ Get a listing of books in the database. Request parameters: offset - Integer. The "page number" used for pagination. Default 0. limit - Integer. The number of records to return. Default 8. order - String, either "desc" or "asc". The order in which to return records. Sorting is always alphabetical by the title of the book. Default "asc". Possible responses: 200 - Will have accompanying JSON data of the books. 400 - Parameters not as expected. An accompanying error message will specify the unexpected parameters. 500 - Standard server error. """ offset = "0" limit = "8" try: offset = int(request.args.get("offset", "0")) limit = int(request.args.get("limit", "8")) except ValueError: return ( "offset and limit must be integers, given (offset: %s, limit: %s)" % (offset, limit), 400) order = request.args.get("order", "asc") if order not in ("desc", "asc"): return "order can only be either 'desc' or 'asc', given %s" % order, 400 bookq = db.session.query(Book) if order == "desc": bookq = bookq.order_by(desc("title")) elif order == "asc": bookq = bookq.order_by(asc("title")) bookq = bookq.limit(limit).offset(offset * limit) books = bookq.all() book_listing = [BookRecord.get_bookrecord(book.id) for book in books] none_count = sum([1 for book in book_listing if book is None]) app.logger.info("none count: %s" % none_count) book_listing = [book for book in book_listing if book is not None] app.logger.debug("Got these books" + str(books)) return flask.jsonify({"data": book_listing})