diff --git a/categories/views.py b/categories/views.py index e975457..4ec28df 100644 --- a/categories/views.py +++ b/categories/views.py @@ -16,5 +16,5 @@ class CategoryModelViewSet(viewsets.ModelViewSet): def category(request, category_name): context = {} context['name'] = category_name - context['tests'] = Test.objects.filter(category=category_name) + context['tests'] = Test.objects.filter(category=category_name, visible=True) return render(request, 'category.html', context) \ No newline at end of file diff --git a/config/views.py b/config/views.py index aa730a6..a6157b1 100644 --- a/config/views.py +++ b/config/views.py @@ -9,7 +9,8 @@ def home(request): context = {} # TODO replace #context['tests'] = Test.objects.filter(owner=request.user) - context['tests'] = Test.objects.all + #context['tests'] = Test.objects.all + context['tests'] = Test.objects.filter(visible=True) return render(request, 'home.html', context) diff --git a/templates/createTest.html b/templates/createTest.html index a9d0ac3..8a08049 100644 --- a/templates/createTest.html +++ b/templates/createTest.html @@ -11,6 +11,13 @@

+ + +
+
+ + + +
{% for question in test.questions.all %} diff --git a/trials/managers.py b/trials/managers.py index 378fd4c..7939799 100644 --- a/trials/managers.py +++ b/trials/managers.py @@ -20,7 +20,8 @@ class TestManager(Manager): name=kwargs.get("name"), passing_score=kwargs.get("passing_score"), category=category, - created_by=kwargs.get("user") + created_by=kwargs.get("user"), + visible=kwargs.get("visible") ) for question in questions: question_instance = Question.objects.create( diff --git a/trials/migrations/0012_test_unvisible.py b/trials/migrations/0012_test_unvisible.py new file mode 100644 index 0000000..4a5e364 --- /dev/null +++ b/trials/migrations/0012_test_unvisible.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-03-30 19:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('trials', '0011_auto_20220123_1633'), + ] + + operations = [ + migrations.AddField( + model_name='test', + name='unvisible', + field=models.BooleanField(default=False), + ), + ] diff --git a/trials/migrations/0013_rename_unvisible_test_invisible.py b/trials/migrations/0013_rename_unvisible_test_invisible.py new file mode 100644 index 0000000..1ceede4 --- /dev/null +++ b/trials/migrations/0013_rename_unvisible_test_invisible.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-03-30 20:42 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('trials', '0012_test_unvisible'), + ] + + operations = [ + migrations.RenameField( + model_name='test', + old_name='unvisible', + new_name='invisible', + ), + ] diff --git a/trials/migrations/0014_auto_20220409_1224.py b/trials/migrations/0014_auto_20220409_1224.py new file mode 100644 index 0000000..4b5cc8b --- /dev/null +++ b/trials/migrations/0014_auto_20220409_1224.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.9 on 2022-04-09 10:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('trials', '0013_rename_unvisible_test_invisible'), + ] + + operations = [ + migrations.RemoveField( + model_name='test', + name='invisible', + ), + migrations.AddField( + model_name='test', + name='visible', + field=models.BooleanField(default=True), + ), + ] diff --git a/trials/models.py b/trials/models.py index e0741c6..5c990b1 100644 --- a/trials/models.py +++ b/trials/models.py @@ -14,6 +14,7 @@ class Test(models.Model): # related_name="tests" # ) category = models.CharField(max_length=100) + visible = models.BooleanField(default=True) created_by = models.ForeignKey( "users.User", null=True, diff --git a/trials/serializers.py b/trials/serializers.py index eb149d2..fa5683c 100644 --- a/trials/serializers.py +++ b/trials/serializers.py @@ -17,6 +17,7 @@ class TestSerializer(serializers.ModelSerializer): "name", "passing_score", "questions", + "visible", "category", ) diff --git a/trials/urls.py b/trials/urls.py index 529d778..f9c3adf 100644 --- a/trials/urls.py +++ b/trials/urls.py @@ -18,6 +18,7 @@ urlpatterns = [ path('/remove-question', RemoveQuestionFromExistingTest.as_view()), path('question//edit', EditQuestionTemplateView.as_view()), path('/editName', editName, name="editName"), + path('/editVisible', editName, name="editVisible"), path('/remove', deleteTest, name="deleteTest"), # path('delete', deleteTest, name="deleteTest"), path('add/test', addTest, name="newTest"), diff --git a/trials/views.py b/trials/views.py index 3a490d3..23d7bbe 100644 --- a/trials/views.py +++ b/trials/views.py @@ -24,9 +24,17 @@ def addTest(request): return render(request, 'createTest.html') +def is_visible(visible): + if visible =="public": + return True + else: + return False + + def addQuestions(request, **kwargs): if request.POST: name = request.GET.get("name") + visible = is_visible(request.GET.get("visible")) passing = request.GET.get("passing") category = request.GET.get("category") number_of_questions = request.GET.get("number_of_questions") @@ -69,7 +77,8 @@ def addQuestions(request, **kwargs): answer4 ] }) - Test.objects.create(name=name, passing_score=passing, category=category, questions=questions, user=user) + Test.objects.create(name=name, passing_score=passing, category=category, questions=questions, user=user, + visible=visible) return redirect('home') return render(request, 'addQuestions.html') @@ -113,7 +122,8 @@ class AddQuestionToExistingTest(TemplateView): ] }) description = dict(request.POST)["desc"][0] - Question.objects.addQuestionToExistingTest(name=description, answers=answers, test=Test.objects.get(id=kwargs["test_id"])) + Question.objects.addQuestionToExistingTest(name=description, answers=answers, + test=Test.objects.get(id=kwargs["test_id"])) return redirect(f'/tests/{kwargs["test_id"]}/edit') @@ -134,7 +144,6 @@ class RemoveQuestionFromExistingTest(TemplateView): return redirect(f'/tests/{kwargs["test_id"]}/edit') - def myTests(request): context = {} user = request.user.id @@ -174,7 +183,6 @@ def editTest(request): class EditTestTemplateView(TemplateView): - template_name = settings.BASE_DIR + f"/templates/editTest.html" def get_queryset(self): @@ -247,7 +255,19 @@ def deleteTest(request, test_id): Test.objects.filter(id=test_id).delete() return redirect('myTests') + def editName(request, test_id): + new_visible = request.GET["visible"] + if new_visible == "public": + new_visible = True + else: + new_visible = False + test = Test.objects.get(id=test_id) + test.visible = new_visible + test.save() + return redirect(f'/tests/{test_id}/edit') + +def editVisible(request, test_id): new_name = request.GET["name"] test = Test.objects.get(id=test_id) test.name = new_name @@ -310,12 +330,12 @@ class TestTemplateView(TemplateView): "points": score, "max": max, "passing": test.passing_score, - "percentage": int(score/max * 100) + "percentage": int(score / max * 100) } SolvedTest.objects.create( score=score, max=max, - percentage=int(score/max * 100), + percentage=int(score / max * 100), user=request.user, test=test ) @@ -323,6 +343,7 @@ class TestTemplateView(TemplateView): template = get_template(template_name) return HttpResponse(template.render(context)) + def testView(request): permission_classes = [] template_name = settings.BASE_DIR + f"/templates/generic_test.html" @@ -367,6 +388,7 @@ class TestValidateAPIView(views.APIView): "points": score }) + class TestResultView(TemplateView): permission_classes = [] - template_name = settings.BASE_DIR + f"/templates/result.html" \ No newline at end of file + template_name = settings.BASE_DIR + f"/templates/result.html"