Merge branch 'feature/post-test-answers' of https://git.wmi.amu.edu.pl/s470629/SOITA into feature/post-test-answers

This commit is contained in:
Piotr Kopycki 2022-01-17 16:35:23 +01:00
commit 3182902f7d
2 changed files with 42 additions and 26 deletions

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,9 +6,11 @@ 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.shortcuts import render, redirect
from django import template
from django.http import HttpResponseRedirect, HttpResponse
from django.template import loader
from django.template.loader import render_to_string, get_template
register = template.Library()
@ -61,6 +63,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"
@ -75,27 +85,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):
import pdb;pdb.set_trace()
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 = []