SOITA/trials/views.py
Hubert Jankowski a5fbb7b736 Added login
2021-12-19 16:59:24 +01:00

47 lines
1.3 KiB
Python

from django.views.generic import TemplateView
from rest_framework import views
from rest_framework import viewsets
from rest_framework.response import Response
from trials.models import Test
from trials.serializers import TestSerializer
class TestModelViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all()
serializer_class = TestSerializer
class TestTemplateView(TemplateView):
def get_queryset(self):
return Test.objects.all()
def get_context_data(self, test_id, **kwargs):
context = super().get_context_data(**kwargs)
context["test"] = self.get_queryset().filter(id=test_id).prefetch_related("questions__answers").first()
return context
class TestValidateAPIView(views.APIView):
PASSED = "passed"
FAILED = "failed"
UNKNOWN = "unknown"
PASSED = {
True: PASSED,
False: FAILED
}
def get_score(self, test: Test, answers):
return test.get_score(answers)
def post(self, request, test_id, **kwargs):
test = Test.objects.get(id=test_id)
score = self.get_score(test, request.data["answers"])
status = score >= test.passing_score
return Response({
"status": self.PASSED.get(status, self.UNKNOWN),
"points": score
})