Update search to use listview

Enables pagination
This commit is contained in:
Edgar P. Burkhart 2022-12-26 18:50:02 +01:00
parent c58ef42bd0
commit 41499f1329
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 31 additions and 25 deletions

View file

@ -48,7 +48,7 @@
accesskey="s"> accesskey="s">
{% translate "Snapshot" %} {% translate "Snapshot" %}
</a> </a>
<form id="search" action="{% url 'search' %}" method="post"> <form id="search" action="{% url 'search_post' %}" method="post">
{% csrf_token %} {% csrf_token %}
<input type="text" <input type="text"
name="search" name="search"

View file

@ -18,5 +18,6 @@ urlpatterns = [
path("snapshot", views.snapshot, name="snapshot"), path("snapshot", views.snapshot, name="snapshot"),
path("snapshot/<uuid>", views.snapshot, name="snapshot"), path("snapshot/<uuid>", views.snapshot, name="snapshot"),
path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"), path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),
path("search", views.search, name="search"), path("search", views.search, name="search_post"),
path("search/<search>", views.SearchView.as_view(), name="search"),
] ]

View file

@ -49,7 +49,7 @@ class LogoutView(auth_views.LogoutView):
class TransactionListView(LoginRequiredMixin, ListView): class TransactionListView(LoginRequiredMixin, ListView):
paginate_by = 64 paginate_by = 24
model = Transaction model = Transaction
template_name = "main/transactions.html" template_name = "main/transactions.html"
context_object_name = "transactions" context_object_name = "transactions"
@ -213,25 +213,30 @@ def del_snapshot(request, uuid):
@login_required @login_required
def search(request): def search(request):
_search = request.POST.get("search") _search = request.POST.get("search")
_transactions = ( return redirect("search", search=_search)
class SearchView(LoginRequiredMixin, ListView):
paginate_by = 24
model = Transaction
template_name = "main/transactions.html"
context_object_name = "transactions"
def get_queryset(self):
self.search = self.kwargs["search"]
return (
Transaction.objects.annotate( Transaction.objects.annotate(
rank=SearchRank( rank=SearchRank(
SearchVector("name", weight="A") SearchVector("name", weight="A")
+ SearchVector("description", weight="B") + SearchVector("description", weight="B")
+ SearchVector("trader", weight="B"), + SearchVector("trader", weight="B"),
SearchQuery(_search, search_type="websearch"), SearchQuery(self.search, search_type="websearch"),
), ),
similarity=TrigramSimilarity("name", _search), similarity=TrigramSimilarity("name", self.search),
) )
.filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.3)) .filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.3))
.order_by("-rank", "-date") .order_by("-rank", "-date")
) )
return render( def get_context_data(self, **kwargs):
request, return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]}
"main/transactions.html",
{
"transactions": _transactions,
"search": _search,
},
)