diff --git a/templates/addQuestions.html b/templates/addQuestions.html index 6bab9da..51004a3 100644 --- a/templates/addQuestions.html +++ b/templates/addQuestions.html @@ -31,8 +31,8 @@ - Correct: - + Correct: + 1 2 3 diff --git a/templates/createTest.html b/templates/createTest.html index 27b4d12..7026390 100644 --- a/templates/createTest.html +++ b/templates/createTest.html @@ -35,7 +35,7 @@ - Dodaj pytania + Dodaj pytania diff --git a/trials/managers.py b/trials/managers.py index b0b73df..378fd4c 100644 --- a/trials/managers.py +++ b/trials/managers.py @@ -19,19 +19,19 @@ class TestManager(Manager): instance = super().create( name=kwargs.get("name"), passing_score=kwargs.get("passing_score"), - category=category[0] + category=category, + created_by=kwargs.get("user") ) - # import pdb;pdb.set_trace() for question in questions: question_instance = Question.objects.create( name=question["name"], - description=question["description"], + description=question.get("description", ""), test=instance ) for answer in question["answers"]: Answer.objects.create( description=answer["description"], - is_correct=answer["is_correct"], + is_correct=answer.get("is_correct", False), question=question_instance ) diff --git a/trials/urls.py b/trials/urls.py index ba2ca97..d9c40fc 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 +from trials.views import TestResultView, addTest, addQuestions, myTests, editTest, solvedTests, EditTestTemplateView, deleteTest router = DefaultRouter(trailing_slash=False) router.register("items", TestModelViewSet) @@ -14,6 +14,7 @@ urlpatterns = [ path('/mark', TestValidateAPIView.as_view()), path('/result', TestResultView.as_view()), path('/edit', EditTestTemplateView.as_view()), + path('delete', deleteTest, name="deleteTest"), 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 928e845..d34b65a 100644 --- a/trials/views.py +++ b/trials/views.py @@ -1,8 +1,11 @@ +import pdb + import requests from django.views.generic import TemplateView -from rest_framework import views +from rest_framework import views, generics from rest_framework import viewsets from rest_framework.response import Response +from rest_framework.views import APIView from trials.models import Test, SolvedTest from trials.serializers import TestSerializer @@ -26,21 +29,53 @@ def addQuestions(request, **kwargs): 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() + 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) return render(request, 'addQuestions.html') - - def myTests(request): context = {} - # context['tests']=Test.objects.filter(category=request.user) - context['tests']=Test.objects.filter(category="Matematyka") - #context['tests'] = Test.objects.all + user = request.user.id + context['tests'] = Test.objects.filter(created_by=user) return render(request, 'myTests.html', context) @@ -54,7 +89,6 @@ def solvedTests(request): "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 @@ -124,6 +158,9 @@ class EditTestTemplateView(TemplateView): template = get_template(template_name) return HttpResponse(template.render(context)) +def deleteTest(TemplateView): + pass + class TestModelViewSet(viewsets.ModelViewSet): queryset = Test.objects.all()