Ejemplo n.º 1
0
def question(request, id):
    """ For showing questions and posting answer """

    question = get_object_or_404(Question, id=id)

    context = {}
    context['question'] = question

    if request.method == 'GET':

        form = AnswerForm()
        context['form'] = form

    if request.method == 'POST':

        form = AnswerForm(request.POST or None)

        if form.is_valid():

            answer = form.save(commit=False)
            answer.created_by = request.user
            answer.question = question
            form.save()

            form = AnswerForm()

        context['form'] = form
    return render(request, 'core/question.html', context=context)
Ejemplo n.º 2
0
def home(request):
    form = AnswerForm()
    datas = AnswerTable.objects.all()
    import os
    print(os.path)
    if request.method == 'POST':
        form = AnswerForm(request.POST, request.FILES)
        if form.is_valid():
            data = AnswerTable()
            data.student_name = request.POST['student_name']
            data.subject_name = request.POST['subject_name']
            data.answer = request.FILES['answer']
            data.answer_key = request.FILES['answer_key']
            data.marks = random.randrange(100)  # dummy random marks
            data.save()
            return redirect('home')
    return render(request, 'home.html', {'form': form, 'datas': datas})
Ejemplo n.º 3
0
def postAnswer(request):
    if request.method != "POST":
        return HttpResponse("Must post.")
    elif not request.user.is_authenticated():
        return HttpResponse("not authenticated")

    answer = Answer()
    answer.user = request.user
    answerForm = AnswerForm(request.POST, instance=answer)
    answerForm.save()
    return HttpResponse("answer saved")
Ejemplo n.º 4
0
def index(request, lang):
    template_name = 'core/{}/index.html'.format(lang if lang else 'ru')

    participant_id = request.session.get('participant_id')
    participant = get_or_none(Participant, pk=participant_id)
    participant_form = ParticipantForm()

    questions = Question.objects.filter(participant=participant,
                                        answered=False)
    forms = []
    for question in questions:
        forms.append({
            'question':
            question,
            'answer_left_form':
            AnswerForm(initial={
                'best': question.left,
                'question': question
            }),
            'answer_right_form':
            AnswerForm(initial={
                'best': question.right,
                'question': question
            }),
            'answer_none_form':
            AnswerForm(initial={
                'best': None,
                'question': question
            }),
        })

    return render(
        request, template_name, {
            'video_path': settings.VIDEO_CORE_PATH,
            'participant': participant,
            'questions': questions,
            'participant_form': participant_form,
            'forms': forms,
            'lang': lang,
        })
Ejemplo n.º 5
0
def answer(request):
    if request.method == 'POST':
        answer_form = AnswerForm(request.POST)

        if answer_form.is_valid():
            answer = answer_form.save(commit=False)
            answer.save()

            answer.question.answered = True
            answer.question.save()

            return HttpResponse(json.dumps({'status': 'ok'}))

    return HttpResponse(json.dumps({'status': 'error'}))
Ejemplo n.º 6
0
def edit_answer(request, pk):
    context = {}
    qu = BookNode.objects.get(pk=pk)
    context['module'] = qu.get_book().module
    context['book'] = qu.get_book()
    context['question'] = qu
    context['subtree'] = qu.get_descendants(include_self=True)
    context['chapter'] = qu.get_parent_chapter()
    assignment = qu.get_parent_assignment()
    context['assignment'] = assignment
    context['toc'] = qu.get_siblings(include_self=True)

    # navigation
    questions = assignment.get_descendants().filter(
        node_type="question").order_by('mpath')
    next = questions.filter(mpath__gt=qu.mpath)
    prev = questions.filter(mpath__lt=qu.mpath).order_by('-pk')
    context['next'] = next[0] if next else None
    context['prev'] = prev[0] if prev else None

    # answer form
    # retreive current saved answer (if any)
    try:
        ans = Answer.objects.get(question=qu, user=request.user)
        form = AnswerForm(instance=ans)
    except Answer.DoesNotExist:
        form = AnswerForm(initial={'question': qu, 'user': request.user})

    # gulp
    if request.method == 'POST':
        form = AnswerForm(request.POST)
        if form.is_valid():
            ques = form.cleaned_data['question']
            user = form.cleaned_data['user']

            # search for saved answer
            answer = Answer.objects.filter(question=ques, user=user).first()

            # create new answer if required
            if not answer:
                answer = form.save(commit=False)
                answer.save()

            # switch on button pressed
            if 'save-answer' in request.POST:
                answer.text = form.cleaned_data['text']
                answer.is_readonly = form.cleaned_data['is_readonly']
                answer.save()

            elif 'save-and-exit' in request.POST:
                answer.text = form.cleaned_data['text']
                answer.is_readonly = form.cleaned_data['is_readonly']
                answer.save()
                return HttpResponseRedirect(
                    reverse('homework',
                            kwargs={'pk': ques.get_parent_assignment().id}))

            elif 'exit' in request.POST:
                return HttpResponseRedirect(
                    reverse('homework',
                            kwargs={'pk': ques.get_parent_assignment().id}))

            else:
                print "BAD LUCK"

        else:
            print "FORM INVALID"
            context['debug'] = form.errors

    context['form'] = form
    return render(request, 'edit_answer.html', context)