From accf5864137aeb18ace364c2b93c5ac8b6aed8eb Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Tue, 29 Aug 2023 18:52:24 -0500 Subject: [PATCH] Add search to internal problem --- judge/views/internal.py | 21 ++++++++++++++------- templates/internal/problem.html | 5 +++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/judge/views/internal.py b/judge/views/internal.py index 4467fc3..eff02a4 100644 --- a/judge/views/internal.py +++ b/judge/views/internal.py @@ -3,7 +3,7 @@ import json from django.views.generic import ListView from django.utils.translation import gettext as _, gettext_lazy -from django.db.models import Count +from django.db.models import Count, Q from django.http import HttpResponseForbidden from django.urls import reverse @@ -38,13 +38,19 @@ class InternalProblem(InternalView, ListView): **kwargs ) + def get_search_query(self): + return self.request.GET.get("q") or self.request.POST.get("q") + def get_queryset(self): - queryset = ( - Problem.objects.annotate(vote_count=Count("volunteer_user_votes")) - .filter(vote_count__gte=1) - .order_by("-vote_count") - ) - return queryset + queryset = Problem.objects.annotate( + vote_count=Count("volunteer_user_votes") + ).filter(vote_count__gte=1) + query = self.get_search_query() + if query: + queryset = queryset.filter( + Q(code__icontains=query) | Q(name__icontains=query) + ) + return queryset.order_by("-vote_count") def get_context_data(self, **kwargs): context = super(InternalProblem, self).get_context_data(**kwargs) @@ -52,6 +58,7 @@ class InternalProblem(InternalView, ListView): context["title"] = self.title context["page_prefix"] = self.request.path + "?page=" context["first_page_href"] = self.request.path + context["query"] = self.get_search_query() return context diff --git a/templates/internal/problem.html b/templates/internal/problem.html index eebb02c..d1147e8 100644 --- a/templates/internal/problem.html +++ b/templates/internal/problem.html @@ -29,6 +29,11 @@ {% endblock %} {% block middle_content %} +
+ + +
+