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"> <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,27 +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):
import pdb;pdb.set_trace() 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),
question_id = 1 "points": score
answers = [] }
while True: template_name = "result.html"
if request.POST.get(str(question_id)) is None: template = get_template(template_name)
break return HttpResponse(template.render(context))
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)
def testView(request): def testView(request):
permission_classes = [] permission_classes = []