Update search to use listview
Enables pagination
This commit is contained in:
parent
c58ef42bd0
commit
41499f1329
3 changed files with 31 additions and 25 deletions
|
@ -48,8 +48,8 @@
|
|||
accesskey="s">
|
||||
{% translate "Snapshot" %}
|
||||
</a>
|
||||
<form id="search" action="{% url 'search' %}" method="post">
|
||||
{% csrf_token %}
|
||||
<form id="search" action="{% url 'search_post' %}" method="post">
|
||||
{% csrf_token %}
|
||||
<input type="text"
|
||||
name="search"
|
||||
placeholder="{% translate "Search" %}"
|
||||
|
|
|
@ -18,5 +18,6 @@ urlpatterns = [
|
|||
path("snapshot", views.snapshot, name="snapshot"),
|
||||
path("snapshot/<uuid>", views.snapshot, name="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"),
|
||||
]
|
||||
|
|
|
@ -49,7 +49,7 @@ class LogoutView(auth_views.LogoutView):
|
|||
|
||||
|
||||
class TransactionListView(LoginRequiredMixin, ListView):
|
||||
paginate_by = 64
|
||||
paginate_by = 24
|
||||
model = Transaction
|
||||
template_name = "main/transactions.html"
|
||||
context_object_name = "transactions"
|
||||
|
@ -213,25 +213,30 @@ def del_snapshot(request, uuid):
|
|||
@login_required
|
||||
def search(request):
|
||||
_search = request.POST.get("search")
|
||||
_transactions = (
|
||||
Transaction.objects.annotate(
|
||||
rank=SearchRank(
|
||||
SearchVector("name", weight="A")
|
||||
+ SearchVector("description", weight="B")
|
||||
+ SearchVector("trader", weight="B"),
|
||||
SearchQuery(_search, search_type="websearch"),
|
||||
),
|
||||
similarity=TrigramSimilarity("name", _search),
|
||||
)
|
||||
.filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.3))
|
||||
.order_by("-rank", "-date")
|
||||
)
|
||||
return redirect("search", search=_search)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"main/transactions.html",
|
||||
{
|
||||
"transactions": _transactions,
|
||||
"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(
|
||||
rank=SearchRank(
|
||||
SearchVector("name", weight="A")
|
||||
+ SearchVector("description", weight="B")
|
||||
+ SearchVector("trader", weight="B"),
|
||||
SearchQuery(self.search, search_type="websearch"),
|
||||
),
|
||||
similarity=TrigramSimilarity("name", self.search),
|
||||
)
|
||||
.filter(models.Q(rank__gte=0.1) | models.Q(similarity__gte=0.3))
|
||||
.order_by("-rank", "-date")
|
||||
)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]}
|
||||
|
|
Loading…
Reference in a new issue