Rozwiazywanie testow #22

Merged
s470631 merged 2 commits from feature/post-test-answers into master 2022-01-17 16:06:22 +01:00
3 changed files with 48 additions and 22 deletions

View File

@ -1,4 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load rest_framework %}
{% block title %}{{ test.name }}{% endblock %} {% block title %}{{ test.name }}{% endblock %}
@ -32,8 +33,8 @@
<div class="question_title"> <div class="question_title">
{{ question.description }} {{ question.description }}
</div> </div>
<div class="list-group"> <div class="list-group">
<form method="post" novalidate>
{% for answer in question.answers.all %} {% for answer in question.answers.all %}
<label class="list-group-item"> <label class="list-group-item">
<input class="form-check-input me-1" type="radio" name={{ question.id }} value={{ answer.id }}> <input class="form-check-input me-1" type="radio" name={{ question.id }} value={{ answer.id }}>
@ -45,6 +46,10 @@
{% endfor %} {% endfor %}
</div> </div>
<input class="testContent" type="submit" value="Send answers"> <input class="testContent" type="submit" value="Send answers">
{# <button type="submit" class="testContent" value="Send answers"></button>#}
</form>
</div> </div>
{% endblock %} {% 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"> <img class="resultImage" src="http://kmit.in/emagazine/wp-content/uploads/2018/02/karnataka-results.jpg" alt="Card image cap">
<div class="resultBody"> <div class="resultBody">
<h5 class="resultMsg">Quite good! All the best for next quiz!</h5> <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>
</div> </div>
{% endblock %} {% endblock %}

View File

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