def mutate(root, info, question_id, answer_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" question = QuestionModel.find_by_id(question_id) if not question: errors["question"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) answer = AnswerModel(created_by=user, **answer_data) answer.save() if question.answers: question.answers.append(answer) else: question.answers = [answer] question.save() return AnswerQuestion(answer=answer, ok=True)
def create_and_save_question(title, text, is_mcq, answers, room): ''' Helper to create a question and save it to the graph :param name: the name of the question :param active: whether or not this question will be active by default :param admin_id: the user id representing the admin :param course_id: the course code / id representing the course that this question studies :return ValueError: raised if either the course or user cannot be found in the graph ''' new_question = Question(title=title, text=text, is_multiple_choice=is_mcq) new_question.save() new_question.room.connect(room) for a in answers: temp_answ = Answer(text=a.get('text'), correct=a.get('correct')) temp_answ.save() temp_answ.question.connect(new_question) return new_question