feature/visibility #32

Merged
s470631 merged 3 commits from feature/visibility into master 2022-04-09 13:04:09 +02:00
12 changed files with 112 additions and 11 deletions

View File

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

View File

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

View File

@ -11,6 +11,13 @@
<input id="name" type="text" name="name" value="Nowy test">
<br>
<br>
<label for="visible">Widzialność: </label>
<select name="visible" id="visible">
<option value="public">Publiczny</option>
<option value="private">Prywatny</option>
</select>
<br>
<br>
<label for="category">Kategoria: </label>
<select name="category" id="category">
<option value="JezykPolski">Język Polski</option>
@ -35,7 +42,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">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>
</form>
</div>

View File

@ -88,6 +88,15 @@
<a href='' onclick="this.href='editName?name='+document.getElementById('name').value">Zmien nazwe</a>
</button>
</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>
<form method="post" novalidate>
{% for question in test.questions.all %}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,6 +18,7 @@ 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>/remove', deleteTest, name="deleteTest"),
# path('delete', deleteTest, name="deleteTest"),
path('add/test', addTest, name="newTest"),

View File

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