def put(cls, questionID): #Check if the question description already exists #if it exists return an error message #if not, check if the question identified by id exists #if true, update the question and return #else, return an error message #Get the identity of the currently logged in user identity = 0 if current_identity.id: identity = current_identity.username data = cls.parser.parse_args() if not QuestionModel.find_by_description(data['description']): question = QuestionModel.find_by_id(questionID) validate_owner = QuestionModel.find_descriptive_single_question( question.id) if question: if validate_owner.get('author') == identity: #create a new object with updated details updated_question = QuestionModel(data['title'], data['description'], question.id) #Update and return json data if updated_question.update(): return QuestionModel.find_descriptive_single_question( questionID), 200 return {"message": "Question could not be updated."}, 409 return { "message": "You do not have permission to edit the question." } return {"message": "You can't edit a non-existing question."}, 422 return { "message": "Question with the same decription already exists." }, 422
def delete(self, questionID): #Check if the question really exists #if True call the delete method and return a success message #else, return an error message #Get the identity of the currently logged in user identity = 0 if current_identity.id: identity = current_identity.username question = QuestionModel.find_by_id(questionID) validate_owner = QuestionModel.find_descriptive_single_question( question.id) if question: if validate_owner.get('author') == identity: if question.delete(): return {"message": "Question deleted successfully."}, 201 else: return { "message": "Question not deleted." }, 409 #Confilict else: return { "message": "You do not have permission to delete the question." } return { "message": "The question is not available or it was already deleted." }, 422 #Unprocessable entity
def get(self, questionID): #Check if question exists #if true(returns an object) return it #else, return an error message question = QuestionModel.find_descriptive_single_question(questionID) if question: return question, 200 return {"message": "Question not available."}, 422