From 33317a84d241b3dad3bd263a6e15ab17aa3b4d11 Mon Sep 17 00:00:00 2001 From: Piotr Kopycki Date: Fri, 21 Jan 2022 18:34:46 +0100 Subject: [PATCH] Edit test page --- static/style.css | 16 ++++++++++++++ templates/editTest.html | 48 ++++++++++++++++++++++++++++++++++++++++ trials/urls.py | 4 ++-- trials/views.py | 49 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 templates/editTest.html diff --git a/static/style.css b/static/style.css index 6b42677..498c2db 100644 --- a/static/style.css +++ b/static/style.css @@ -352,4 +352,20 @@ background-color:#FF0B7E border-radius: 25px; border: none; outline: none; +} + +.editContainer { + overflow: scroll; +} + +.editContainerLine { + padding: 10px 0px 10px 0px; +} + +.editContainerSection { + padding-bottom: 15px; +} + +.editContainerSection h2 { + display: inline; } \ No newline at end of file diff --git a/templates/editTest.html b/templates/editTest.html new file mode 100644 index 0000000..59ba99a --- /dev/null +++ b/templates/editTest.html @@ -0,0 +1,48 @@ +{% extends "base.html" %} +{% load rest_framework %} + +{% block title %}{{ test.name }} - Edit{% endblock %} + +{% block additional_head %} + +{% endblock %} + +{% block content %} +
+
+
+
+ + +
+
+ {% for question in test.questions.all %} +
+
+ + +
+ {% for answer in question.answers.all %} +
+ + +
+ {% endfor %} +
+ + +
+
+ {% endfor %} +
+ +
+
+
+{% endblock %} + diff --git a/trials/urls.py b/trials/urls.py index 14615fc..ba2ca97 100644 --- a/trials/urls.py +++ b/trials/urls.py @@ -4,7 +4,7 @@ from rest_framework.routers import DefaultRouter from trials.views import TestModelViewSet from trials.views import TestTemplateView from trials.views import TestValidateAPIView -from trials.views import TestResultView, addTest, addQuestions, myTests, editTest, solvedTests +from trials.views import TestResultView, addTest, addQuestions, myTests, editTest, solvedTests, EditTestTemplateView router = DefaultRouter(trailing_slash=False) router.register("items", TestModelViewSet) @@ -13,7 +13,7 @@ urlpatterns = [ path('/show', TestTemplateView.as_view()), path('/mark', TestValidateAPIView.as_view()), path('/result', TestResultView.as_view()), - path('/edit', editTest), + path('/edit', EditTestTemplateView.as_view()), path('add/test', addTest, name="newTest"), path('add/questions', addQuestions, name="addQuestions"), path('my', myTests, name="myTests"), diff --git a/trials/views.py b/trials/views.py index 1ec78d3..a99c76c 100644 --- a/trials/views.py +++ b/trials/views.py @@ -64,6 +64,55 @@ 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()