From: Jan Szejko Date: Wed, 13 Dec 2017 16:40:21 +0000 (+0100) Subject: revamp search hints X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/5074895f7d96cbd5fda6a4d566ddb25533137fd4?hp=1d5891fb132659113423705d8d8c982144ef7a8c revamp search hints --- diff --git a/src/search/context_processors.py b/src/search/context_processors.py index a3f1ea912..6ad2fe9e2 100644 --- a/src/search/context_processors.py +++ b/src/search/context_processors.py @@ -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)} diff --git a/src/search/index.py b/src/search/index.py index 7bc61c5c6..ce60978d5 100644 --- a/src/search/index.py +++ b/src/search/index.py @@ -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")) diff --git a/src/search/views.py b/src/search/views.py index a659329f3..70a216e3b 100644 --- a/src/search/views.py +++ b/src/search/views.py @@ -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': '%s, %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)), diff --git a/src/wolnelektury/static/js/search.js b/src/wolnelektury/static/js/search.js index 4d001e0c0..786a05c79 100644 --- a/src/wolnelektury/static/js/search.js +++ b/src/wolnelektury/static/js/search.js @@ -33,8 +33,7 @@ var __bind = function (self, fn) { render_item: function (ul, item) { return $("
  • ").data('item.autocomplete', item) - .append(''+item.label+''+ - ''+item.category+'') + .append(''+item.label+'') .appendTo(ul); }, diff --git a/src/wolnelektury/static/scss/main/search.scss b/src/wolnelektury/static/scss/main/search.scss index 14d8f0d92..ad151d80f 100755 --- a/src/wolnelektury/static/scss/main/search.scss +++ b/src/wolnelektury/static/scss/main/search.scss @@ -96,13 +96,7 @@ .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; -} -