def put(self, quiz_id, question_id): data = parser.parse_args() data['quiz_id'] = quiz_id question = QuestionModel.find_by_id(question_id) if question is None: new_question = QuestionModel(**data) try: new_question.save() return new_question.json(), 201 except: return { "message": "An error occurred while inserting Question." }, 500 try: question.update(**data) except: return { "message": "An error occurred while updating Question." }, 500 return question.json(), 200
def post(self, quiz_id): data = parser.parse_args() data['quiz_id'] = quiz_id question = QuestionModel(**data) try: question.save() except: return { "message": "An error occurred while inserting Question." }, 500 return question.json(), 201
def post(self, id): if QuestionModel.find_by_id(id): return { 'message': "A question with id '{}' already exists.".format(id) }, 400 question = QuestionModel(id) try: question.save_to_db() except: return {"message": "An error occurred creating the question."}, 500 return question.json(), 201
def post(self): parser = self.default_parser parser.add_argument('evaluation', type=int, required=True, help="A question needs an evaluation") parser.add_argument('author', type=str, required=True, help="A question needs an author") data = parser.parse_args() question = QuestionModel(**data) try: question.save_to_db() except Exception as e: return dict(message='something goes wrong with your insert db action', error=e.args) return question.json(), 201
def post(self): # parse_args() return only arguments added by add_argument as Namespace # Any missing added argument will stop and return help message to the browser data = Question.parser.parse_args() # data namespace is rolled into one argument (**data) question = QuestionModel(**data) try: question.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return question.json(), 201
def put(self, question_id): data = Question.parser.parse_args() question = QuestionModel.find_by_id(question_id) if question is None: # Create a new question if it does not exist in the database question = QuestionModel(**data) else: # Update the question if it exists in the database question.quiz_id = data['quiz_id'] question.question_category = data['question_category'] question.questiontype_id = data['questiontype_id'] question.question_statement = data['question_statement'] question.question_correct_entries = data[ 'question_correct_entries'] question.question_wrong_entries = data['question_wrong_entries'] question.question_update = datetime.now() question.save_to_db() return question.json()
def post(self, qa_id): session = QaModel.get_by_id(qa_id) ##If sesssion exists, is ongoing, and question doesnt exist, allow posting question, else error if session: if not session.is_ongoing(): return {"message": "This session is not currently active"}, 400 request_data = Question.parser.parse_args() if request_data["text"] == "" or request_data[ "asked_by_user"] == "": return { "message": "Please fill all required fields, they cannot be empty" }, 400 if QuestionModel.question_exists( qa_id, request_data["text"].strip(), request_data["asked_by_user"].strip()): return { "message": "This question already exists for this session" }, 400 question = QuestionModel(qa_id, request_data["text"].strip(), request_data["asked_by_user"].strip()) try: question.save_to_db() except Exception as e: print(str(e)) return { "message": "An error occurred while posting the question." }, 500 else: return {"message": "The session was not found"}, 404 #return object json representation if request was successful return question.json(), 201