Exemple #1
0
def question_detail(request, pk):
    question = get_object_or_404(Question, pk=pk)
    context = {'question':question}

    if request.method == 'POST':
        answer_form = AnswerForm(request.POST)

        if answer_form.is_valid():
            answer_text = answer_form.cleaned_data.get('text')

            #creating new answer for this question
            new_answer = Answer()
            new_answer.text = answer_text
            if request.user.is_authenticated():
                new_answer.user = request.user
            new_answer.question = question
            new_answer.save()

            #message for successful save
            context['answer_saved']=True
            answer_form = AnswerForm()

        context['answer_form'] = answer_form
        # return render(request, 'question/detail.html', context)
        return redirect(request.META['HTTP_REFERER']) # redirect to same url (where form was submitted )

    else:
        answer_form = AnswerForm()
        context['answer_form']=answer_form

    return render(request, 'question/detail.html', context)
Exemple #2
0
def answer_create(user_id, code):
    """
    답변 생성
    """
    member = get_object_or_404(Member, name=user_id)
    answer = Answer(member=member, content=code, create_date=timezone.now())
    answer.save()
Exemple #3
0
def QuestionView(request, pk):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/')
    if request.method == 'POST':
        form = NewAnswerForm(request.POST)
        if form.is_valid():
            answer = Answer()
            answer.text=form.cleaned_data['text']
            answer.title=form.cleaned_data['title']
            answer.user = request.user
            answer.question=Question.objects.get(pk=int(pk))
            answer.save()
            print "Ok"
            send_mail('Subject', 'You have new answers', '*****@*****.**',
                      ['*****@*****.**'], fail_silently=False)
            return HttpResponseRedirect('/question/'+pk+'/#answer'+str(answer.pk))
        else:
            context = {'form':form}
            context['question']= Question.objects.get(pk=int(pk))
            context['answers']= Answer.objects.filter(question__pk=int(pk))
            return render_to_response('question.html', context, context_instance=RequestContext(request))
    else:
        ''' '''
        form = NewAnswerForm()
        context = {'form':form}
        context['question']= Question.objects.get(pk=int(pk))
        context['answers']= Answer.objects.filter(question__pk=int(pk))
        return  render_to_response('question.html',context, context_instance=RequestContext(request))
Exemple #4
0
def answer_question(request, q_id):
	if request.POST():
		ansform = AnswerForm(request.POST)
		if ansform.is_valid():
			q = get_object_or_404(Question, id=q_id)
			new_answer = Answer(question = q_id, content=ansform.cleaned_data['content'])
			new_answer.save()
			return render(request, 'success.html')
		else:
			context = {'ansform': ansform}
			return render(request, 'ask_question.html', context)
	else:
		ansform = AnswerForm()
		context = {'ansForm': ansform}
		return render(request, 'answer_question.html', context)
Exemple #5
0
def answer_question(request, q_id):
    if request.POST():
        ansform = AnswerForm(request.POST)
        if ansform.is_valid():
            q = get_object_or_404(Question, id=q_id)
            new_answer = Answer(question=q_id,
                                content=ansform.cleaned_data['content'])
            new_answer.save()
            return render(request, 'success.html')
        else:
            context = {'ansform': ansform}
            return render(request, 'ask_question.html', context)
    else:
        ansform = AnswerForm()
        context = {'ansForm': ansform}
        return render(request, 'answer_question.html', context)
Exemple #6
0
def analyze_answer(request, exam_id):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('exam:home'))
    template = loader.get_template('answer.html')
    if request.method == "GET":
        try:
            if not validate_number_of_attempts(request.user.id, exam_id):
                return HttpResponse("Noch Noch")

            exam_answer = ExamAnswer.objects.get(user_id=request.user.id, exam_id=exam_id)

            context = RequestContext(request, {'answer': exam_answer})
            return HttpResponse(template.render(context))
        except ObjectDoesNotExist as e:
            return HttpResponseRedirect(reverse('exam:home'))
    if request.method == "POST":
        exam = Exam.objects.get(id=exam_id)
        exam_answer, is_new = ExamAnswer.objects.get_or_create(user_id=request.user.id, exam_id=exam_id)
        if not validate_number_of_attempts(request.user.id, exam_id):
            return HttpResponse("Noch Noch")

        exam_answer_history = ExamAnswerHistory()
        exam_answer_history.user_id = request.user.id
        exam_answer_history.exam = exam
        exam_answer_history.save()
        for q in exam.question_set.all():
            answer = Answer()
            answer.question = q
            answer.exam_answer = exam_answer_history
            answer.save()
            if str(q.id) in request.POST.keys():
                for a in request.POST.getlist(str(q.id)):
                    answer.selected_choices.add(a)
            else:
                answer.selected_choices.add(-1)

        if exam_answer_history.score() > exam_answer.score:
            exam_answer.score = exam_answer_history.score()
            exam_answer.save()

    context = RequestContext(request, {'answer': exam_answer_history})
    return HttpResponse(template.render(context))