test visibility

This commit is contained in:
Hubert Jankowski 2022-04-09 13:02:21 +02:00
parent 181877a70d
commit d9ad2c2f9d
9 changed files with 51 additions and 21 deletions

View File

@ -16,5 +16,5 @@ class CategoryModelViewSet(viewsets.ModelViewSet):
def category(request, category_name): def category(request, category_name):
context = {} context = {}
context['name'] = category_name context['name'] = category_name
context['tests'] = Test.objects.filter(category=category_name, invisible=False) context['tests'] = Test.objects.filter(category=category_name, visible=True)
return render(request, 'category.html', context) return render(request, 'category.html', context)

View File

@ -10,7 +10,7 @@ def home(request):
# TODO replace # TODO replace
#context['tests'] = Test.objects.filter(owner=request.user) #context['tests'] = Test.objects.filter(owner=request.user)
#context['tests'] = Test.objects.all #context['tests'] = Test.objects.all
context['tests'] = Test.objects.filter(invisible=False) context['tests'] = Test.objects.filter(visible=True)
return render(request, 'home.html', context) return render(request, 'home.html', context)

View File

@ -11,8 +11,11 @@
<input id="name" type="text" name="name" value="Nowy test"> <input id="name" type="text" name="name" value="Nowy test">
<br> <br>
<br> <br>
<label for="invisible">Prywatny</label> <label for="visible">Widzialność: </label>
<input type="checkbox" id="invisible" name="invisible" value="True"> <select name="visible" id="visible">
<option value="public">Publiczny</option>
<option value="private">Prywatny</option>
</select>
<br> <br>
<br> <br>
<label for="category">Kategoria: </label> <label for="category">Kategoria: </label>
@ -39,7 +42,7 @@
<br> <br>
<br> <br>
<button class="defaultButton"> <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">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">Dodaj pytania</a>
</button> </button>
</form> </form>
</div> </div>

View File

@ -88,6 +88,15 @@
<a href='' onclick="this.href='editName?name='+document.getElementById('name').value">Zmien nazwe</a> <a href='' onclick="this.href='editName?name='+document.getElementById('name').value">Zmien nazwe</a>
</button> </button>
</div> </div>
<label for="actual_visible">Aktualna widzialność: {% if test.visible %}Publiczny{% else %}Prywatny{% endif %}</label><br>
<label for="visible">Widzialność: </label>
<select name="visible" id="visible">
<option value="public">Publiczny</option>
<option value="private">Prywatny</option>
</select>
<button class="defaultButton">
<a href='' onclick="this.href='editVisible?visible='+document.getElementById('visible').value">Zmien widzialność testu</a>
</button>
</div> </div>
<form method="post" novalidate> <form method="post" novalidate>
{% for question in test.questions.all %} {% for question in test.questions.all %}

View File

@ -20,7 +20,8 @@ class TestManager(Manager):
name=kwargs.get("name"), name=kwargs.get("name"),
passing_score=kwargs.get("passing_score"), passing_score=kwargs.get("passing_score"),
category=category, category=category,
created_by=kwargs.get("user") created_by=kwargs.get("user"),
visible=kwargs.get("visible")
) )
for question in questions: for question in questions:
question_instance = Question.objects.create( question_instance = Question.objects.create(

View File

@ -14,7 +14,7 @@ class Test(models.Model):
# related_name="tests" # related_name="tests"
# ) # )
category = models.CharField(max_length=100) category = models.CharField(max_length=100)
invisible = models.BooleanField(default=False) visible = models.BooleanField(default=True)
created_by = models.ForeignKey( created_by = models.ForeignKey(
"users.User", "users.User",
null=True, null=True,

View File

@ -17,7 +17,7 @@ class TestSerializer(serializers.ModelSerializer):
"name", "name",
"passing_score", "passing_score",
"questions", "questions",
"invisible", "visible",
"category", "category",
) )

View File

@ -18,6 +18,7 @@ urlpatterns = [
path('<int:test_id>/remove-question', RemoveQuestionFromExistingTest.as_view()), path('<int:test_id>/remove-question', RemoveQuestionFromExistingTest.as_view()),
path('question/<int:question_id>/edit', EditQuestionTemplateView.as_view()), path('question/<int:question_id>/edit', EditQuestionTemplateView.as_view()),
path('<int:test_id>/editName', editName, name="editName"), path('<int:test_id>/editName', editName, name="editName"),
path('<int:test_id>/editVisible', editName, name="editVisible"),
path('<int:test_id>/remove', deleteTest, name="deleteTest"), path('<int:test_id>/remove', deleteTest, name="deleteTest"),
# path('delete', deleteTest, name="deleteTest"), # path('delete', deleteTest, name="deleteTest"),
path('add/test', addTest, name="newTest"), path('add/test', addTest, name="newTest"),

View File

@ -24,10 +24,17 @@ def addTest(request):
return render(request, 'createTest.html') return render(request, 'createTest.html')
def is_visible(visible):
if visible =="public":
return True
else:
return False
def addQuestions(request, **kwargs): def addQuestions(request, **kwargs):
if request.POST: if request.POST:
name = request.GET.get("name") name = request.GET.get("name")
invisible = request.GET.get("invisible") visible = is_visible(request.GET.get("visible"))
passing = request.GET.get("passing") passing = request.GET.get("passing")
category = request.GET.get("category") category = request.GET.get("category")
number_of_questions = request.GET.get("number_of_questions") number_of_questions = request.GET.get("number_of_questions")
@ -70,12 +77,8 @@ def addQuestions(request, **kwargs):
answer4 answer4
] ]
}) })
if invisible == "True":
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,
invisible=True) visible=visible)
else:
Test.objects.create(name=name, passing_score=passing, category=category, questions=questions, user=user,
invisible=False)
return redirect('home') return redirect('home')
return render(request, 'addQuestions.html') return render(request, 'addQuestions.html')
@ -119,7 +122,8 @@ class AddQuestionToExistingTest(TemplateView):
] ]
}) })
description = dict(request.POST)["desc"][0] 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') return redirect(f'/tests/{kwargs["test_id"]}/edit')
@ -140,7 +144,6 @@ class RemoveQuestionFromExistingTest(TemplateView):
return redirect(f'/tests/{kwargs["test_id"]}/edit') return redirect(f'/tests/{kwargs["test_id"]}/edit')
def myTests(request): def myTests(request):
context = {} context = {}
user = request.user.id user = request.user.id
@ -180,7 +183,6 @@ def editTest(request):
class EditTestTemplateView(TemplateView): class EditTestTemplateView(TemplateView):
template_name = settings.BASE_DIR + f"/templates/editTest.html" template_name = settings.BASE_DIR + f"/templates/editTest.html"
def get_queryset(self): def get_queryset(self):
@ -253,7 +255,19 @@ def deleteTest(request, test_id):
Test.objects.filter(id=test_id).delete() Test.objects.filter(id=test_id).delete()
return redirect('myTests') return redirect('myTests')
def editName(request, test_id): 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"] new_name = request.GET["name"]
test = Test.objects.get(id=test_id) test = Test.objects.get(id=test_id)
test.name = new_name test.name = new_name
@ -316,12 +330,12 @@ class TestTemplateView(TemplateView):
"points": score, "points": score,
"max": max, "max": max,
"passing": test.passing_score, "passing": test.passing_score,
"percentage": int(score/max * 100) "percentage": int(score / max * 100)
} }
SolvedTest.objects.create( SolvedTest.objects.create(
score=score, score=score,
max=max, max=max,
percentage=int(score/max * 100), percentage=int(score / max * 100),
user=request.user, user=request.user,
test=test test=test
) )
@ -329,6 +343,7 @@ class TestTemplateView(TemplateView):
template = get_template(template_name) template = get_template(template_name)
return HttpResponse(template.render(context)) return HttpResponse(template.render(context))
def testView(request): def testView(request):
permission_classes = [] permission_classes = []
template_name = settings.BASE_DIR + f"/templates/generic_test.html" template_name = settings.BASE_DIR + f"/templates/generic_test.html"
@ -373,6 +388,7 @@ class TestValidateAPIView(views.APIView):
"points": score "points": score
}) })
class TestResultView(TemplateView): class TestResultView(TemplateView):
permission_classes = [] permission_classes = []
template_name = settings.BASE_DIR + f"/templates/result.html" template_name = settings.BASE_DIR + f"/templates/result.html"