def put(self, idd): # Create a new author with the data passed to us. parser = reqparse.RequestParser( ) # create parameters parser from request # define all input parameters need and its type parser.add_argument('name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument( 'birth_date', type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'), required=True, help="This field cannot be left blanck") parser.add_argument('city', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('country', type=str, required=True, help="This field cannot be left blanck") data = parser.parse_args() author = AuthorModel.find_by_id(idd) if author: author.delete_from_db() new_author = AuthorModel(data.get('name'), data.get('birth_date'), data.get('city'), data.get('country')) new_author.save_to_db() return {'message': "Author modified"}, 201 return {'message': "Author with id [{}] Not found".format(idd)}, 409
def get(self, id): """GET request that deals with requests that look for a author by id""" # Call the model Author = AuthorModel.find_by_id(id) # If exists if Author: return Author.json() # If doesn't exists return {'message': 'Author not found'}, 404
def delete(self, id): """DELETE request that deals with the deletion of an author provided a certain id""" # Call the model Author = AuthorModel.find_by_id(id) # If exists if Author: try: # We try to delete Author.delete_from_db() return {'message': 'Author deleted'} except: return { 'message': 'ERROR : During the deletion of author : {}'.foramt(id) } else: # In case we don't found the author return {'message': 'Author not found'}, 404
def delete(self, id): """DELETE request that deals with the deletion of an author provided an authorId""" # We look for the Author provided an id Author = AuthorModel.find_by_id(id) # If exists if Author: try: # We try to delete it from the database Author.delete_from_db() return {'message': 'Author deleted'} except: # If error return { 'message': 'The author has relations you might want to delete the books that belongs to him first.' } else: # If he doesn't exist return {'message': 'Author not found'}, 404
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()
def get(self, idd): author = AuthorModel.find_by_id(idd) if author: return {'author': author.json()} return {'message': "Author with id [{}] Not found".format(idd)}, 409
def delete(self, idd): author = AuthorModel.find_by_id(idd) if author: author.delete_from_db() return {'message': "OK"}, 201 return {'message': "Artist with id [{}] Not found".format(idd)}, 409