diff --git a/templates/generic_test.html b/templates/generic_test.html index 403ccd7..b8e9ddc 100644 --- a/templates/generic_test.html +++ b/templates/generic_test.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{% load rest_framework %} {% block title %}{{ test.name }}{% endblock %} @@ -32,8 +33,8 @@
{{ question.description }}
-
+
{% for answer in question.answers.all %}
+ +{# #} + + {% endblock %} diff --git a/templates/result.html b/templates/result.html index 4cd3fdf..450e9c4 100644 --- a/templates/result.html +++ b/templates/result.html @@ -7,12 +7,14 @@ Card image cap
Quite good! All the best for next quiz!
-
Score: {{ request.session.points }}
+
Result: {{ status }}
+
Score: {{ points }}
+ Home +{#

Percentage: 60%

#} +{#

Correct answers: 3

#} +{#

Incorrect answers: 2

#} +{#

Total questions: 5

#} -

Percentage: 60%

-

Correct answers: 3

-

Incorrect answers: 2

-

Total questions: 5

{% endblock %} diff --git a/trials/views.py b/trials/views.py index 842d6a0..f613231 100644 --- a/trials/views.py +++ b/trials/views.py @@ -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 = []