From e421a2b4cdfd7b4dbc4401f95acd0019a265948d Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 12 Dec 2018 15:31:22 +0100 Subject: [PATCH] forum: increase search speed by optimizing permission filter --- core/views/__init__.py | 1 - forum/views.py | 22 ++++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/views/__init__.py b/core/views/__init__.py index 4b690c4c..2d764237 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -43,7 +43,6 @@ from django.db.models import Count from core.models import Group from core.views.forms import LoginForm -from haystack.query import SearchQuerySet def forbidden(request): diff --git a/forum/views.py b/forum/views.py index 32dab177..6289c42f 100644 --- a/forum/views.py +++ b/forum/views.py @@ -56,10 +56,24 @@ class ForumSearchView(ListView): query = self.request.GET.get("query", "") if query == "": return [] - queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query)[:100] - return [ - r.object for r in queryset if can_view(r.object.topic, self.request.user) - ][:30] + queryset = ( + SearchQuerySet().models(ForumMessage).autocomplete(auto=query).load_all() + ) + + # Filter unauthorized responses + resp = [] + count = 0 + max_count = 30 + for r in queryset: + if count >= max_count: + return resp + if can_view(r.object, self.request.user) and can_view( + r.object.topic, self.request.user + ): + resp.append(r.object) + count += 1 + + return resp class ForumMainView(ListView):