add tag ramka
[wolnelektury.git] / src / search / views.py
index f7aa77c..14da6f6 100644 (file)
@@ -3,19 +3,18 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf import settings
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf import settings
-from django.shortcuts import render_to_response, get_object_or_404
+from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.views.decorators import cache
 from django.template import RequestContext
 from django.views.decorators import cache
-from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect, JsonResponse
+from django.http import HttpResponse, JsonResponse
 from django.utils.translation import ugettext as _
 
 from catalogue.utils import split_tags
 from django.utils.translation import ugettext as _
 
 from catalogue.utils import split_tags
-from catalogue.models import Book, Tag, Fragment
+from catalogue.models import Book
 from pdcounter.models import Author as PDCounterAuthor, BookStub as PDCounterBook
 from search.index import Search, SearchResult
 from suggest.forms import PublishingSuggestForm
 import re
 from pdcounter.models import Author as PDCounterAuthor, BookStub as PDCounterBook
 from search.index import Search, SearchResult
 from suggest.forms import PublishingSuggestForm
 import re
-#import enchant
 import json
 
 
 import json
 
 
@@ -116,7 +115,7 @@ def hint(request):
                 break
             limit -= 1
             data.append({
                 break
             limit -= 1
             data.append({
-                'label': b.title,
+                'label': '<cite>%s</cite>, %s' % (b.title, b.author_unicode()),
                 'category': _('book'),
                 'id': b.id,
                 'url': b.get_absolute_url()
                 'category': _('book'),
                 'id': b.id,
                 'url': b.get_absolute_url()
@@ -132,17 +131,19 @@ def hint(request):
 
 @cache.never_cache
 def main(request):
 
 @cache.never_cache
 def main(request):
-    results = {}
-
-    results = None
-    query = None
-
     query = request.GET.get('q', '')
     query = request.GET.get('q', '')
+    query = ' '.join(query.split())
+    # filter out private use characters
+    import unicodedata
+    query = ''.join(ch for ch in query if unicodedata.category(ch) != 'Co')
 
     if len(query) < 2:
 
     if len(query) < 2:
-        return render_to_response('catalogue/search_too_short.html',
-                                  {'prefix': query},
+        return render_to_response(
+            'catalogue/search_too_short.html', {'prefix': query},
             context_instance=RequestContext(request))
             context_instance=RequestContext(request))
+    elif len(query) > 256:
+        return render_to_response(
+            'catalogue/search_too_long.html', {'prefix': query}, context_instance=RequestContext(request))
 
     query = remove_query_syntax_chars(query)
     
 
     query = remove_query_syntax_chars(query)
     
@@ -151,7 +152,7 @@ def main(request):
     theme_terms = search.index.analyze(text=query, field="themes_pl") \
         + search.index.analyze(text=query, field="themes")
 
     theme_terms = search.index.analyze(text=query, field="themes_pl") \
         + search.index.analyze(text=query, field="themes")
 
-            # change hints
+    # change hints
     tags = search.hint_tags(query, pdcounter=True, prefix=False)
     tags = split_tags(tags)
 
     tags = search.hint_tags(query, pdcounter=True, prefix=False)
     tags = split_tags(tags)
 
@@ -221,7 +222,6 @@ def main(request):
     text_phrase = filter(ensure_exists, text_phrase)
     everywhere = filter(ensure_exists, everywhere)
 
     text_phrase = filter(ensure_exists, text_phrase)
     everywhere = filter(ensure_exists, everywhere)
 
-    results = author_results + translator_results + title_results + text_phrase + everywhere
     # ensure books do exists & sort them
     for res in (author_results, translator_results, title_results, text_phrase, everywhere):
         res.sort(reverse=True)
     # ensure books do exists & sort them
     for res in (author_results, translator_results, title_results, text_phrase, everywhere):
         res.sort(reverse=True)
@@ -234,22 +234,30 @@ def main(request):
     #         #frag = Fragment.objects.get(anchor=anchor)
     #         return HttpResponseRedirect(fragment_hits[0]['fragment'].get_absolute_url())
     #     return HttpResponseRedirect(results[0].book.get_absolute_url())
     #         #frag = Fragment.objects.get(anchor=anchor)
     #         return HttpResponseRedirect(fragment_hits[0]['fragment'].get_absolute_url())
     #     return HttpResponseRedirect(results[0].book.get_absolute_url())
-    if len(results) == 0:
+    if not (author_results or translator_results or title_results or text_phrase or everywhere):
         form = PublishingSuggestForm(initial={"books": query + ", "})
         form = PublishingSuggestForm(initial={"books": query + ", "})
-        return render_to_response('catalogue/search_no_hits.html',
-                                  {'tags': tags,
-                                   'prefix': query,
-                                   "form": form,
-                                   'did_you_mean': suggestion},
+        return render_to_response(
+            'catalogue/search_no_hits.html',
+            {
+                'tags': tags,
+                'prefix': query,
+                'form': form,
+                'did_you_mean': suggestion
+            },
             context_instance=RequestContext(request))
 
             context_instance=RequestContext(request))
 
-    return render_to_response('catalogue/search_multiple_hits.html',
-                              {'tags': tags,
-                               'prefix': query,
-                               'results': {'author': author_results,
-                                           'translator': translator_results,
-                                           'title': title_results,
-                                           'content': text_phrase,
-                                           'other': everywhere},
-                               'did_you_mean': suggestion},
+    return render_to_response(
+        'catalogue/search_multiple_hits.html',
+        {
+            'tags': tags,
+            'prefix': query,
+            'results': {
+                'author': author_results,
+                'translator': translator_results,
+                'title': title_results,
+                'content': text_phrase,
+                'other': everywhere
+            },
+            'did_you_mean': suggestion
+        },
         context_instance=RequestContext(request))
         context_instance=RequestContext(request))