X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9a0deb49e85ee676f71f15abafa382ebf4cf92ee..2ea9be2319952e1b977b9a175559871589c1046e:/apps/search/views.py diff --git a/apps/search/views.py b/apps/search/views.py index fd5883ede..881815da1 100644 --- a/apps/search/views.py +++ b/apps/search/views.py @@ -10,6 +10,7 @@ from django.utils.translation import ugettext as _ from catalogue.utils import split_tags from catalogue.models import Book, Tag, Fragment +from pdcounter.models import Author as PDCounterAuthor, BookStub as PDCounterBook from catalogue.views import JSONResponse from search import Search, JVM, SearchResult from lucene import StringReader @@ -17,6 +18,7 @@ from suggest.forms import PublishingSuggestForm from time import sleep import re import enchant +import json dictionary = enchant.Dict('pl_PL') @@ -90,22 +92,40 @@ def hint(request): tags = search.hint_tags(prefix, pdcounter=True) books = search.hint_books(prefix) + + 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 + + tags = filter(lambda t: not is_dupe(t), tags) + def category_name(c): if c.startswith('pd_'): c = c[len('pd_'):] return _(c) - return JSONResponse( - [{'label': t.name, - 'category': category_name(t.category), - 'id': t.id, - 'url': t.get_absolute_url()} - for t in tags] + \ - [{'label': b.title, - 'category': _('book'), - 'id': b.id, - 'url': b.get_absolute_url()} - for b in books]) + callback = request.GET.get('callback', None) + data = [{'label': t.name, + 'category': category_name(t.category), + 'id': t.id, + 'url': t.get_absolute_url()} + for t in tags] + \ + [{'label': b.title, + 'category': _('book'), + 'id': b.id, + 'url': b.get_absolute_url()} + for b in books] + if callback: + return HttpResponse("%s(%s);" % (callback, json.dumps(data)), + content_type="application/json; charset=utf-8") + else: + return JSONResponse(data) + def main(request):