feature/post-test-answers #24

Merged
s470631 merged 4 commits from feature/post-test-answers into master 2022-01-22 20:39:48 +01:00
10 changed files with 168 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

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

@ -0,0 +1,25 @@
# Generated by Django 3.2.9 on 2022-01-22 18:34
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('trials', '0007_solvedtest'),
]
operations = [
migrations.AlterField(
model_name='solvedtest',
name='test',
field=models.ManyToManyField(null=True, related_name='answers', to='trials.Test'),
),
migrations.AlterField(
model_name='solvedtest',
name='user',
field=models.ManyToManyField(null=True, related_name='answers', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -0,0 +1,34 @@
# Generated by Django 3.2.9 on 2022-01-22 18:42
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('trials', '0008_auto_20220122_1934'),
]
operations = [
migrations.RemoveField(
model_name='solvedtest',
name='test',
),
migrations.AddField(
model_name='solvedtest',
name='test',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='answers', to='trials.test'),
),
migrations.RemoveField(
model_name='solvedtest',
name='user',
),
migrations.AddField(
model_name='solvedtest',
name='user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='answers', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -0,0 +1,21 @@
# Generated by Django 3.2.9 on 2022-01-22 18:57
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('trials', '0009_auto_20220122_1942'),
]
operations = [
migrations.AddField(
model_name='test',
name='created_by',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tests', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -12,6 +12,12 @@ class Test(models.Model):
# related_name="tests" # related_name="tests"
# ) # )
category = models.CharField(max_length=100) category = models.CharField(max_length=100)
created_by = models.ForeignKey(
"users.User",
null=True,
related_name="tests",
on_delete=models.CASCADE
)
objects = TestManager() objects = TestManager()
@ -34,3 +40,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))