revamp search hints
authorJan Szejko <janek37@gmail.com>
Wed, 13 Dec 2017 16:40:21 +0000 (17:40 +0100)
committerJan Szejko <janek37@gmail.com>
Wed, 13 Dec 2017 16:40:21 +0000 (17:40 +0100)
src/search/context_processors.py
src/search/index.py
src/search/views.py
src/wolnelektury/static/js/search.js
src/wolnelektury/static/scss/main/search.scss

index a3f1ea9..6ad2fe9 100644 (file)
@@ -7,4 +7,4 @@ from search.forms import SearchForm
 
 
 def search_form(request):
-    return {'search_form': SearchForm(reverse('search.views.hint'), request.GET)}
+    return {'search_form': SearchForm(reverse('search.views.hint')+'?max=10', request.GET)}
index 7bc61c5..ce60978 100644 (file)
@@ -826,13 +826,9 @@ class Search(SolrIndex):
                 if is_pdcounter:
                     if category == 'pd_author':
                         tag = PDCounterAuthor.objects.get(id=doc.get('tag_id'))
-                    elif category == 'pd_book':
+                    else:  # category == 'pd_book':
                         tag = PDCounterBook.objects.get(id=doc.get('tag_id'))
                         tag.category = 'pd_book'  # make it look more lik a tag.
-                    else:
-                        # WTF
-                        print ("Warning. cannot get pdcounter tag_id=%d from db; cat=%s" % (
-                            int(doc.get('tag_id')), category)).encode('utf-8')
                     pd_tags.append(tag)
                 else:
                     tag = catalogue.models.Tag.objects.get(id=doc.get("tag_id"))
index a659329..70a216e 100644 (file)
@@ -10,7 +10,7 @@ from django.http import HttpResponse, JsonResponse
 from django.utils.translation import ugettext as _
 
 from catalogue.utils import split_tags
-from catalogue.models import Book
+from catalogue.models import Book, Tag
 from pdcounter.models import Author as PDCounterAuthor, BookStub as PDCounterBook
 from search.index import Search, SearchResult
 from suggest.forms import PublishingSuggestForm
@@ -66,26 +66,6 @@ def hint(request):
 
     prefix = remove_query_syntax_chars(prefix)
 
-    search = Search()
-    # tagi beda ograniczac tutaj
-    # ale tagi moga byc na ksiazce i na fragmentach
-    # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
-    # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
-
-    def is_dupe(tag):
-        if isinstance(tag, PDCounterAuthor):
-            if filter(lambda t: t.slug == tag.slug and t != tag, tags):
-                return True
-        elif isinstance(tag, PDCounterBook):
-            if filter(lambda b: b.slug == tag.slug, tags):
-                return True
-        return False
-
-    def category_name(c):
-        if c.startswith('pd_'):
-            c = c[len('pd_'):]
-        return _(c)
-
     try:
         limit = int(request.GET.get('max', ''))
     except ValueError:
@@ -94,33 +74,25 @@ def hint(request):
         if limit < 1:
             limit = -1
 
-    data = []
-
-    tags = search.hint_tags(prefix, pdcounter=True)
-    tags = filter(lambda t: not is_dupe(t), tags)
-    for t in tags:
-        if not limit:
-            break
-        limit -= 1
-        data.append({
-            'label': t.name,
-            'category': category_name(t.category),
-            'id': t.id,
-            'url': t.get_absolute_url()
-            })
-    if limit:
-        books = search.hint_books(prefix)
-        for b in books:
-            if not limit:
-                break
-            limit -= 1
-            data.append({
+    data = [
+        {
+            'label': author.name,
+            'category': _('author'),
+            'id': author.id,
+            'url': author.get_absolute_url(),
+        }
+        for author in Tag.objects.filter(category='author', name__iregex='\m' + prefix)[:10]
+    ]
+    if len(data) < limit:
+        data += [
+            {
                 'label': '<cite>%s</cite>, %s' % (b.title, b.author_unicode()),
                 'category': _('book'),
                 'id': b.id,
                 'url': b.get_absolute_url()
-                })
-
+            }
+            for b in Book.objects.filter(title__iregex='\m' + prefix)[:limit-len(data)]
+        ]
     callback = request.GET.get('callback', None)
     if callback:
         return HttpResponse("%s(%s);" % (callback, json.dumps(data)),
index 4d001e0..786a05c 100644 (file)
@@ -33,8 +33,7 @@ var __bind = function (self, fn) {
    
        render_item: function (ul, item) {
            return $("<li></li>").data('item.autocomplete', item)
-               .append('<a href="'+this.options.host+item.url+'"><span class="search-hint-label">'+item.label+'</span>'+
-                       '<span class="search-hint-category mono">'+item.category+'</span></a>')
+               .append('<a href="'+this.options.host+item.url+'"><span class="search-hint-label">'+item.label+'</span>')
                .appendTo(ul);
        }, 
 
index 14d8f0d..ad151d8 100755 (executable)
 
 .search-hint-label {
     display: inline-block;
-    @include size(font-size, 11px);
-    @include size(width, 275px);
+    @include size(font-size, 16px);
+    //@include size(width, 275px);
     line-height: 1.636em;
 }
-
-.search-hint-category {
-    @include size(font-size, 11px);
-    line-height: 1.636em;
-}
-