Rozwiazywanie testow #22

Merged
s470631 merged 2 commits from feature/post-test-answers into master 2022-01-17 16:06:22 +01:00
2 changed files with 42 additions and 26 deletions
Showing only changes of commit a824cba46c - Show all commits

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,27 +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):
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 = []