Merge pull request 'Rozwiazywanie testow' (#22) from feature/post-test-answers into master

Reviewed-on: #22
This commit is contained in:
s470631 2022-01-17 16:06:22 +01:00
commit d4e1ff1acb
3 changed files with 48 additions and 22 deletions

View File

@ -1,4 +1,5 @@
{% extends "base.html" %}
{% load rest_framework %}
{% block title %}{{ test.name }}{% endblock %}
@ -32,8 +33,8 @@
<div class="question_title">
{{ question.description }}
</div>
<div class="list-group">
<form method="post" novalidate>
{% for answer in question.answers.all %}
<label class="list-group-item">
<input class="form-check-input me-1" type="radio" name={{ question.id }} value={{ answer.id }}>
@ -45,6 +46,10 @@
{% endfor %}
</div>
<input class="testContent" type="submit" value="Send answers">
{# <button type="submit" class="testContent" value="Send answers"></button>#}
</form>
</div>
{% endblock %}

View File

@ -7,12 +7,14 @@
<img class="resultImage" src="http://kmit.in/emagazine/wp-content/uploads/2018/02/karnataka-results.jpg" alt="Card image cap">
<div class="resultBody">
<h5 class="resultMsg">Quite good! All the best for next quiz!</h5>
<h5 class="resultScore">Score: {{ request.session.points }}</h5>
<h5 class="resultScore">Result: {{ status }}</h5>
<h5 class="resultScore">Score: {{ points }}</h5>
<a class="btn btn-primary" href="/home">Home</a>
{# <p class="resultText">Percentage: 60%</p>#}
{# <p class="resultText">Correct answers: 3</p>#}
{# <p class="resultText">Incorrect answers: 2</p>#}
{# <p class="resultText">Total questions: 5</p>#}
<p class="resultText">Percentage: 60%</p>
<p class="resultText">Correct answers: 3</p>
<p class="resultText">Incorrect answers: 2</p>
<p class="resultText">Total questions: 5</p>
</div>
</div>
{% endblock %}

View File

@ -6,7 +6,9 @@ from rest_framework.response import Response
from trials.models import Test
from trials.serializers import TestSerializer
from django.conf import settings
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse
from django.template import loader
from django.template.loader import render_to_string, get_template
class TestModelViewSet(viewsets.ModelViewSet):
@ -14,6 +16,14 @@ class TestModelViewSet(viewsets.ModelViewSet):
serializer_class = TestSerializer
class TestTemplateView(TemplateView):
PASSED = "passed"
FAILED = "failed"
UNKNOWN = "unknown"
PASSED = {
True: PASSED,
False: FAILED
}
permission_classes = []
template_name = settings.BASE_DIR + f"/templates/generic_test.html"
@ -28,22 +38,31 @@ class TestTemplateView(TemplateView):
context["test"] = self.get_queryset().filter(id=test_id).prefetch_related("questions__answers").first()
return context
def get_score(self, test: Test, answers):
return test.get_score(answers)
def formatted_responses(self, unformatted_json):
formatted_response = list()
for question, answer in unformatted_json.items():
formatted_response.append(
{
"question": question,
"answer": answer
}
)
return formatted_response
def post(self, request, *args, **kwargs):
question_id = 1
answers = []
while True:
if request.POST.get(str(question_id)) is None:
break
ans_id = request.POST.get(str(question_id))
answers.append({'question': question_id, 'answer': ans_id})
question_id += 1
points = self.get_queryset().filter(id=self.test_id).get_score(answers)
#return redirect('account')
request.session['points'] = points
url = '/tests/' + str(self.test_id) + '/result'
return HttpResponseRedirect(url)
test = Test.objects.get(id=kwargs.get("test_id"))
score = self.get_score(test, self.formatted_responses(request.POST))
status = score >= test.passing_score
context = {
"status": self.PASSED.get(status, self.UNKNOWN),
"points": score
}
template_name = "result.html"
template = get_template(template_name)
return HttpResponse(template.render(context))
def testView(request):
permission_classes = []