def on_get(self, req, resp, *args, **kwargs): super(ResourceCheckCorrectAnswer, self).on_get(req, resp, *args, **kwargs) # Recivimos un JSON del usuario con 2 parámetros # "question" : EL texto de la pregunta # "answer" : la respuesta que el usuario ha dado a la pregunta aux_question = Question() aux_question.question = req.media["question"] # Filtraremos la pregunta para tener su id real_question = self.db_session.query(Question).filter \ (aux_question.question == Question.question).one() aux_answer = Answer() aux_answer.answer = req.media["answer"] # Filtraremos la respuesta para tener su id real_answer = self.db_session.query(Answer).filter \ (aux_answer.answer == Answer.answer).one() # Finalmente filtraremos en la Asociacion para comprovar si es correcta query = self.db_session.query(AnswerQuestionAssiation).filter\ (real_question.id == AnswerQuestionAssiation.id_question, real_answer.id == AnswerQuestionAssiation.id_answer).one() print(query.is_correct) if query.is_correct: resp.media = "La Respuesta es Correcta" else: resp.media = "La Respuesta no es Correcta" resp.status = falcon.HTTP_200
def on_post(self, req, resp, *args, **kwargs): super(ResourceAddQuestion, self).on_get(req, resp, *args, **kwargs) q = Question() try: try: q.question = req.media["question"] category = req.media["category"] if category not in [ i.value for i in CategoryEnum.__members__.values() ]: raise falcon.HTTPInvalidParam( messages.event_status_invalid, "category") q.category = category answers = req.media["answers"] self.db_session.add(q) for answer in answers: a = Answer() a.answer = answer["answer"] self.db_session.add(a) self.db_session.commit() s = AnswerQuestionAssiation() s.id_question = q.id s.id_answer = a.id if answer["is_correct"]: s.is_correct = True else: s.is_correct = False self.db_session.add(s) self.db_session.commit() except IntegrityError: raise falcon.HTTPBadRequest(description=messages.user_exists) except KeyError: raise falcon.HTTPBadRequest( description=messages.parameters_invalid) resp.status = falcon.HTTP_200
cnt = 1 q = None while line: l = line.split(":") if l[0] == "C": q = Question() q.category = str.strip(l[1]) elif l[0] == "Q": q.question = str.strip(l[1]) elif l[0] == "AF" or l[0] == "AT": q.owner_id = int(random.uniform(1, 20)) db_session.add(q) db_session.commit() a = Answer() a.answer = str.strip(l[1]) db_session.add(a) db_session.commit() s = AnswerQuestionAssiation() s.id_question = q.id s.id_answer = a.id if l[0] == "AT": s.is_correct = True else: s.is_correct = False db_session.add(s) db_session.commit() cnt += 1 line = fp.readline()