Add search to internal problem

This commit is contained in:
cuom1999 2023-08-29 18:52:24 -05:00
parent 20a3a61206
commit accf586413
2 changed files with 19 additions and 7 deletions

View file

@ -3,7 +3,7 @@ import json
from django.views.generic import ListView from django.views.generic import ListView
from django.utils.translation import gettext as _, gettext_lazy 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.http import HttpResponseForbidden
from django.urls import reverse from django.urls import reverse
@ -38,13 +38,19 @@ class InternalProblem(InternalView, ListView):
**kwargs **kwargs
) )
def get_search_query(self):
return self.request.GET.get("q") or self.request.POST.get("q")
def get_queryset(self): def get_queryset(self):
queryset = ( queryset = Problem.objects.annotate(
Problem.objects.annotate(vote_count=Count("volunteer_user_votes")) vote_count=Count("volunteer_user_votes")
.filter(vote_count__gte=1) ).filter(vote_count__gte=1)
.order_by("-vote_count") query = self.get_search_query()
) if query:
return queryset queryset = queryset.filter(
Q(code__icontains=query) | Q(name__icontains=query)
)
return queryset.order_by("-vote_count")
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(InternalProblem, self).get_context_data(**kwargs) context = super(InternalProblem, self).get_context_data(**kwargs)
@ -52,6 +58,7 @@ class InternalProblem(InternalView, ListView):
context["title"] = self.title context["title"] = self.title
context["page_prefix"] = self.request.path + "?page=" context["page_prefix"] = self.request.path + "?page="
context["first_page_href"] = self.request.path context["first_page_href"] = self.request.path
context["query"] = self.get_search_query()
return context return context

View file

@ -29,6 +29,11 @@
{% endblock %} {% endblock %}
{% block middle_content %} {% block middle_content %}
<form>
<label for="name">{{_('Search')}}:</label>
<input type="text" name="q" value="{{query}}">
</form>
<br>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>