def post(self): """POST request that creates an author, provided a name and description, image_url, wiki_url""" # Parse the application/json data data = Author.parser.parse_args() # Look if we find the author by its name if AuthorModel.find_by_name(data['name']): return { 'message': "An author with name '{}' already exists.".format(data['name']) }, 400 # If user doesn't exists then we create it author = AuthorModel(**data) # we try push and commit try: author.save_to_db() except: #if error return {"message": "An error occurred inserting the author."}, 500 # We return a json of the author return author.json(), 201
def put(self, name): """PUT request that creates an author, provided a name and description, image_url, wiki_url""" # Parse the application/json data data = Author.parser.parse_args() # Request to the model to find the author author = AuthorModel.find_by_name(name) # If found if author: # We update its variables author.name = data['name'] author.description = data['description'] author.image_url = data['image_url'] author.wiki_url = data['wiki_url'] else: # Else we create it author = AuthorModel(**data) # Then we save author.save_to_db() # We return the updated author in json return author.json()
def post(self, name): """POST request that creates an author, provided a name and description, image_url, wiki_url""" # Request to the model and if found if AuthorModel.find_by_name(name): # Return meessage that author exists return { 'message': "An Author with name '{}' already exists.".format(name) }, 400 # Parse the application/json data data = Author.parser.parse_args() # We pass the arguments to the model author = AuthorModel(name, **data) # Try to save try: author.save_to_db() except: # if error return {"message": "An error occurred inserting the Author."}, 500 # Return a json of the created author return author.json(), 201
def post(self, name): for author in AuthorModel.find_by_name(name): if author.name == name: return {'message': f'User: {name} already exists.'} author = AuthorModel(name) author.save_to_db() return author.json(), 201
def post(self, name): if _author(name): return {"message": "author with that name already exists"}, 409 data = Author.parser.parse_args() author = AuthorModel(name) try: author.save_to_db() except Exception: return {'message': 'can not save the note'}, 500 return author.json(), 201
def post(self): data = self.arguments.parse_args() author = AuthorModel(**data) author_find = author.find(author.firstname) if author_find: #return {"mensage": "Book id '{}' already exists.".format(data.title)}, 200 return author_find.json(), 409 else: try: author.save() except: return { 'mensage': 'An internal error ocurred trying to save author.' }, 500 return author.json()
def post(self, id): """POST request that deals with the creation of an a new author""" if AuthorModel.find_by_id(id): return { 'message': "An Author with id '{}' already exists.".format(id) }, 400 # Parse the application/json data data = Author.parser.parse_args() # Call the model author = AuthorModel(id, **data) try: # Try to save and commit the author author.save_to_db() except: # If it breaks return {"message": "An error occurred inserting the Author."}, 500 # Return the json of the author return author.json(), 201
def put(self, id): """PUT request that deals with the edit or a creation of an author with at a certain id""" # Parse the application/json data data = Author.parser.parse_args() # Call the model author = AuthorModel.find_by_id(id) # if exists if author: # Update the fields author.name = data['name'] author.description = data['description'] author.image_url = data['image_url'] author.wiki_url = data['wiki_url'] else: # Else we create author = AuthorModel(**data) # save and commit author.save_to_db() # Return json when all is done. return author.json()