2022-01-17 18:50:08 +01:00
|
|
|
import requests
|
2021-12-05 13:50:34 +01:00
|
|
|
from django.views.generic import TemplateView
|
|
|
|
from rest_framework import views
|
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2022-01-22 19:55:25 +01:00
|
|
|
from trials.models import Test, SolvedTest
|
2021-12-05 13:50:34 +01:00
|
|
|
from trials.serializers import TestSerializer
|
2022-01-09 22:43:49 +01:00
|
|
|
from django.conf import settings
|
2022-01-17 16:31:09 +01:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django import template
|
2022-01-17 16:05:26 +01:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
|
|
from django.template import loader
|
|
|
|
from django.template.loader import render_to_string, get_template
|
2022-01-17 18:50:08 +01:00
|
|
|
from django.http import HttpRequest
|
|
|
|
import requests
|
2022-01-17 16:31:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def addTest(request):
|
|
|
|
return render(request, 'createTest.html')
|
|
|
|
|
|
|
|
|
2022-01-22 19:55:25 +01:00
|
|
|
def addQuestions(request, **kwargs):
|
2022-01-17 16:31:09 +01:00
|
|
|
if request.POST:
|
2022-01-22 19:55:25 +01:00
|
|
|
name = request.GET.get("name")
|
|
|
|
passing = request.GET.get("passing")
|
|
|
|
category = request.GET.get("category")
|
|
|
|
number_of_questions = request.GET.get("number_of_questions")
|
|
|
|
import pdb;pdb.set_trace()
|
|
|
|
for question ,values in request.POST.iteritems():
|
|
|
|
|
|
|
|
|
|
|
|
import pdb;pdb.set_trace()
|
|
|
|
return render(request, 'addQuestions.html')
|
2022-01-17 18:50:08 +01:00
|
|
|
|
|
|
|
|
2021-12-05 13:50:34 +01:00
|
|
|
|
|
|
|
|
2022-01-17 17:38:53 +01:00
|
|
|
def myTests(request):
|
|
|
|
context = {}
|
|
|
|
# context['tests']=Test.objects.filter(category=request.user)
|
|
|
|
context['tests']=Test.objects.filter(category="Matematyka")
|
|
|
|
#context['tests'] = Test.objects.all
|
|
|
|
return render(request, 'myTests.html', context)
|
|
|
|
|
|
|
|
|
2022-01-19 20:56:00 +01:00
|
|
|
def solvedTests(request):
|
|
|
|
context = {}
|
2022-01-22 19:55:25 +01:00
|
|
|
user_id = request.user.id
|
|
|
|
solved_tests = SolvedTest.objects.filter(user__id=user_id)
|
|
|
|
formatted_tests = list()
|
|
|
|
for solved_test in solved_tests:
|
|
|
|
formatted_tests.append({
|
|
|
|
"name": solved_test.test.name_and_passing_score()["name"],
|
|
|
|
"passing_score": solved_test.test.name_and_passing_score()["passing_score"],
|
|
|
|
"score": solved_test.score
|
|
|
|
})
|
|
|
|
context['tests'] = formatted_tests
|
2022-01-19 20:56:00 +01:00
|
|
|
return render(request, 'myTests.html', context)
|
|
|
|
|
|
|
|
|
2022-01-17 17:38:53 +01:00
|
|
|
def editTest(request):
|
|
|
|
pass
|
|
|
|
# TODO
|
|
|
|
|
2022-01-21 18:34:46 +01:00
|
|
|
class EditTestTemplateView(TemplateView):
|
|
|
|
PASSED = "passed"
|
|
|
|
FAILED = "failed"
|
|
|
|
UNKNOWN = "unknown"
|
|
|
|
|
|
|
|
PASSED = {
|
|
|
|
True: PASSED,
|
|
|
|
False: FAILED
|
|
|
|
}
|
|
|
|
|
|
|
|
permission_classes = []
|
|
|
|
template_name = settings.BASE_DIR + f"/templates/editTest.html"
|
|
|
|
test_id = None
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Test.objects.all()
|
|
|
|
|
|
|
|
def get_context_data(self, test_id, **kwargs):
|
|
|
|
self.test_id = test_id
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
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):
|
|
|
|
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))
|
|
|
|
|
2022-01-17 17:38:53 +01:00
|
|
|
|
2021-12-05 13:50:34 +01:00
|
|
|
class TestModelViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = Test.objects.all()
|
|
|
|
serializer_class = TestSerializer
|
|
|
|
|
2022-01-17 18:50:08 +01:00
|
|
|
|
2021-12-05 13:50:34 +01:00
|
|
|
class TestTemplateView(TemplateView):
|
2022-01-17 16:05:26 +01:00
|
|
|
PASSED = "passed"
|
|
|
|
FAILED = "failed"
|
|
|
|
UNKNOWN = "unknown"
|
|
|
|
|
|
|
|
PASSED = {
|
|
|
|
True: PASSED,
|
|
|
|
False: FAILED
|
|
|
|
}
|
2021-12-05 13:50:34 +01:00
|
|
|
|
2022-01-09 22:43:49 +01:00
|
|
|
permission_classes = []
|
|
|
|
template_name = settings.BASE_DIR + f"/templates/generic_test.html"
|
2022-01-16 17:51:36 +01:00
|
|
|
test_id = None
|
2022-01-09 22:43:49 +01:00
|
|
|
|
2021-12-05 13:50:34 +01:00
|
|
|
def get_queryset(self):
|
|
|
|
return Test.objects.all()
|
|
|
|
|
|
|
|
def get_context_data(self, test_id, **kwargs):
|
2022-01-16 17:51:36 +01:00
|
|
|
self.test_id = test_id
|
2021-12-05 13:50:34 +01:00
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["test"] = self.get_queryset().filter(id=test_id).prefetch_related("questions__answers").first()
|
|
|
|
return context
|
|
|
|
|
2022-01-17 16:05:26 +01:00
|
|
|
def get_score(self, test: Test, answers):
|
|
|
|
return test.get_score(answers)
|
2022-01-16 17:51:36 +01:00
|
|
|
|
2022-01-17 16:05:26 +01:00
|
|
|
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
|
2022-01-16 17:51:36 +01:00
|
|
|
|
2022-01-17 16:05:26 +01:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
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
|
|
|
|
}
|
2022-01-22 19:55:25 +01:00
|
|
|
SolvedTest.objects.create(
|
|
|
|
score=score,
|
|
|
|
user=request.user,
|
|
|
|
test=test
|
|
|
|
)
|
2022-01-17 16:05:26 +01:00
|
|
|
template_name = "result.html"
|
|
|
|
template = get_template(template_name)
|
|
|
|
return HttpResponse(template.render(context))
|
2021-12-05 13:50:34 +01:00
|
|
|
|
2022-01-12 10:50:34 +01:00
|
|
|
def testView(request):
|
|
|
|
permission_classes = []
|
|
|
|
template_name = settings.BASE_DIR + f"/templates/generic_test.html"
|
|
|
|
context = {}
|
|
|
|
|
|
|
|
if request.POST:
|
|
|
|
form = RegistrationForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
User.objects.create(
|
|
|
|
email=form.cleaned_data["email"],
|
|
|
|
first_name=form.cleaned_data["first_name"],
|
|
|
|
last_name=form.cleaned_data["last_name"],
|
|
|
|
password=form.cleaned_data["password1"]
|
|
|
|
)
|
|
|
|
return redirect('register_success')
|
|
|
|
else:
|
|
|
|
context['registration_form'] = form
|
|
|
|
else:
|
|
|
|
context["test"] = Test.objects.all().filter(id=test_id).prefetch_related("questions__answers").first()
|
|
|
|
return render(request, 'generic_test.html', context)
|
|
|
|
|
|
|
|
|
2021-12-05 13:50:34 +01:00
|
|
|
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
|
|
|
|
})
|
2022-01-12 10:50:34 +01:00
|
|
|
|
|
|
|
class TestResultView(TemplateView):
|
|
|
|
permission_classes = []
|
|
|
|
template_name = settings.BASE_DIR + f"/templates/result.html"
|