Add my solved test and create test in progress

This commit is contained in:
Hubert Jankowski 2022-01-22 19:55:25 +01:00
parent 33317a84d2
commit 773dd783ae
8 changed files with 83 additions and 36 deletions

View File

@ -3,14 +3,14 @@
{% load rest_framework %} {% load rest_framework %}
{% block title %}New Test{% endblock %} {% block title %}{{ request.GET.name }}{% endblock %}
{% block content %} {% block content %}
<div class="newContainer"> <div class="newContainer">
<form method="post"> <form method="post">
<h1>Add questions</h1> <h1>Add questions</h1>
{% for i in number_of_questions|times %} {% for i in request.GET.number_of_questions|times %}
<h2>Question {{i}}</h2> <h2>Question {{forloop.counter}}</h2>
<label for="desc">Description: </label> <label for="desc">Description: </label>
<input id="desc" type="text" name="desc"> <input id="desc" type="text" name="desc">
<br> <br>

View File

@ -1,8 +1,8 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load filters %}
{% load rest_framework %} {% load rest_framework %}
{% block title %}New Test{% endblock %} {% block title %}New Test{% endblock %}
{% block content %} {% block content %}
<div class="newContainer"> <div class="newContainer">
<form method="post"> <form method="post">
@ -27,15 +27,14 @@
<br> <br>
<br> <br>
<label for="questions">Number of questions: </label> <label for="questions">Number of questions: </label>
<input id="questions" type="number" name="questions" value="10"> <input id="questions" type="number" name="questions" value="2">
<br> <br>
<br> <br>
<label for="passing">Passing score: </label> <label for="passing">Passing score: </label>
<input id="passing" type="number" name="passing" value="5"> <input id="passing" type="number" name="passing" value="5">
<br> <br>
<br> <br>
<input type="submit" value="Add questions"> <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>
</form> </form>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -4,6 +4,7 @@
{% block content %} {% block content %}
<h1>Check your tests</h1> <h1>Check your tests</h1>
{{ tests }}
{% for test in tests %} {% for test in tests %}
<div class="mainTestContainer"> <div class="mainTestContainer">
<div class="mainTestName"> <div class="mainTestName">

View File

@ -2,5 +2,6 @@ from django import template
register = template.Library() register = template.Library()
@register.filter(name='times') @register.filter(name='times')
def times(number): def times(number_str):
number = int(number_str)
return range(number) return range(number)

View File

@ -14,9 +14,7 @@ class TestManager(Manager):
): ):
Question = apps.get_model("questions", "Question") Question = apps.get_model("questions", "Question")
Answer = apps.get_model("answers", "Answer") Answer = apps.get_model("answers", "Answer")
category = Category.objects.get_or_create( category = category
name=category["name"]
)
instance = super().create( instance = super().create(
name=kwargs.get("name"), name=kwargs.get("name"),
@ -31,8 +29,6 @@ class TestManager(Manager):
test=instance test=instance
) )
for answer in question["answers"]: for answer in question["answers"]:
# import pdb;
# pdb.set_trace()
Answer.objects.create( Answer.objects.create(
description=answer["description"], description=answer["description"],
is_correct=answer["is_correct"], is_correct=answer["is_correct"],

View File

@ -0,0 +1,24 @@
# Generated by Django 3.2.9 on 2022-01-22 18:28
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('trials', '0006_alter_test_category'),
]
operations = [
migrations.CreateModel(
name='SolvedTest',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('score', models.IntegerField(default=0)),
('test', models.ManyToManyField(related_name='answers', to='trials.Test')),
('user', models.ManyToManyField(related_name='answers', to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -34,3 +34,25 @@ class Test(models.Model):
if question.answers.get(id=answer["answer"]).is_correct: if question.answers.get(id=answer["answer"]).is_correct:
points += question.points points += question.points
return points return points
def name_and_passing_score(self):
return {
"name": self.name,
"passing_score": self.passing_score
}
class SolvedTest(models.Model):
score = models.IntegerField(default=0)
user = models.ForeignKey(
"users.User",
null=True,
related_name="answers",
on_delete=models.CASCADE
)
test = models.ForeignKey(
"Test",
null=True,
related_name="answers",
on_delete=models.CASCADE
)

View File

@ -4,7 +4,7 @@ from rest_framework import views
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.response import Response from rest_framework.response import Response
from trials.models import Test from trials.models import Test, SolvedTest
from trials.serializers import TestSerializer from trials.serializers import TestSerializer
from django.conf import settings from django.conf import settings
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
@ -17,28 +17,21 @@ import requests
def addTest(request): def addTest(request):
if request.POST:
data = request.POST
return addQuestions(request="GET", data_about_test=data)
return render(request, 'createTest.html') return render(request, 'createTest.html')
def addQuestions(request, data_about_test): def addQuestions(request, **kwargs):
number_of_question = 0
if request == "GET":
for key, value in data_about_test.items():
if key == "questions":
number_of_question = int(value)
context = {
"number_of_questions": number_of_question
}
template_name = "addQuestions.html"
template = get_template(template_name)
return HttpResponse(template.render(context))
# return render(response, 'addQuestions.html')
if request.POST: if request.POST:
pass name = request.GET.get("name")
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()
return render(request, 'addQuestions.html')
@ -52,11 +45,17 @@ def myTests(request):
def solvedTests(request): def solvedTests(request):
# TODO implementation
context = {} context = {}
# context['tests']=Test.objects.filter(category=request.user) user_id = request.user.id
context['tests']=Test.objects.filter(category="Matematyka") solved_tests = SolvedTest.objects.filter(user__id=user_id)
#context['tests'] = Test.objects.all formatted_tests = list()
for solved_test in solved_tests:
formatted_tests.append({
"name": solved_test.test.name_and_passing_score()["name"],
"passing_score": solved_test.test.name_and_passing_score()["passing_score"],
"score": solved_test.score
})
context['tests'] = formatted_tests
return render(request, 'myTests.html', context) return render(request, 'myTests.html', context)
@ -164,6 +163,11 @@ class TestTemplateView(TemplateView):
"status": self.PASSED.get(status, self.UNKNOWN), "status": self.PASSED.get(status, self.UNKNOWN),
"points": score "points": score
} }
SolvedTest.objects.create(
score=score,
user=request.user,
test=test
)
template_name = "result.html" template_name = "result.html"
template = get_template(template_name) template = get_template(template_name)
return HttpResponse(template.render(context)) return HttpResponse(template.render(context))