diff --git a/django/core/urls.py b/django/core/urls.py index bd7ee73..fac43b9 100644 --- a/django/core/urls.py +++ b/django/core/urls.py @@ -25,7 +25,7 @@ urlpatterns = [ path('', include('homepage.urls')), path('play/', include('play.urls')), path('library/', include('library.urls')), - path('components/', include('components.urls')) + path('components/', include('components.urls')), ] if settings.DEBUG: diff --git a/django/library/forms.py b/django/library/forms.py index fb55f92..f2bd3b9 100644 --- a/django/library/forms.py +++ b/django/library/forms.py @@ -15,7 +15,7 @@ from django import forms class QuizFilterForm(forms.Form): search = forms.CharField(required=False, label="Suche", widget=forms.TextInput(attrs={ - 'class': 'flex flex-grow rounded-lg p-2' ,'placeholder': 'Suche ...', + 'class': 'flex flex-grow rounded-lg p-2' ,'placeholder': 'Suche ...', 'list': 'quiz-names', })) diff --git a/django/library/urls.py b/django/library/urls.py index 1721a2b..6c8a275 100644 --- a/django/library/urls.py +++ b/django/library/urls.py @@ -16,5 +16,6 @@ urlpatterns = [ path('question/delete//', views.delete_question, name='delete_question'), path('detail/copy//', views.copy_quiz, name='copy_quiz'), path('favorite_quiz//', views.favorite_quiz, name='favorite_quiz'), + path("quiz-names-json/", views.quiz_names_json, name="quiz_names_json"), ] diff --git a/django/library/views.py b/django/library/views.py index 343c49c..529c2d3 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -1,6 +1,6 @@ from django.utils import timezone import uuid -from django.http import HttpResponse, HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User @@ -16,6 +16,18 @@ from django.db.models import Count from django.contrib.auth.models import User # Übersicht aller Quizze + +def quiz_names_json(request): + form = QuizFilterForm(request.GET) + if form.is_valid(): + search = form.cleaned_data.get('search') + names = list( + QivipQuiz.objects.all().annotate(max_amout_questions=Count('questions')).filter(max_amout_questions__gte=1, status='öffentlich', name__icontains=search) + .values_list('name', flat=True) + .distinct() +) + return JsonResponse(names, safe=False) + def overview_quiz(request): # Filter form = QuizFilterForm(request.GET) @@ -85,36 +97,6 @@ def overview_quiz(request): except: favorite_quizzes=QivipQuiz.objects.none() - """ - if search: # Suche nach Namen - filter_other_quizzes = filter_other_quizzes.filter(name__icontains=search) - filter_my_quizzes = filter_my_quizzes.filter(name__icontains=search) - - if min_amout_questions: - filter_other_quizzes = filter_other_quizzes.filter(max_amout_questions__gte=min_amout_questions) - filter_my_quizzes = filter_my_quizzes.filter(max_amout_questions__gte=min_amout_questions) if filter_my_quizzes else QivipQuiz.objects.none() - - if max_amout_questions: - filter_other_quizzes = filter_other_quizzes.filter(max_amout_questions__lte=max_amout_questions) - try: - filter_my_quizzes =filter_my_quizzes.filter(max_amout_questions__lte=max_amout_questions) - except: - filter_my_quizzes = QivipQuiz.objects.none() - - if username: - try: - user = User.objects.get(username=username) # Benutzer anhand des Namens holen - filter_other_quizzes = filter_other_quizzes.filter(user_id=user.id) - filter_my_quizzes = filter_my_quizzes.filter(user_id=user.id) # Nach der user_id filtern - except User.DoesNotExist: - filter_other_quizzes = QivipQuiz.objects.none() # Falls User nicht existiert, leere Query zurückgeben - filter_my_quizzes = QivipQuiz.objects.none() - - try: - filter_other_quizzes=filter_other_quizzes.exclude(user_id=request.user) - except: - filter_other_quizzes=filter_other_quizzes - """ # Pagination for my quizzes diff --git a/django/templates/partials/_nav.html b/django/templates/partials/_nav.html index de79de9..0be0706 100644 --- a/django/templates/partials/_nav.html +++ b/django/templates/partials/_nav.html @@ -11,7 +11,7 @@ + + + {% for quiz in quiz_names %} + + + + {% for quiz in quiz_names %} + + {% endfor %} + + + + + + {{ quiz_names|json_script:"quiz-names-data" }} + + + + + diff --git a/django/templates/play/join_game.html b/django/templates/play/join_game.html index 47db8d2..ec409e2 100644 --- a/django/templates/play/join_game.html +++ b/django/templates/play/join_game.html @@ -7,7 +7,7 @@
{% csrf_token %}
- +