import requests 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 from django.conf import settings from django.shortcuts import render, redirect from django import template from django.http import HttpResponseRedirect, HttpResponse from django.template import loader from django.template.loader import render_to_string, get_template from django.http import HttpRequest import requests def addTest(request): if request.POST: data = request.POST return addQuestions(request="GET", data_about_test=data) return render(request, 'createTest.html') def addQuestions(request, data_about_test): number_of_question = 0 if request == "GET": for key, value in data_about_test.items(): if key == "questions": number_of_question = int(value) context = { "number_of_questions": number_of_question } template_name = "addQuestions.html" template = get_template(template_name) return HttpResponse(template.render(context)) # return render(response, 'addQuestions.html') if request.POST: pass 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) def solvedTests(request): # TODO implementation 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) def editTest(request): pass # TODO 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)) class TestModelViewSet(viewsets.ModelViewSet): queryset = Test.objects.all() 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" 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)) 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) 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 }) class TestResultView(TemplateView): permission_classes = [] template_name = settings.BASE_DIR + f"/templates/result.html"