def post(self, id=None): """ Function to make a new book""" if id: abort(400, "This is a bad request, try again") self.reqparse.add_argument("book_name", type=str, required=True, help="Book Name Required") self.reqparse.add_argument("book_isbn", type=str, required=True, help="Book ISBN Required") self.reqparse.add_argument("stock_count", type=int, required=True, help="Book Name Required") args = self.reqparse.parse_args() book_name = args["book_name"] book_isbn = args["book_isbn"] stock_count = args["stock_count"] # Validating the user inputs if not is_not_empty(book_name): return {"message": "No blank book names allowed"}, 400 if book_name.isspace(): return { "message": "The name you have entered is not relevant" }, 400 # creating and saving an instance of a book book_instance = Books(book_name=book_name, book_isbn=book_isbn, stock_count=stock_count, user_id=g.user.id) save(book_instance) msg = (book_instance.book_name + "of ID" + str(book_instance.id) + " Has been \ saved successfully") return {"message": msg}, 201
def post(self): """ Function to create a new user""" args = self.reqparse.parse_args() username, password, email = (args["username"].lower(), args["password"], args["email"]) # Validating the user input using regular expressions if not re.match("^[a-zA-Z0-9_.-]+$", username): return { "message": ("only numbers, letters, '-','-','.' allowed" "in username entry") }, 400 # Validating email inputs with regular expressions if not re.match("\S+[@]\S+[.]\S", email): return {"message": "Enter a valid email"}, 400 # Password validation by size if len(password) < 6: return {"message": "password must be at least 6 characters"}, 400 user_info = User.query.filter_by(username=username).first() # Condition to check itf the username entered is available for a new # user if user_info is not None: return { "message": "The username you have entered is not available,\ try a different one" }, 403 user = User(username=username, email=email, password=password) save(user) # Return a message id the user has been successfully added to the # system msg = "You have been successfully added as " + user.username return {"message": msg}, 201
def put(self, book_isbn=None): if not book_isbn: return {"message": "Bad request"}, 400 self.reqparse.add_argument("book_isbn", type=str, required=True, help="Book ISBN is required") args = self.reqparse.parse_args() book_isbn = args["book_isbn"] if not is_not_empty(book_isbn): return {"message": "No blank fields allowed"}, 400 if book_isbn.isspace(): return {"message": "The ISBN entered is invalid "}, 400 book_info = Books.query.filter_by(book_isbn=book_isbn.first()) if not book_info or (book_info.user_id != g.user.id): abort(404, "Book is not found") book_info.stock_count = stock_count + 1 save(book_info) msg = ("Book ID: " + str(book_info.id) + "Is Updated") return {"message": msg}, 200
def save_images(self, images: List[str]) -> None: # сохранение изображений на диск for index, image in enumerate(images): save(f"{self.path_to_save_image}/{int(time())}_{index}.jpg", image)