fix
[wolnelektury.git] / src / search / forms.py
index f3bf0c0..176c73e 100644 (file)
@@ -3,7 +3,7 @@
 #
 from django.apps import apps
 from django.conf import settings
-from django.contrib.postgres.search import SearchHeadline, SearchRank, SearchQuery
+from django.contrib.postgres.search import SearchHeadline, SearchQuery
 from django import forms
 from django.utils.translation import gettext_lazy as _
 from catalogue.constants import LANGUAGES_3TO2
@@ -85,9 +85,9 @@ class SearchFilters(forms.Form):
             'theme': catalogue.models.Tag.objects.filter(category='theme'),
             'genre': catalogue.models.Tag.objects.filter(category='genre'),
             'collection': catalogue.models.Collection.objects.all(),
-            'book': catalogue.models.Book.objects.all(), #findable
+            'book': catalogue.models.Book.objects.filter(findable=True),
             'pdbook': pdcounter.models.BookStub.objects.all(),
-            'snippet': catalogue.models.Snippet.objects.all(),
+            'snippet': catalogue.models.Snippet.objects.filter(book__findable=True),
             'art': picture.models.Picture.objects.all(),
             # art pieces
         }
@@ -104,14 +104,14 @@ class SearchFilters(forms.Form):
                 qs['pdbook'] = qs['pdbook'].none()
             if c != 'quote': qs['snippet'] = qs['snippet'].none()
             if c != 'art': qs['art'] = qs['art'].none()
-            qs['art'] = Picture.objects.none()
+            qs['art'] = picture.models.Picture.objects.none()
 
         if self.cleaned_data['format']:
             c = self.cleaned_data['format']
             qs['author'] = qs['author'].none()
             qs['pdauthor'] = qs['pdauthor'].none()
             qs['theme'] = qs['theme'].none()
-            qs['genre'] = qs['genrer'].none()
+            qs['genre'] = qs['genre'].none()
             qs['collection'] = qs['collection'].none()
             if c == 'art':
                 qs['book'] = qs['book'].none()
@@ -163,20 +163,17 @@ class SearchFilters(forms.Form):
         books = qs['book'].annotate(
             search_vector=UnaccentSearchVector('title')
         ).filter(search_vector=squery)
-        books = books.exclude(ancestor__in=books)
+        books = books.exclude(ancestor__in=books).order_by('-popularity__count')
 
-        snippets = qs['snippet'].annotate(
-                    rank=SearchRank('search_vector', squery)
-                ).filter(rank__gt=0).order_by('-rank').annotate(
+        snippets = qs['snippet'].filter(search_vector=squery).annotate(
                     headline=SearchHeadline(
                         'text',
                         query,
                         config='polish',
                         start_sel='<strong>',
                         stop_sel='</strong>',
-                        highlight_all=True
                     )
-                )[:100]
+                ).order_by('-book__popularity__count', 'sec')[:100]
         snippets_by_book = {}
         for snippet in snippets:
             snippet_list = snippets_by_book.setdefault(snippet.book, [])