X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9dc1452931f801d6ad00d1238531769b3887a820..102acc0b6eb715826f26b5082611604cf6ebe240:/src/search/views.py?ds=inline diff --git a/src/search/views.py b/src/search/views.py index 459190d28..6066cd97a 100644 --- a/src/search/views.py +++ b/src/search/views.py @@ -24,22 +24,8 @@ def remove_query_syntax_chars(query, replace=' '): return query_syntax_chars.sub(replace, query) -@cache.never_cache -def hint(request, mozhint=False, param='term'): - prefix = request.GET.get(param, '') - if len(prefix) < 2: - return JsonResponse([], safe=False) - - prefix = re_escape(' '.join(remove_query_syntax_chars(prefix).split())) - - try: - limit = int(request.GET.get('max', '')) - except ValueError: - limit = 20 - else: - if limit < 1: - limit = 20 - +def get_hints(prefix, user=None, limit=10): + if not prefix: return [] data = [] if len(data) < limit: authors = catalogue.models.Tag.objects.filter( @@ -50,17 +36,21 @@ def hint(request, mozhint=False, param='term'): 'label': author.name, 'url': author.get_absolute_url(), 'img': get_thumbnail(author.photo, '72x72', crop='top').url if author.photo else '', + 'slug': author.slug, + 'id': author.id, } for author in authors[:limit - len(data)] ]) - if request.user.is_authenticated and len(data) < limit: + + if user is not None and user.is_authenticated and len(data) < limit: tags = social.models.UserList.objects.filter( - user=request.user, name__iregex='\m' + prefix).only('name', 'id', 'slug') + user=user, name__iregex='\m' + prefix).only('name', 'id', 'slug') data.extend([ { - 'type': 'set', + 'type': 'userlist', 'label': tag.name, 'url': tag.get_absolute_url(), + 'slug': tag.slug, } for tag in tags[:limit - len(data)] ]) @@ -72,6 +62,8 @@ def hint(request, mozhint=False, param='term'): 'type': tag.category, 'label': tag.name, 'url': tag.get_absolute_url(), + 'slug': tag.slug, + 'id': tag.id, } for tag in tags[:limit - len(data)] ]) @@ -83,6 +75,7 @@ def hint(request, mozhint=False, param='term'): 'type': 'collection', 'label': collection.title, 'url': collection.get_absolute_url(), + 'slug': collection.slug, } for collection in collections[:limit - len(data)] ]) @@ -99,6 +92,7 @@ def hint(request, mozhint=False, param='term'): 'author': author_str, 'url': b.get_absolute_url(), 'img': get_thumbnail(b.cover_clean, '72x72').url if b.cover_clean else '', + 'slug': b.slug, } ) if len(data) < limit: @@ -111,9 +105,34 @@ def hint(request, mozhint=False, param='term'): 'type': 'info', 'label': info.title, 'url': info.get_absolute_url(), + 'slug': info.slug, } for info in infos[:limit - len(data)] ]) + return data + + +@cache.never_cache +def hint(request, mozhint=False, param='term'): + prefix = request.GET.get(param, '') + if len(prefix) < 2: + return JsonResponse([], safe=False) + + prefix = re_escape(' '.join(remove_query_syntax_chars(prefix).split())) + + try: + limit = int(request.GET.get('max', '')) + except ValueError: + limit = 20 + else: + if limit < 1: + limit = 20 + + data = get_hints( + prefix, + user=request.user if request.user.is_authenticated else None, + limit=limit + ) if mozhint: data = [