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 put(self, questionID, answerID): # parser = reqparse.RequestParser() # parser.add_argument('downvote', # type = str, # required = True, # help = "The upvote field is required" # ) # data = parser.parse_args() question = QuestionModel.find_by_id(questionID) if question: answer = AnswerModel.find_by_id(questionID, answerID) if answer: answer.downvote(questionID) return { "message": "Answer downvoted successfully" }, 201 #Created else: return { "message": "Cannot downvote for a non-existing answer." }, 422 #Unprocessable entity else: return { "message": "Cannot downvote answer for a non-existing question." }, 422 #Unprocessable entity
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 test_json(): create_tables() create_user() my_question = QuestionModel('Json title', 'Json description') my_question.save(1) question_query = QuestionModel.find_by_id(1) assert question_query.json() == { "title": 'Json title', "description": 'Json description' } teardown()
def test_question_find_by_id(): """ Given a new question saved When a query is called by id Then a new object should be returned, check if the fields are correct """ create_tables() create_user() new_question = QuestionModel('This is a sample 1 title', 'This is a sample 1 description') new_question.save(1) #It's the first to be stored hence id == 1 question = QuestionModel.find_by_id(1) assert question.title == 'This is a sample 1 title' new_question1 = QuestionModel('This is a sample 2 title', 'This is a sample 2 description') new_question1.save(1) question1 = QuestionModel.find_by_id(2) assert question1.title == 'This is a sample 2 title' teardown()
def get(self, questionID): #Check if the question exists #if True, return the answers #else, return an error message if QuestionModel.find_by_id(questionID): answers = AnswerModel.get_answers(questionID) if answers: return answers, 200 return { "message": "There are no answers for this question" }, 422 #Unprocessable entity return { "message": "You can't find answers for a non existing question" }, 422 # Unp0rocessable entity
def test_delete(): """ GIVEN a new question saved WHEN a new query is passed for deletion THEN it should return True """ create_tables() create_user() new_question = QuestionModel('This is a sample title', 'This is a sample description') new_question.save(1) question = QuestionModel.find_by_id(1) assert question.delete() == True teardown()
def delete(self, questionID, answerID): question = QuestionModel.find_by_id(questionID) if question: answer = AnswerModel.find_by_id(questionID, answerID) if answer: answer.delete(questionID) return {"message": "Answer deleted successfully"}, 201 else: return { "message": "Answer already deleted or does not exist." }, 422 else: return { "message": "Cannot delete answer for a non-existing question." }, 422
def get(self, questionID, answerID): #Check if the question really exists #If True check for the answer and return #else, return a error message if QuestionModel.find_by_id(questionID): #Check for answer, returns an object answer = AnswerModel.find_by_id(questionID, answerID) if answer: return AnswerModel.find_descriptive_single_answer( answerID), 200 else: return {"message": "Answer not found."}, 422 else: return { "message": "Cannot get answer for a non-existing question" }, 422
def put(self, questionID, answerID): question = QuestionModel.find_by_id(questionID) if question: answer = AnswerModel.find_by_id(questionID, answerID) if answer: answer.upvote(questionID) return { "message": "Answer upvoted successfully" }, 201 #Created else: return { "message": "Cannot upvote for a non-existing answer." }, 422 #Unprocessable entity else: return { "message": "Cannot upvote answer for a non-existing question." }, 422 #Unprocessable entity
def put(cls, questionID, answerID): data = cls.parser.parse_args() #check if the question exists #if exists, check for the answer #create a new answer object, update and return it #else return an error message if QuestionModel.find_by_id(questionID): answer = AnswerModel.find_by_id(questionID, answerID) if answer: answer.answer = data['answer'] if answer.update(questionID): return {"message": "Your answer updated successfully"}, 201 else: return {"message": "Cannot update a non-existing answer."}, 422 else: return { "message": "Cannot update answer for a non-existing question." }, 422
def post(cls, questionID): #Check if the question exists #if True create an answer dictionary and pass it #to add_answer method in answermodel(return the response) #else return error messages data = cls.parser.parse_args() identity = 0 if current_identity.id: identity = current_identity.username if QuestionModel.find_by_id(questionID): if not AnswerModel.find_by_answer(questionID, data['answer']): answer = AnswerModel(data["answer"]) if answer.add_answer(questionID, identity): return {"message": "Answer inserted successfully"}, 201 return {"message": "The answer already exists"}, 409 return {"message": "You cannot answer a non-existing question"}, 422
def put(self, questionID, answerID): question = QuestionModel.find_by_id(questionID) if question: answer = AnswerModel.find_by_id(questionID, answerID) if answer: answer.solve(questionID) return { "message": "Answer marked as solution successfully" }, 201 #Created else: return { "message": "Cannot mark a non-existing answer as a solution." }, 422 #Unprocessable entity else: return { "message": "Cannot solve a non-existing question." }, 422 #Unprocessable entity