def post(self): try: counter = 0 data = request.get_json(force=True) #print(data) num = int(data['correctAnswerNum']) new_question = QuestionModel(name=data['name'], description=data['description'], difficulty=data['difficulty'], category_id=data['category_id']) new_question.flush_db() questionId = new_question.id new_question.save_to_db() data = data['answers'] #print(questionId) for answer in data: if counter == num: new_answer = AnswersModel(question_id=questionId, answer=answer, correct='1') else: new_answer = AnswersModel(question_id=questionId, answer=answer, correct='0') new_answer.save_to_db() counter += 1 return {'response': 'Succesfull'}, 200 except Exception as e: print('ez a hiba', e) return {'response': 'Invalid data'}, 400
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 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 = Quiz.parser.parse_args() # Save data into Quiz table quiz = QuizModel(data["quiz_name"], data["theme_id"]) try: quiz.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 # Save data into Question table questions = data["questions"] for q in questions: question = QuestionModel(quiz.quiz_id, q["question_category"], q["questiontype_id"], q["question_statement"], q["question_correct_entries"], q["question_wrong_entries"]) try: question.save_to_db() except: return { "message": "An error occurred inserting the question." }, 500 # Save data into Question table answers = q["answers"] for a in answers: answer = AnswerModel(question.question_id, a["answer_is_correct"], a["answer_statement"]) try: answer.save_to_db() except: return { "message": "An error occurred inserting the answer." }, 500 return quiz.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