示例#1
0
 def post(self, user_id):
     '''endpoint allows admin to add a new book information'''
     author = request.json.get('author')
     title = request.json.get('title')
     publisher = request.json.get('publisher')
     edition = request.json.get('edition')
     category = request.json.get('category')
     copies = request.json.get('copies')
     if title is None:
         return make_response(
             jsonify('provide book title in correct format')), 400
     if publisher is None:
         return make_response(
             jsonify('provide book publisher information in correct format')
         ), 400
     if edition is None:
         return make_response(
             jsonify(
                 'provide book edition information in correct format')), 400
     if category is None:
         return make_response(
             jsonify('provide book category information in correct format')
         ), 400
     if author.isdigit() or title.isdigit() or publisher.isdigit(
     ) or category.isdigit():
         return make_response(
             jsonify({'message': 'book details must be alphabet'})), 409
     if not re.findall(r'(^[A-Za-z]+\s[A-Za-z]+$)', author):
         return make_response(
             jsonify({'message':
                      'author must be in form of Evalyn James'})), 409
     if not isinstance(copies, int):
         return make_response(
             jsonify({'message':
                      'copies must be provided in digits only'})), 400
     if not User.admin_flag(user_id):
         return make_response(
             jsonify({
                 'message':
                 'you are not authorized to perform this action'
             })), 401
     else:
         book = Book(author=author,
                     book_title=title,
                     publisher=publisher,
                     edition=edition,
                     category=category,
                     copies=copies,
                     user_id=user_id)
         if book.book_exist(author, title, edition):
             return make_response(
                 jsonify({
                     'message':
                     'book already exist, perhaps you can change its quantity.'
                 })), 409
         book.save_book()
         response = {
             'book_id': book.id,
             'book_title': book.book_title,
             'publisher': book.publisher,
             'edition': book.edition,
             'category': book.category,
             'copies': book.copies,
             'creator': book.creator_id
         }
         return make_response(jsonify(response)), 201