Exemplo 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)
Exemplo n.º 2
0
def answer_question(request, pk):
    answer = Answer.objects.all()
    if request.method == 'POST':
        form = AnswerForm(request.POST)
        if form.is_valid():
            answer = form.save(commit=False)
            answer.save()
            return redirect('home')
    else:
        form = AnswerForm()
    return render(request, 'core/answer.html', {
        'form': form,
        'answer': answer
    })
Exemplo n.º 3
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'}))
Exemplo n.º 4
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'}))
Exemplo n.º 5
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})
Exemplo n.º 6
0
def answer_add(request,qid):
    """
    add answer
    """
    question = get_object_or_404(Question,id=qid)
    if request.method == 'POST':
        form = AnswerForm(request.user,question,request.POST)
        if form.is_valid():
            form.save()
            request.user.message_set.create(message="Thanks for answer the question")
            return HttpResponseRedirect(reverse('core-question',args=[question.id]))
    else:
        form = AnswerForm(request.user,question)

    return render_to_response("core/answer.html", 
            {
            'form' : form,
            'question':question
            },context_instance=RequestContext(request))
Exemplo n.º 7
0
def add_answer(request, pk):
    print('question')
    from core.forms import AnswerForm
    from django.views.generic.edit import CreateView
    # answer = get_object_or_404(Question, pk=pk)
    # question = get_object_or_404(Question, pk=pk)
    if request.method == "POST":
        form = AnswerForm(request.POST)
        if form.is_valid():
            answer = form.save(commit=False)
            answer.user = request.user
            answer.target_question = get_object_or_404(Question, pk=pk)
            form.save()
            # send_mail(
            #     f'{answer.user} answered your question',
            #     f"{answer.user} posted an answer to your question: {question}: '{answer}''",
            #     '*****@*****.**',
            #     [f'{question.owner.email}'],
            #     fail_silently=False,
            # )
    else:
        form = AnswerForm()
    return HttpResponse()
Exemplo n.º 8
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)
Exemplo n.º 9
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)