def post(self, request): if not request.user.is_authenticated(): return HttpResponse(json.dumps({ "status": "fail", "msg": "用户未登录" }), content_type='application/json') choices = request.POST.get('choices', '') question_id = request.POST.get('question_id', '') if int(question_id) > 0 and choices: exist_records = Answer.objects.filter(user_id=request.user.id, question_id=int(question_id)) if exist_records: return HttpResponse('{"status":"success","msg":"你已经回答过这个问题"}', content_type='application/json') answer = Answer() question = Question.objects.get(id=question_id) question_standard_choices = QuestionStandardAnswers.objects.get( question_id=question_id).choice_position answer.question_id = question.id answer.content = choices answer.user = request.user if choices != question_standard_choices: answer.correct = False answer.save() return HttpResponse('{"status":"success","msg":"答案错误"}', content_type='application/json') answer.save() return HttpResponse('{"status":"success","msg":"答案正确"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"回答失败"}', content_type='application/json')
def post(self, request): if not request.user.is_authenticated(): return HttpResponse(json.dumps({ "status": "fail", "msg": "用户未登录" }), content_type='application/json') content = request.POST.get('content', '') question_id = request.POST.get('question_id', '') if int(question_id) > 0 and content: answer = Answer() question = Question.objects.get(id=question_id) answer.question_id = question.id answer.content = content answer.user = request.user answer.save() return HttpResponse(json.dumps({ "status": "success", "msg": "回答成功", "id": answer.id }), content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"回答失败,请重新回答"}', content_type='application/json')
def run(self): answer = Answer() answer.question = self.question answer.user = self.user question_title = self.question.title question_description = self.question.description if USE_SERVER: answer_content = self.reuse(question_title, question_description) if not answer_content: answer_content = self.answer(question_title, question_description) if answer_content: answer.answer_text = answer_content answer.save()