示例#1
0
 def post(self):
     """Post method to allow addition of book"""
     add_book_args = add_book_parser.parse_args()
     book_title = add_book_args['book_title'].strip().title()
     authors = add_book_args['authors'].strip().title()
     year = add_book_args['year'].strip()
     edition = add_book_args['edition']
     city_published = add_book_args['city_published'].strip().title()
     book_isnb = add_book_args['book_isnb'].strip()
     publisher = add_book_args['publisher'].strip().title()
     copies = add_book_args['copies']
     available_by_isnb = Book.query.filter_by(book_isnb=book_isnb).first()
     available_by_author_title = Book.query.filter_by(
         book_title=book_title, authors=authors).first()
     if available_by_isnb:
         return {
             "message":
             "This book's id is {}. Please update it.".format(
                 available_by_isnb.book_id)
         }, 409
     if available_by_author_title:
         return {
             "message":
             "This book's is {}. Please update it.".format(
                 available_by_author_title.book_id)
         }, 409
     new_book = Book(book_id=random.randint(1111, 9999),
                     book_title=book_title,
                     authors=authors,
                     edition=edition,
                     city_published=city_published,
                     book_isnb=book_isnb,
                     publisher=publisher,
                     year=year,
                     copies=copies)
     new_book.save_book()
     return {
         "message": "The book was added successfully.",
         "book_added": new_book.book_serializer()
     }, 201