import pdb import requests from django.views.generic import TemplateView from rest_framework import views, generics from rest_framework import viewsets from rest_framework.response import Response from rest_framework.views import APIView from questions.models import Question from trials.models import Test, SolvedTest 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): return render(request, 'createTest.html') def is_visible(visible): if visible =="public": return True else: return False def addQuestions(request, **kwargs): if request.POST: name = request.GET.get("name") visible = is_visible(request.GET.get("visible")) passing = request.GET.get("passing") category = request.GET.get("category") number_of_questions = request.GET.get("number_of_questions") user = request.user # k = dict(request.POST)["desc"] questions = [] for i in range(0, int(number_of_questions)): description = dict(request.POST)["desc"][i] answer1 = { "description": dict(request.POST)["ans1"][i], "is_correct": False } answer2 = { "description": dict(request.POST)["ans2"][i], "is_correct": False } answer3 = { "description": dict(request.POST)["ans3"][i], "is_correct": False } answer4 = { "description": dict(request.POST)["ans4"][i], "is_correct": False } is_correct = dict(request.POST)["is_correct"][i] if is_correct == "1": answer1["is_correct"] = True if is_correct == "2": answer2["is_correct"] = True if is_correct == "3": answer3["is_correct"] = True if is_correct == "4": answer4["is_correct"] = True questions.append({ "name": description, "answers": [ answer1, answer2, answer3, answer4 ] }) Test.objects.create(name=name, passing_score=passing, category=category, questions=questions, user=user, visible=visible) return redirect('home') return render(request, 'addQuestions.html') class AddQuestionToExistingTest(TemplateView): template_name = settings.BASE_DIR + f"/templates/addQuestionToExistingTest.html" def post(self, request, *args, **kwargs): answers = [] answer1 = { "description": dict(request.POST)["ans1"][0], "is_correct": False } answer2 = { "description": dict(request.POST)["ans2"][0], "is_correct": False } answer3 = { "description": dict(request.POST)["ans3"][0], "is_correct": False } answer4 = { "description": dict(request.POST)["ans4"][0], "is_correct": False } is_correct = dict(request.POST)["is_correct"][0] if is_correct == "1": answer1["is_correct"] = True if is_correct == "2": answer2["is_correct"] = True if is_correct == "3": answer3["is_correct"] = True if is_correct == "4": answer4["is_correct"] = True answers.append({ "answers": [ answer1, answer2, answer3, answer4 ] }) description = dict(request.POST)["desc"][0] Question.objects.addQuestionToExistingTest(name=description, answers=answers, test=Test.objects.get(id=kwargs["test_id"])) return redirect(f'/tests/{kwargs["test_id"]}/edit') class RemoveQuestionFromExistingTest(TemplateView): template_name = settings.BASE_DIR + f"/templates/removeQuestionFromExistingTest.html" 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 post(self, request, *args, **kwargs): Question.objects.get(id=request.POST["id"]).delete() return redirect(f'/tests/{kwargs["test_id"]}/edit') def myTests(request): context = {} user = request.user.id context['tests'] = Test.objects.filter(created_by=user) return render(request, 'myTests.html', context) def solvedTests(request): context = {} 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, "max": solved_test.max, "percentage": solved_test.percentage }) context['tests'] = formatted_tests return render(request, 'solvedTests.html', context) def editTest(request): if request.POST: # TODO here # firstName = request.POST.get("firstName") # lastName = request.POST.get("lastName") # # u = request.user # u.first_name = firstName # u.last_name = lastName # u.save() return redirect('myTests') return render(request, 'editTest.html') class EditTestTemplateView(TemplateView): template_name = settings.BASE_DIR + f"/templates/editTest.html" 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() if context["test"].created_by.email != self.request.user.email: return render(context, "canNotEditTest.html") return context def post(self, request, *args, **kwargs): return redirect(f'/tests/question/{request.POST["id"]}/edit?test_id={kwargs["test_id"]}') class EditQuestionTemplateView(TemplateView): template_name = settings.BASE_DIR + f"/templates/editQuestion.html" def get_queryset(self): return Question.objects.all() def get_context_data(self, question_id, **kwargs): context = super().get_context_data(**kwargs) context["question"] = self.get_queryset().filter(id=question_id).prefetch_related("answers").first() return context def post(self, request, *args, **kwargs): question_id = kwargs["question_id"] test = request.GET.get("test_id") answers = [] answer1 = { "description": dict(request.POST)["ans1"][0], "is_correct": False } answer2 = { "description": dict(request.POST)["ans2"][0], "is_correct": False } answer3 = { "description": dict(request.POST)["ans3"][0], "is_correct": False } answer4 = { "description": dict(request.POST)["ans4"][0], "is_correct": False } is_correct = dict(request.POST)["is_correct"][0] if is_correct == "1": answer1["is_correct"] = True if is_correct == "2": answer2["is_correct"] = True if is_correct == "3": answer3["is_correct"] = True if is_correct == "4": answer4["is_correct"] = True answers.append({ "answers": [ answer1, answer2, answer3, answer4 ] }) description = dict(request.POST)["desc"][0] Question.objects.EditQuestion(question_id=question_id, name=description, answers=answers) return redirect(f'/tests/{test}/edit') def deleteTest(request, test_id): Test.objects.filter(id=test_id).delete() return redirect('myTests') def editName(request, test_id): new_visible = request.GET["visible"] if new_visible == "public": new_visible = True else: new_visible = False test = Test.objects.get(id=test_id) test.visible = new_visible test.save() return redirect(f'/tests/{test_id}/edit') def editVisible(request, test_id): new_name = request.GET["name"] test = Test.objects.get(id=test_id) test.name = new_name test.save() return redirect(f'/tests/{test_id}/edit') class TestModelViewSet(viewsets.ModelViewSet): queryset = Test.objects.all() serializer_class = TestSerializer class TestTemplateView(TemplateView): PASSED = "Zaliczony" FAILED = "Niezaliczony" UNKNOWN = "nieznany" 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 get_maxscore(self, test: Test, answers): return test.get_maxscore(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)) max = self.get_maxscore(test, self.formatted_responses(request.POST)) status = score >= test.passing_score context = { "status": self.PASSED.get(status, self.UNKNOWN), "points": score, "max": max, "passing": test.passing_score, "percentage": int(score / max * 100) } SolvedTest.objects.create( score=score, max=max, percentage=int(score / max * 100), user=request.user, test=test ) 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"