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_question_find_by_description(): """ Given a new question saved When a query is called by description Then a boolean is 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_description( 'This is a sample 1 description') assert question == True teardown()
def post(self): data = QuestionList.parser.parse_args() #Get the identity of the currently logged in user identity = 0 if current_identity.id: identity = current_identity.id else: return {"message": "Login to continue"} #Check if a question with the same description exists #return an error message if it exists #else, save the new question and return a response if QuestionModel.find_by_description(data['description']): return { "message": "The question is already asked" }, 409 # Conflict(Duplicate) #Create a question object and pass the arguments question = QuestionModel(data["title"], data["description"]) response = question.save(identity) return response, 201