cover inheritance tests and fix
[wolnelektury.git] / apps / search / views.py
index dcd80b2..ec8275b 100644 (file)
@@ -10,15 +10,17 @@ from django.utils.translation import ugettext as _
 
 from catalogue.utils import split_tags
 from catalogue.models import Book, Tag, Fragment
 
 from catalogue.utils import split_tags
 from catalogue.models import Book, Tag, Fragment
-from catalogue.fields import dumps
+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
 from suggest.forms import PublishingSuggestForm
 from catalogue.views import JSONResponse
 from search import Search, JVM, SearchResult
 from lucene import StringReader
 from suggest.forms import PublishingSuggestForm
+from time import sleep
 import re
 import re
-import enchant
+#import enchant
+import json
 
 
-dictionary = enchant.Dict('pl_PL')
+#dictionary = enchant.Dict('en_US')
 
 
 def match_word_re(word):
 
 
 def match_word_re(word):
@@ -35,13 +37,14 @@ def did_you_mean(query, tokens):
         if len(authors) > 0:
             continue
 
         if len(authors) > 0:
             continue
 
-        if not dictionary.check(t):
-            try:
-                change_to = dictionary.suggest(t)[0].lower()
-                if change_to != t.lower():
-                    change[t] = change_to
-            except IndexError:
-                pass
+        if False:
+            if not dictionary.check(t):
+                try:
+                    change_to = dictionary.suggest(t)[0].lower()
+                    if change_to != t.lower():
+                        change[t] = change_to
+                except IndexError:
+                    pass
 
     if change == {}:
         return None
 
     if change == {}:
         return None
@@ -51,8 +54,21 @@ def did_you_mean(query, tokens):
 
     return query
 
 
     return query
 
+
 JVM.attachCurrentThread()
 JVM.attachCurrentThread()
-search = Search()
+_search = None
+
+
+def get_search():
+    global _search
+
+    while _search is False:
+        sleep(1)
+
+    if _search is None:
+        _search = False
+        _search = Search()
+    return _search
 
 
 def hint(request):
 
 
 def hint(request):
@@ -61,6 +77,7 @@ def hint(request):
         return JSONResponse([])
     JVM.attachCurrentThread()
 
         return JSONResponse([])
     JVM.attachCurrentThread()
 
+    search = get_search()
     hint = search.hint()
     try:
         tags = request.GET.get('tags', '')
     hint = search.hint()
     try:
         tags = request.GET.get('tags', '')
@@ -76,22 +93,40 @@ def hint(request):
     tags = search.hint_tags(prefix, pdcounter=True)
     books = search.hint_books(prefix)
 
     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)
 
     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):
 
 
 def main(request):
@@ -118,6 +153,7 @@ def main(request):
         return render_to_response('catalogue/search_too_short.html', {'prefix': query},
                                   context_instance=RequestContext(request))
 
         return render_to_response('catalogue/search_too_short.html', {'prefix': query},
                                   context_instance=RequestContext(request))
 
+    search = get_search()
     # hint.tags(tag_list)
     # if book:
     #     hint.books(book)
     # hint.tags(tag_list)
     # if book:
     #     hint.books(book)