Fixed the error of saving the test result after each page refresh

This commit is contained in:
Piotr Kopycki 2022-04-15 16:59:14 +02:00
parent dead795e03
commit a0e0ff7e03
2 changed files with 25 additions and 21 deletions

View File

@ -9,25 +9,25 @@
<div class="resultBody"> <div class="resultBody">
<h5 class="resultMsg"> <h5 class="resultMsg">
<!-- Quite good! All the best for next quiz!--> <!-- Quite good! All the best for next quiz!-->
{% if percentage == 100 %} {% if request.session.percentage == 100 %}
Idealnie, widzę że ten temat nie ma dla ciebie tajemnic! Idealnie, widzę że ten temat nie ma dla ciebie tajemnic!
{% elif percentage >= 75 %} {% elif request.session.percentage >= 75 %}
Bardzo dobrze, ale są jeszcze pewne braki ;) Bardzo dobrze, ale są jeszcze pewne braki ;)
{% elif percentage >= 50 %} {% elif request.session.percentage >= 50 %}
Nie jest źle, wiedziałeś więcej niż mniej Nie jest źle, wiedziałeś więcej niż mniej
{% elif percentage >= 25 %} {% elif request.session.percentage >= 25 %}
Masz spore braki, powinieneś trochę więcej się pouczyć Masz spore braki, powinieneś trochę więcej się pouczyć
{% else %} {% else %}
Słabiutko, ale następnym razem będzie lepiej Słabiutko, ale następnym razem będzie lepiej
{% endif %} {% endif %}
</h5> </h5>
<h5 class="resultScore">Rezultat: {{ status }}</h5> <h5 class="resultScore">Rezultat: {{ request.session.status }}</h5>
<h5 class="resultText">Twój wynik: {{ points }}</h5> <h5 class="resultText">Twój wynik: {{ request.session.points }}</h5>
<h5 class="resultText">Próg zaliczenia: {{ passing }}</h5> <h5 class="resultText">Próg zaliczenia: {{ request.session.passing }}</h5>
<h5 class="resultText">Maksymalny wynik: {{ max }}</h5> <h5 class="resultText">Maksymalny wynik: {{ request.session.max }}</h5>
<h5 class="resultText">Wynik procentowy: {{ percentage }}%</h5> <h5 class="resultText">Wynik procentowy: {{ request.session.percentage }}%</h5>
<button class="defaultButton"><a href="{% url 'home' %}">Strona główna</a></button> <button class="defaultButton"><a href="{% url 'home' %}">Strona główna</a></button>
{% if password == "" %} {% if request.session.password == "" %}
<br> <br>
<h5>Udostępnij:</h5> <h5>Udostępnij:</h5>
{% post_to_facebook object_or_url %} {% post_to_facebook object_or_url %}

View File

@ -336,14 +336,20 @@ class TestTemplateView(TemplateView):
score = self.get_score(test, self.formatted_responses(request.POST)) score = self.get_score(test, self.formatted_responses(request.POST))
max = self.get_maxscore(test, self.formatted_responses(request.POST)) max = self.get_maxscore(test, self.formatted_responses(request.POST))
status = score >= test.passing_score status = score >= test.passing_score
context = { # context = {
"status": self.PASSED.get(status, self.UNKNOWN), # "status": self.PASSED.get(status, self.UNKNOWN),
"points": score, # "points": score,
"max": max, # "max": max,
"passing": test.passing_score, # "passing": test.passing_score,
"percentage": int(score / max * 100), # "percentage": int(score / max * 100),
"password": test.password # "password": test.password
} # }
request.session["status"] = self.PASSED.get(status, self.UNKNOWN)
request.session["points"] = score
request.session["max"] = max
request.session["passing"] = test.passing_score
request.session["percentage"] = int(score / max * 100)
request.session["password"] = test.password
SolvedTest.objects.create( SolvedTest.objects.create(
score=score, score=score,
max=max, max=max,
@ -351,9 +357,7 @@ class TestTemplateView(TemplateView):
user=request.user, user=request.user,
test=test test=test
) )
template_name = "result.html" return HttpResponseRedirect(f'result')
template = get_template(template_name)
return HttpResponse(template.render(context, request))
def TestPasswordTemplateView(request, test_id): def TestPasswordTemplateView(request, test_id):