fix <cite> in autocomplete
[wolnelektury.git] / src / search / views.py
index 70a216e..7e512be 100644 (file)
@@ -7,16 +7,16 @@ from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.views.decorators import cache
 from django.http import HttpResponse, JsonResponse
 from django.template import RequestContext
 from django.views.decorators import cache
 from django.http import HttpResponse, JsonResponse
-from django.utils.translation import ugettext as _
 
 from catalogue.utils import split_tags
 from catalogue.models import Book, Tag
 
 from catalogue.utils import split_tags
 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
 import re
 import json
 
 from search.index import Search, SearchResult
 from suggest.forms import PublishingSuggestForm
 import re
 import json
 
+from wolnelektury.utils import re_escape
+
 
 def match_word_re(word):
     if 'sqlite' in settings.DATABASES['default']['ENGINE']:
 
 def match_word_re(word):
     if 'sqlite' in settings.DATABASES['default']['ENGINE']:
@@ -29,7 +29,7 @@ query_syntax_chars = re.compile(r"[\\/*:(){}]")
 
 
 def remove_query_syntax_chars(query, replace=' '):
 
 
 def remove_query_syntax_chars(query, replace=' '):
-    return query_syntax_chars.sub(' ', query)
+    return query_syntax_chars.sub(replace, query)
 
 
 def did_you_mean(query, tokens):
 
 
 def did_you_mean(query, tokens):
@@ -64,30 +64,31 @@ def hint(request):
     if len(prefix) < 2:
         return JsonResponse([], safe=False)
 
     if len(prefix) < 2:
         return JsonResponse([], safe=False)
 
-    prefix = remove_query_syntax_chars(prefix)
+    prefix = re_escape(' '.join(remove_query_syntax_chars(prefix).split()))
 
     try:
         limit = int(request.GET.get('max', ''))
     except ValueError:
 
     try:
         limit = int(request.GET.get('max', ''))
     except ValueError:
-        limit = -1
+        limit = 20
     else:
         if limit < 1:
     else:
         if limit < 1:
-            limit = -1
+            limit = 20
 
 
+    authors = Tag.objects.filter(
+        category='author', name_pl__iregex='\m' + prefix).only('name', 'id', 'slug', 'category')
     data = [
         {
             'label': author.name,
     data = [
         {
             'label': author.name,
-            'category': _('author'),
             'id': author.id,
             'url': author.get_absolute_url(),
         }
             'id': author.id,
             'url': author.get_absolute_url(),
         }
-        for author in Tag.objects.filter(category='author', name__iregex='\m' + prefix)[:10]
+        for author in authors[:limit]
     ]
     if len(data) < limit:
         data += [
             {
     ]
     if len(data) < limit:
         data += [
             {
-                'label': '<cite>%s</cite>, %s' % (b.title, b.author_unicode()),
-                'category': _('book'),
+                'label': b.title,
+                'author': b.author_unicode(),
                 'id': b.id,
                 'url': b.get_absolute_url()
             }
                 'id': b.id,
                 'url': b.get_absolute_url()
             }