diff --git a/templates/createTest.html b/templates/createTest.html index 8a08049..5ad210a 100644 --- a/templates/createTest.html +++ b/templates/createTest.html @@ -18,6 +18,10 @@

+ + - Jeśli test ma nie posiadać hasła pozostaw puste pole +
+
+
{% for question in test.questions.all %} diff --git a/templates/home.html b/templates/home.html index fde897f..9e53cfc 100644 --- a/templates/home.html +++ b/templates/home.html @@ -17,7 +17,8 @@ - +{# #} +
{% endfor %} diff --git a/templates/testPassword.html b/templates/testPassword.html new file mode 100644 index 0000000..cea905e --- /dev/null +++ b/templates/testPassword.html @@ -0,0 +1,7 @@ + + + +
+ +
+
\ No newline at end of file diff --git a/trials/managers.py b/trials/managers.py index 7939799..97ac3de 100644 --- a/trials/managers.py +++ b/trials/managers.py @@ -21,7 +21,8 @@ class TestManager(Manager): passing_score=kwargs.get("passing_score"), category=category, created_by=kwargs.get("user"), - visible=kwargs.get("visible") + visible=kwargs.get("visible"), + password=kwargs.get("password") ) for question in questions: question_instance = Question.objects.create( diff --git a/trials/migrations/0015_test_password.py b/trials/migrations/0015_test_password.py new file mode 100644 index 0000000..5abe5cc --- /dev/null +++ b/trials/migrations/0015_test_password.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-04-09 11:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('trials', '0014_auto_20220409_1224'), + ] + + operations = [ + migrations.AddField( + model_name='test', + name='password', + field=models.CharField(default='', max_length=100), + ), + ] diff --git a/trials/models.py b/trials/models.py index 5c990b1..e91e07d 100644 --- a/trials/models.py +++ b/trials/models.py @@ -21,6 +21,7 @@ class Test(models.Model): related_name="tests", on_delete=models.CASCADE ) + password = models.CharField(max_length=100, default="") objects = TestManager() diff --git a/trials/urls.py b/trials/urls.py index f9c3adf..44688db 100644 --- a/trials/urls.py +++ b/trials/urls.py @@ -4,13 +4,14 @@ from rest_framework.routers import DefaultRouter from trials.views import TestModelViewSet from trials.views import TestTemplateView from trials.views import TestValidateAPIView -from trials.views import TestResultView, addTest, addQuestions, myTests, editTest, solvedTests, EditTestTemplateView, deleteTest, AddQuestionToExistingTest, RemoveQuestionFromExistingTest, EditQuestionTemplateView, editName +from trials.views import TestResultView, addTest, addQuestions, myTests, editTest, solvedTests, EditTestTemplateView, deleteTest, AddQuestionToExistingTest, RemoveQuestionFromExistingTest, EditQuestionTemplateView, editName, editVisible, editPassword, TestPasswordTemplateView router = DefaultRouter(trailing_slash=False) router.register("items", TestModelViewSet) urlpatterns = [ path('/show', TestTemplateView.as_view()), + path('/password', TestPasswordTemplateView), path('/mark', TestValidateAPIView.as_view()), path('/result', TestResultView.as_view()), path('/edit', EditTestTemplateView.as_view()), @@ -18,7 +19,8 @@ urlpatterns = [ path('/remove-question', RemoveQuestionFromExistingTest.as_view()), path('question//edit', EditQuestionTemplateView.as_view()), path('/editName', editName, name="editName"), - path('/editVisible', editName, name="editVisible"), + path('/editVisible', editVisible, name="editVisible"), + path('/editPassword', editPassword, name="editPassword"), 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 1d0171f..367b717 100644 --- a/trials/views.py +++ b/trials/views.py @@ -38,6 +38,7 @@ def addQuestions(request, **kwargs): passing = request.GET.get("passing") category = request.GET.get("category") number_of_questions = request.GET.get("number_of_questions") + password = request.GET.get("password") user = request.user # k = dict(request.POST)["desc"] questions = [] @@ -78,7 +79,7 @@ def addQuestions(request, **kwargs): ] }) Test.objects.create(name=name, passing_score=passing, category=category, questions=questions, user=user, - visible=visible) + visible=visible, password=password) return redirect('home') return render(request, 'addQuestions.html') @@ -258,7 +259,7 @@ def deleteTest(request, test_id): return redirect('myTests') -def editName(request, test_id): +def editVisible(request, test_id): new_visible = request.GET["visible"] if new_visible == "public": new_visible = True @@ -269,7 +270,7 @@ def editName(request, test_id): test.save() return redirect(f'/tests/{test_id}/edit') -def editVisible(request, test_id): +def editName(request, test_id): new_name = request.GET["name"] test = Test.objects.get(id=test_id) test.name = new_name @@ -277,6 +278,14 @@ def editVisible(request, test_id): return redirect(f'/tests/{test_id}/edit') +def editPassword(request, test_id): + new_password = request.GET["password"] + test = Test.objects.get(id=test_id) + test.password = new_password + test.save() + return redirect(f'/tests/{test_id}/edit') + + class TestModelViewSet(viewsets.ModelViewSet): queryset = Test.objects.all() serializer_class = TestSerializer @@ -346,6 +355,14 @@ class TestTemplateView(TemplateView): return HttpResponse(template.render(context)) +def TestPasswordTemplateView(request, test_id): + if request.POST: + test = Test.objects.get(id=test_id) + if request.POST["password"] == test.password: + return redirect(f'/tests/{test_id}/show') + return render(request, 'testPassword.html') + + def testView(request): permission_classes = [] template_name = settings.BASE_DIR + f"/templates/generic_test.html"