Merge pull request 'Create test' (#25) from feautre/add-test-json into master

Reviewed-on: #25
This commit is contained in:
s470631 2022-01-24 22:18:17 +01:00
commit b10aec8fc5
5 changed files with 58 additions and 20 deletions

View File

@ -31,8 +31,8 @@
<input id="ans4" type="text" name="ans4"> <input id="ans4" type="text" name="ans4">
<br> <br>
<br> <br>
<label for="category">Correct: </label> <label for="is_correct">Correct: </label>
<select name="category" id="category"> <select name="is_correct" id="is_correct">
<option value="1">1</option> <option value="1">1</option>
<option value="2">2</option> <option value="2">2</option>
<option value="3">3</option> <option value="3">3</option>

View File

@ -35,7 +35,7 @@
<br> <br>
<br> <br>
<button class="defaultButton"> <button class="defaultButton">
<a href='' onclick="this.href='questions?passing='+document.getElementById('passing').value + '&name=' +document.getElementById('name').value + '&catgory='+document.getElementById('category').value+'&number_of_questions='+document.getElementById('questions').value">Dodaj pytania</a> <a href='' onclick="this.href='questions?passing='+document.getElementById('passing').value + '&name=' +document.getElementById('name').value + '&category='+document.getElementById('category').value+'&number_of_questions='+document.getElementById('questions').value">Dodaj pytania</a>
</button> </button>
</form> </form>
</div> </div>

View File

@ -19,19 +19,19 @@ class TestManager(Manager):
instance = super().create( instance = super().create(
name=kwargs.get("name"), name=kwargs.get("name"),
passing_score=kwargs.get("passing_score"), 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: for question in questions:
question_instance = Question.objects.create( question_instance = Question.objects.create(
name=question["name"], name=question["name"],
description=question["description"], description=question.get("description", ""),
test=instance test=instance
) )
for answer in question["answers"]: for answer in question["answers"]:
Answer.objects.create( Answer.objects.create(
description=answer["description"], description=answer["description"],
is_correct=answer["is_correct"], is_correct=answer.get("is_correct", False),
question=question_instance question=question_instance
) )

View File

@ -4,7 +4,7 @@ from rest_framework.routers import DefaultRouter
from trials.views import TestModelViewSet from trials.views import TestModelViewSet
from trials.views import TestTemplateView from trials.views import TestTemplateView
from trials.views import TestValidateAPIView 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 = DefaultRouter(trailing_slash=False)
router.register("items", TestModelViewSet) router.register("items", TestModelViewSet)
@ -14,6 +14,7 @@ urlpatterns = [
path('<int:test_id>/mark', TestValidateAPIView.as_view()), path('<int:test_id>/mark', TestValidateAPIView.as_view()),
path('<int:test_id>/result', TestResultView.as_view()), path('<int:test_id>/result', TestResultView.as_view()),
path('<int:test_id>/edit', EditTestTemplateView.as_view()), path('<int:test_id>/edit', EditTestTemplateView.as_view()),
path('delete', deleteTest, name="deleteTest"),
path('add/test', addTest, name="newTest"), path('add/test', addTest, name="newTest"),
path('add/questions', addQuestions, name="addQuestions"), path('add/questions', addQuestions, name="addQuestions"),
path('my', myTests, name="myTests"), path('my', myTests, name="myTests"),

View File

@ -1,8 +1,11 @@
import pdb
import requests import requests
from django.views.generic import TemplateView 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 import viewsets
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView
from trials.models import Test, SolvedTest from trials.models import Test, SolvedTest
from trials.serializers import TestSerializer from trials.serializers import TestSerializer
@ -26,21 +29,53 @@ def addQuestions(request, **kwargs):
passing = request.GET.get("passing") passing = request.GET.get("passing")
category = request.GET.get("category") category = request.GET.get("category")
number_of_questions = request.GET.get("number_of_questions") number_of_questions = request.GET.get("number_of_questions")
import pdb;pdb.set_trace() user = request.user
for question ,values in request.POST.iteritems(): # k = dict(request.POST)["desc"]
questions = []
for i in range(0, int(number_of_questions)):
import pdb;pdb.set_trace() 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') return render(request, 'addQuestions.html')
def myTests(request): def myTests(request):
context = {} context = {}
# context['tests']=Test.objects.filter(category=request.user) user = request.user.id
context['tests']=Test.objects.filter(category="Matematyka") context['tests'] = Test.objects.filter(created_by=user)
#context['tests'] = Test.objects.all
return render(request, 'myTests.html', context) return render(request, 'myTests.html', context)
@ -54,7 +89,6 @@ def solvedTests(request):
"name": solved_test.test.name_and_passing_score()["name"], "name": solved_test.test.name_and_passing_score()["name"],
"passing_score": solved_test.test.name_and_passing_score()["passing_score"], "passing_score": solved_test.test.name_and_passing_score()["passing_score"],
"score": solved_test.score, "score": solved_test.score,
"max": solved_test.max,
"percentage": solved_test.percentage "percentage": solved_test.percentage
}) })
context['tests'] = formatted_tests context['tests'] = formatted_tests
@ -124,6 +158,9 @@ class EditTestTemplateView(TemplateView):
template = get_template(template_name) template = get_template(template_name)
return HttpResponse(template.render(context)) return HttpResponse(template.render(context))
def deleteTest(TemplateView):
pass
class TestModelViewSet(viewsets.ModelViewSet): class TestModelViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all() queryset = Test.objects.all()