Merge pull request 'add password to tests' (#34) from feature/set-password-for-test into master

Reviewed-on: #34
This commit is contained in:
s470631 2022-04-10 15:05:48 +02:00
commit 6b02a0bc94
9 changed files with 65 additions and 8 deletions

View File

@ -18,6 +18,10 @@
</select>
<br>
<br>
<label for="password">Hasło: </label>
<input id="password" type="text" name="password" value=""> - Jeśli test ma nie posiadać hasła pozostaw puste pole
<br>
<br>
<label for="category">Kategoria: </label>
<select name="category" id="category">
<option value="JezykPolski">Język Polski</option>
@ -42,7 +46,7 @@
<br>
<br>
<button class="defaultButton">
<a href='' onclick="this.href='questions?passing='+document.getElementById('passing').value + '&name=' +document.getElementById('name').value + '&category='+document.getElementById('category').value+'&number_of_questions='+document.getElementById('questions').value+'&visible='+document.getElementById('visible').value">Dodaj pytania</a>
<a href='' onclick="this.href='questions?passing='+document.getElementById('passing').value + '&name=' +document.getElementById('name').value + '&category='+document.getElementById('category').value+'&number_of_questions='+document.getElementById('questions').value+'&visible='+document.getElementById('visible').value+'&password='+document.getElementById('password').value">Dodaj pytania</a>
</button>
</form>
</div>

View File

@ -97,6 +97,12 @@
<button class="defaultButton">
<a href='' onclick="this.href='editVisible?visible='+document.getElementById('visible').value">Zmien widzialność testu</a>
</button>
<label for="password"><h2>Hasło:</h2></label>
<input id="password" type="text" name="password" value="">
<button class="defaultButton">
<a href='' onclick="this.href='editPassword?password='+document.getElementById('password').value">Zmien hasło</a>
</button>
</div>
<form method="post" novalidate>
{% for question in test.questions.all %}

View File

@ -17,7 +17,8 @@
<!-- <div class="mainTestDesc">-->
<!-- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus volutpat scelerisque tortor, id sodales leo finibus id. Vivamus id viverra nunc, ac faucibus metus. Nulla a mauris imperdiet sapien lobortis dapibus. Quisque ornare posuere pulvinar.-->
<!-- </div>-->
<button><a href="/tests/{{test.id}}/show">Rozwiąż</a></button>
{# <button><a href="/tests/{{test.id}}/show">Rozwiąż</a></button>#}
<button><a href="/tests/{{test.id}}/password">Rozwiąż</a></button>
</div>
<br>
{% endfor %}

View File

@ -0,0 +1,7 @@
<form method="post" novalidate>
<label for="password"><h2>Hasło:</h2></label>
<input id="password" type="text" name="password" value="">
<div class="testContent">
<input type="submit" value="Zatwierdź">
</div>
</form>

View File

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

View File

@ -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),
),
]

View File

@ -21,6 +21,7 @@ class Test(models.Model):
related_name="tests",
on_delete=models.CASCADE
)
password = models.CharField(max_length=100, default="")
objects = TestManager()

View File

@ -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('<int:test_id>/show', TestTemplateView.as_view()),
path('<int:test_id>/password', TestPasswordTemplateView),
path('<int:test_id>/mark', TestValidateAPIView.as_view()),
path('<int:test_id>/result', TestResultView.as_view()),
path('<int:test_id>/edit', EditTestTemplateView.as_view()),
@ -18,7 +19,8 @@ urlpatterns = [
path('<int:test_id>/remove-question', RemoveQuestionFromExistingTest.as_view()),
path('question/<int:question_id>/edit', EditQuestionTemplateView.as_view()),
path('<int:test_id>/editName', editName, name="editName"),
path('<int:test_id>/editVisible', editName, name="editVisible"),
path('<int:test_id>/editVisible', editVisible, name="editVisible"),
path('<int:test_id>/editPassword', editPassword, name="editPassword"),
path('<int:test_id>/remove', deleteTest, name="deleteTest"),
# path('delete', deleteTest, name="deleteTest"),
path('add/test', addTest, name="newTest"),

View File

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