def t075_QuestionAnswer(self): """QuestionAnswer""" L.i("QuestionAnswerTestData load start") L.i(" building the questionAnswer list") c = 0 for el in TestData.listQuestionAnswer: qa = QuestionAnswer(body=el['body'], rating=el['rating'], dateTime=self.parseDateTime(el['dateTime']), relatedTo=None, who=random.choice(self.users)) self.questionAnswers.append(qa) qa.save() L.i(" Dataloaded #{0}".format(c)) c += 1 L.i(" questionAnswer list built") L.i(" cross linking") for i in xrange(len(self.questionAnswers)): self.questionAnswers[i].relatedTo = \ random.choice( self.questionAnswers[0:i] + self.questionAnswers[(i+1):]) L.i(" cross linking ended") L.i("QuestionAnswerTestData load ended") return True
def t075_QuestionAnswer(self): """QuestionAnswer""" L.i("QuestionAnswerTestData load start") L.i(" building the questionAnswer list") c = 0 for el in TestData.listQuestionAnswer: qa = QuestionAnswer( body = el['body'], rating = el['rating'], dateTime = self.parseDateTime(el['dateTime']), relatedTo = None, who = random.choice(self.users) ) self.questionAnswers.append(qa) qa.save() L.i(" Dataloaded #{0}".format(c)); c+=1; L.i(" questionAnswer list built") L.i(" cross linking") for i in xrange(len(self.questionAnswers)): self.questionAnswers[i].relatedTo = \ random.choice( self.questionAnswers[0:i] + self.questionAnswers[(i+1):]) L.i(" cross linking ended") L.i("QuestionAnswerTestData load ended") return True
async def post(self, request, current_user, qid=0): try: req = request.json qa = QuestionAnswer( users=req['user_id'], question=req['question'], answer=req['answer'], ) await qa.update_or_create('users', 'question') quiz = await Quiz.get_by_id(qid) question = await quiz.get_question(req['current_question'] + 1) if isinstance(question, dict): return json(question) q = await question.to_dict() return json(q, sort_keys=True) except: logging.exception('err quiz.post') return json({}, status=500)
def survey_fill(request): ans = SurveyAnswer() orig_survey = Survey.objects.get(id=request.POST['survey_id']) ans.orig_survey = orig_survey ans.save() questions = orig_survey.question_set.all() for question in questions: qc = request.POST['question'+str(question.id)] qa = QuestionAnswer() qa.answer = Choice.objects.get(id=int(qc)) qa.survey_answer = ans qa.save() ans.save() return render(request, 'survey-complete.html', {})
def save_answers(self, instance, answers): actuals = instance.job.questions.all() requireds = actuals.filter(is_required=True) for key in answers.keys(): if not re.match("^question_[0-9]+$", key): raise Exception("Invalid key") send_ids = [int(p.split("_")[1]) for p in answers.keys()] actuals_ids = [r.id for r in actuals] required_ids = [r.id for r in requireds] # Check if all answers are for valid questions for id in send_ids: if id not in actuals_ids: raise Exception("Unknow question") # Check if required questions are answered for id in required_ids: if id not in send_ids: raise Exception("Required question not present") if answers.get('question_%s' % id) is None: raise Exception("Invalid content for required question") questions = {q.id: q for q in actuals} # Create models models = [QuestionAnswer( postulation=instance, question=questions.get(id)) for id in send_ids] # Set responses for answer in models: answer.set_response( answers.get("question_%s" % answer.question.id)) return QuestionAnswer.objects.bulk_create(models)