feature/post-test-answers #23

Merged
s470631 merged 11 commits from feature/post-test-answers into master 2022-01-22 14:19:05 +01:00
4 changed files with 115 additions and 2 deletions
Showing only changes of commit 33317a84d2 - Show all commits

View File

@ -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;
}

48
templates/editTest.html Normal file
View File

@ -0,0 +1,48 @@
{% extends "base.html" %}
{% load rest_framework %}
{% block title %}{{ test.name }} - Edit{% endblock %}
{% block additional_head %}
<meta charset="UTF-8">
{% endblock %}
{% block content %}
<div class="editContainer">
<form method="post" novalidate>
<div class="editContainerSection">
<div class="editContainerLine">
<label for="name"><h2>Name:</h2></label>
<input id="name" type="text" name="name" value="New test">
</div>
</div>
{% for question in test.questions.all %}
<div class="editContainerSection">
<div class="editContainerLine">
<label for="desc"><b>Description:</b></label>
<input id="desc" type="text" name="desc", value="{{ question.description }}">
</div>
{% for answer in question.answers.all %}
<div class="editContainerLine">
<label for="ans-{{ forloop.counter }}">Answer {{ forloop.counter }}: </label>
<input id="ans-{{ forloop.counter }}" type="text" name="ans-{{ forloop.counter }}", value="{{ answer.description }}">
</div>
{% endfor %}
<div class="editContainerLine">
<label for="category">Correct: </label>
<select name="category" id="category">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
{% endfor %}
<div class="testContent">
<input type="submit" value="Edit test">
</div>
</form>
</div>
{% endblock %}

View File

@ -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('<int:test_id>/show', TestTemplateView.as_view()),
path('<int:test_id>/mark', TestValidateAPIView.as_view()),
path('<int:test_id>/result', TestResultView.as_view()),
path('<int:test_id>/edit', editTest),
path('<int:test_id>/edit', EditTestTemplateView.as_view()),
path('add/test', addTest, name="newTest"),
path('add/questions', addQuestions, name="addQuestions"),
path('my', myTests, name="myTests"),

View File

@ -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()