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">
|
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"
|
||||||
placeholder="{% translate "Search" %}"
|
placeholder="{% translate "Search" %}"
|
||||||
|
|
|
@ -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"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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)
|
||||||
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 render(
|
|
||||||
request,
|
class SearchView(LoginRequiredMixin, ListView):
|
||||||
"main/transactions.html",
|
paginate_by = 24
|
||||||
{
|
model = Transaction
|
||||||
"transactions": _transactions,
|
template_name = "main/transactions.html"
|
||||||
"search": _search,
|
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