From ed31555583300bc47e4a3fa9fb4b35b1864abb80 Mon Sep 17 00:00:00 2001 From: Hubert Jankowski Date: Sun, 30 Jan 2022 20:04:27 +0100 Subject: [PATCH] edit tests --- questions/managers.py | 55 +++++++ templates/addQuestionToExistingTest.html | 45 ++++++ templates/editQuestion.html | 39 +++++ templates/editTest.html | 126 +++++++++++---- templates/removeQuestionFromExistingTest.html | 53 ++++++ trials/urls.py | 8 +- trials/views.py | 152 ++++++++++++++---- 7 files changed, 416 insertions(+), 62 deletions(-) create mode 100644 templates/addQuestionToExistingTest.html create mode 100644 templates/editQuestion.html create mode 100644 templates/removeQuestionFromExistingTest.html diff --git a/questions/managers.py b/questions/managers.py index 0ad0e41..4e192fe 100644 --- a/questions/managers.py +++ b/questions/managers.py @@ -14,6 +14,7 @@ class QuestionManager(Manager): **kwargs ): Answer = apps.get_model("answers", "Answer") + instance = super().create( name=name, test=test, @@ -27,3 +28,57 @@ class QuestionManager(Manager): ) return instance + def addQuestionToExistingTest( + self, *, + answers=[], + name=None, + test=None, + description=None, + **kwargs + ): + Answer = apps.get_model("answers", "Answer") + + instance = super().create( + name=name, + test=test, + description=name, + ) + for answer in answers: + for a in answer["answers"]: + Answer.objects.create( + question=instance, + description=a["description"], + is_correct=a["is_correct"] + ) + return instance + + + def EditQuestion( + self, *, + question_id=None, + answers=[], + name=None, + description=None, + **kwargs + ): + Answer = apps.get_model("answers", "Answer") + instance = super().get( + id=question_id + ) + + instance.name = name + instance.description = name + + actual_answers = instance.answers.all() + for answer in actual_answers: + answer.is_correct = False + answer.save() + + for i in range(0, len(answers[0]["answers"])): + actual_answer = actual_answers[i] + new_answer = answers[0]["answers"][i] + actual_answer.description = new_answer["description"] + actual_answer.is_correct = new_answer["is_correct"] + actual_answer.save() + instance.save() + return instance diff --git a/templates/addQuestionToExistingTest.html b/templates/addQuestionToExistingTest.html new file mode 100644 index 0000000..9bed7dc --- /dev/null +++ b/templates/addQuestionToExistingTest.html @@ -0,0 +1,45 @@ +{% extends "base.html" %} +{% load filters %} +{% load rest_framework %} + + +{#{% block title %}{{ request.GET.name }}{% endblock %}#} + +{% block content %} +
+
+

Dodaj pytanie

+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+{% endblock %} + diff --git a/templates/editQuestion.html b/templates/editQuestion.html new file mode 100644 index 0000000..2d0f6e5 --- /dev/null +++ b/templates/editQuestion.html @@ -0,0 +1,39 @@ +{% extends "base.html" %} +{% load rest_framework %} + +{% block title %}{{ test.name }} - Edytuj{% endblock %} + +{% block additional_head %} + +{% endblock %} + +{% block content %} +
+
+
+
+ + +
+ {% for answer in question.answers.all %} +
+ + +
+ {% endfor %} +
+ + +
+
+
+ +
+
+
+{% endblock %} diff --git a/templates/editTest.html b/templates/editTest.html index 5b87c7d..e373595 100644 --- a/templates/editTest.html +++ b/templates/editTest.html @@ -1,48 +1,116 @@ +{#
#} +{#
#} +{# #} +{# #} +{#
#} +{#
#} + + +{#{% extends "base.html" %}#} +{#{% load rest_framework %}#} +{##} +{#{% block title %}{{ test.name }} - Edytuj{% endblock %}#} +{##} +{#{% block additional_head %}#} +{# #} +{#{% endblock %}#} +{##} +{#{% block content %}#} +{#
#} +{#
#} +{##} +{# {% for question in test.questions.all %}#} +{#
#} +{#
#} +{# #} +{# #} +{#
#} +{# {% for answer in question.answers.all %}#} +{#
#} +{# #} +{# #} +{#
#} +{# {% endfor %}#} +{#
#} +{# #} +{# #} +{#
#} +{#
#} +{# {% endfor %}#} +{#
#} +{# #} +{# Dodaj pytanie#} +{# Usun pytanie#} +{#
#} +{#
#} +{#
#} +{#{% endblock %}#} +{##} + + {% extends "base.html" %} {% load rest_framework %} -{% block title %}{{ test.name }} - Edytuj{% endblock %} +{% block title %}{{ test.name }}{% endblock %} {% block additional_head %} + + {% endblock %} {% block content %} -
-
+
+
+
- {% for question in test.questions.all %} -
-
- - + + {% for question in test.questions.all %} +
+ {{ question.description }} +
- {% for answer in question.answers.all %} -
- - -
- {% endfor %} -
- - +
+ {% for answer in question.answers.all %} + + {% endfor %}
+ {% endfor %} + - {% endfor %} -
- -
- -
-{% endblock %} + +
+
+ +{% endblock %} diff --git a/templates/removeQuestionFromExistingTest.html b/templates/removeQuestionFromExistingTest.html new file mode 100644 index 0000000..9febc6c --- /dev/null +++ b/templates/removeQuestionFromExistingTest.html @@ -0,0 +1,53 @@ +{% extends "base.html" %} +{% load rest_framework %} + +{% block title %}{{ test.name }}{% endblock %} + +{% block additional_head %} + + + +{% endblock %} + +{% block content %} +
+
+
+ Usuń pytanie +
+
+ {% for question in test.questions.all %} +
+ {{ question.description }} + +
+
+ {% for answer in question.answers.all %} + + {% endfor %} +
+ {% endfor %} +
+ +
+
+
+ +
+ +{% endblock %} diff --git a/trials/urls.py b/trials/urls.py index 60d6d00..529d778 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, EditTestTemplateView, deleteTest +from trials.views import TestResultView, addTest, addQuestions, myTests, editTest, solvedTests, EditTestTemplateView, deleteTest, AddQuestionToExistingTest, RemoveQuestionFromExistingTest, EditQuestionTemplateView, editName router = DefaultRouter(trailing_slash=False) router.register("items", TestModelViewSet) @@ -14,7 +14,11 @@ urlpatterns = [ path('/mark', TestValidateAPIView.as_view()), path('/result', TestResultView.as_view()), path('/edit', EditTestTemplateView.as_view()), - path('/remove', deleteTest, name = "deleteTest"), + path('/add-question', AddQuestionToExistingTest.as_view()), + path('/remove-question', RemoveQuestionFromExistingTest.as_view()), + path('question//edit', EditQuestionTemplateView.as_view()), + path('/editName', editName, name="editName"), + path('/remove', deleteTest, name="deleteTest"), # path('delete', deleteTest, name="deleteTest"), path('add/test', addTest, name="newTest"), path('add/questions', addQuestions, name="addQuestions"), diff --git a/trials/views.py b/trials/views.py index e934168..6749aa3 100644 --- a/trials/views.py +++ b/trials/views.py @@ -7,6 +7,7 @@ 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 @@ -73,6 +74,67 @@ def addQuestions(request, **kwargs): 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 @@ -112,18 +174,8 @@ def editTest(request): 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() @@ -134,36 +186,74 @@ class EditTestTemplateView(TemplateView): 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 post(self, request, *args, **kwargs): + return redirect(f'/tests/question/{request.POST["id"]}/edit?test_id={kwargs["test_id"]}') - 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 + +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): - 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 + question_id = kwargs["question_id"] + test = request.GET.get("test_id") + answers = [] + answer1 = { + "description": dict(request.POST)["ans1"][0], + "is_correct": False } - template_name = "result.html" - template = get_template(template_name) - return HttpResponse(template.render(context)) + 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_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()