diff --git a/templates/addQuestions.html b/templates/addQuestions.html index 76db180..6bab9da 100644 --- a/templates/addQuestions.html +++ b/templates/addQuestions.html @@ -3,14 +3,14 @@ {% load rest_framework %} -{% block title %}New Test{% endblock %} +{% block title %}{{ request.GET.name }}{% endblock %} {% block content %}

Add questions

- {% for i in number_of_questions|times %} -

Question {{i}}

+ {% for i in request.GET.number_of_questions|times %} +

Question {{forloop.counter}}


diff --git a/templates/createTest.html b/templates/createTest.html index 93e5bb5..7d2ce7a 100644 --- a/templates/createTest.html +++ b/templates/createTest.html @@ -1,8 +1,8 @@ {% extends "base.html" %} +{% load filters %} {% load rest_framework %} {% block title %}New Test{% endblock %} - {% block content %}
@@ -27,15 +27,14 @@

- +



- + Dodaj pytania
{% endblock %} - diff --git a/templates/myTests.html b/templates/myTests.html index 3f1acb2..6a07bc5 100644 --- a/templates/myTests.html +++ b/templates/myTests.html @@ -4,6 +4,7 @@ {% block content %}

Check your tests

+ {{ tests }} {% for test in tests %}
diff --git a/tools/templatetags/filters.py b/tools/templatetags/filters.py index 1e67d58..a56110e 100644 --- a/tools/templatetags/filters.py +++ b/tools/templatetags/filters.py @@ -2,5 +2,6 @@ from django import template register = template.Library() @register.filter(name='times') -def times(number): +def times(number_str): + number = int(number_str) return range(number) \ No newline at end of file diff --git a/trials/managers.py b/trials/managers.py index a8b4335..b0b73df 100644 --- a/trials/managers.py +++ b/trials/managers.py @@ -14,9 +14,7 @@ class TestManager(Manager): ): Question = apps.get_model("questions", "Question") Answer = apps.get_model("answers", "Answer") - category = Category.objects.get_or_create( - name=category["name"] - ) + category = category instance = super().create( name=kwargs.get("name"), @@ -31,8 +29,6 @@ class TestManager(Manager): test=instance ) for answer in question["answers"]: - # import pdb; - # pdb.set_trace() Answer.objects.create( description=answer["description"], is_correct=answer["is_correct"], diff --git a/trials/migrations/0007_solvedtest.py b/trials/migrations/0007_solvedtest.py new file mode 100644 index 0000000..3c36195 --- /dev/null +++ b/trials/migrations/0007_solvedtest.py @@ -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)), + ], + ), + ] diff --git a/trials/models.py b/trials/models.py index 5d378f2..8594e25 100644 --- a/trials/models.py +++ b/trials/models.py @@ -34,3 +34,25 @@ class Test(models.Model): if question.answers.get(id=answer["answer"]).is_correct: points += question.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 + ) \ No newline at end of file diff --git a/trials/views.py b/trials/views.py index a99c76c..9cb7b1a 100644 --- a/trials/views.py +++ b/trials/views.py @@ -4,7 +4,7 @@ from rest_framework import views from rest_framework import viewsets from rest_framework.response import Response -from trials.models import Test +from trials.models import Test, SolvedTest from trials.serializers import TestSerializer from django.conf import settings from django.shortcuts import render, redirect @@ -17,28 +17,21 @@ import requests def addTest(request): - if request.POST: - data = request.POST - return addQuestions(request="GET", data_about_test=data) return render(request, 'createTest.html') -def addQuestions(request, data_about_test): - 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') - +def addQuestions(request, **kwargs): 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): - # TODO implementation context = {} - # context['tests']=Test.objects.filter(category=request.user) - context['tests']=Test.objects.filter(category="Matematyka") - #context['tests'] = Test.objects.all + user_id = request.user.id + solved_tests = SolvedTest.objects.filter(user__id=user_id) + 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) @@ -164,6 +163,11 @@ class TestTemplateView(TemplateView): "status": self.PASSED.get(status, self.UNKNOWN), "points": score } + SolvedTest.objects.create( + score=score, + user=request.user, + test=test + ) template_name = "result.html" template = get_template(template_name) return HttpResponse(template.render(context))