Esempio n. 1
0
    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 = Answer.parser.parse_args()

        # data namespace is rolled into one argument (**data)
        answer = AnswerModel(**data)

        try:
            answer.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return answer.json(), 201
Esempio n. 2
0
    def put(self, answer_id):
        data = Answer.parser.parse_args()

        answer = AnswerModel.find_by_id(answer_id)

        if answer is None:  # Create a new answer if it does not exist in the database
            answer = AnswerModel(**data)
        else:  # Update the answer if it exists in the database
            answer.question_id = data['question_id']
            answer.answer_is_correct = data['answer_is_correct']
            answer.answer_statement = data['answer_statement']
            answer.answer_update = datetime.now()

        answer.save_to_db()

        return answer.json()
Esempio n. 3
0
    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