1 # -*- coding: utf-8 -*-
3 from django.shortcuts import render_to_response, get_object_or_404
4 from django.template import RequestContext
5 from django.contrib.auth.decorators import login_required
6 from django.views.decorators import cache
7 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
8 from django.utils.translation import ugettext as _
10 from catalogue.utils import get_random_hash
11 from catalogue.models import Book, Tag, Fragment, TAG_CATEGORIES
12 from catalogue.fields import dumps
13 from catalogue.views import JSONResponse
14 from catalogue import forms
15 from search import MultiSearch, JVM, SearchResult
16 from lucene import StringReader
17 from suggest.forms import PublishingSuggestForm
21 dictionary = enchant.Dict('pl_PL')
24 def did_you_mean(query, tokens):
26 # sprawdzić, czy słowo nie jest aby autorem - proste szukanie termu w author!
28 print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
29 if not dictionary.check(t):
31 change[t] = dictionary.suggest(t)[0]
38 for frm, to in change.items():
39 query = query.replace(frm, to)
44 def category_name(category):
46 return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
48 raise KeyError("No category %s" % category)
52 prefix = request.GET.get('term', '')
54 return JSONResponse([])
55 JVM.attachCurrentThread()
60 tags = request.GET.get('tags', '')
61 hint.tags(Tag.get_tag_list(tags))
65 # tagi beda ograniczac tutaj
66 # ale tagi moga byc na ksiazce i na fragmentach
67 # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
68 # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
71 tags = s.hint_tags(prefix)
72 books = s.hint_books(prefix)
78 'category': _(category_name(t.category)),
80 'url': t.get_absolute_url()}
83 'category': _(category_name('book')),
85 'url': b.get_absolute_url()}
89 def foo(s, q, tag_list=None):
92 tag_list = Tag.get_tag_list(tag_list)
103 JVM.attachCurrentThread() # where to put this?
110 if 'q' in request.GET:
111 tags = request.GET.get('tags', '')
112 query = request.GET['q']
113 book_id = request.GET.get('book', None)
115 if book_id is not None:
116 book = get_object_or_404(Book, id=book_id)
120 tag_list = Tag.get_tag_list(tags)
125 return render_to_response('catalogue/search_too_short.html', {'tags': tag_list, 'prefix': query},
126 context_instance=RequestContext(request))
132 toks = StringReader(query)
133 fuzzy = 'fuzzy' in request.GET
137 results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy, hint=hint),
138 srch.search_perfect_parts(toks, fuzzy=fuzzy, hint=hint),
139 srch.search_everywhere(toks, fuzzy=fuzzy, hint=hint))
140 results.sort(reverse=True)
145 if len(results) == 1:
146 if len(results[0].hits) == 0:
147 return HttpResponseRedirect(results[0].book.get_absolute_url())
148 elif len(results[0].hits) == 1 and results[0].hits[0] is not None:
149 frag = Fragment.objects.get(anchor=results[0].hits[0])
150 return HttpResponseRedirect(frag.get_absolute_url())
151 elif len(results) == 0:
152 form = PublishingSuggestForm(initial={"books": query + ", "})
153 return render_to_response('catalogue/search_no_hits.html',
154 {'tags': tag_list, 'prefix': query, "pubsuggest_form": form,
155 'form': forms.SearchForm()},
156 context_instance=RequestContext(request))
158 return render_to_response('catalogue/search_multiple_hits.html',
159 {'tags': tag_list, 'prefix': query,
160 'results': results, 'from': forms.SearchForm()},
161 context_instance=RequestContext(request))
163 # return render_to_response('newsearch/search.html', {'results': results,
164 # 'did_you_mean': (query is not None) and
165 # did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
167 # context_instance=RequestContext(request))